C# Scripting

The Spatial Creator Toolkit supports traditional Unity C# Scripting. If you're familiar with scripting in Unity, you'll feel right at home with the Creator Toolkit!

Click here to view the full API reference for the Creator Toolkit C# Scripting API.

Interpreted C#

C# code on Spatial is interpreted at runtime rather then compiled ahead of time. This comes with 5x to 10x execution time penalty compared to traditional pre-compiled C#.

Limitations

Only a subset of C# and Unity APIs are supported with replacements provided where neccessary.

Click here to read more about the C# Limitations.

Packages Types

C# scripting is only supported with Space packages. Visual Scripting can be used instead for other package types.

Getting Started

Before you start programming you'll want to make sure have a code editor setup.

Click here to download and setup VSCode with Unity

Creating an Assembly

In Spatial all scripts uploaded with a space need to be contained in a single Assembly. You mark a folder and all its contained scripts as an assembly by creating an Assembly Definition asset in the folder.

In the toolkit this can be done quickly by clicking the Create New Assembly button in the space package config.

Image showing the create new assembly button in the package config highlighted

Creating a Script

After creating an assembly you can create a script inside it by right clicking the folder and selecting Create/C# Script.

If your script is not in the same folder as the assembly definition, or one of its sub-folders your script will not be uploaded to Spatial.

Image showing a script file created in the same folder as an assembly definition

Using MonoBehaviors

By default C# Scripts in Unity are MonoBehaviors. These are components that you can attach to a GameObject.

Below is an example of a MonoBehavior that when attached to a GameObject continually rotates it on the y-axis.

using UnityEngine;
public class Rotate : MonoBehaviour
{
public float rotateSpeed = 50f;
void Update()
{
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
}
}
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float rotateSpeed = 50f;
void Update()
{
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
}
}

Your MonoBehavior can then be added to a gameObject by using the AddComponent button inside the Inspector.

Image highlighting a component and the add component button inside the unity inspector

Using Spatial Bridge

When it comes time to interact with the Spatial platform this is done through the Spatial Bridge. This is a class that contains all the services and API's we provide.

Bellow is an example of using the bridge to display a popup message to the user. Note how SpatialSys.UnitySDK namespace needs to be included in order to access the SpatialBridge.

using UnityEngine;
using SpatialSys.UnitySDK;
public class MessagePopup : MonoBehaviour
{
void Start()
{
SpatialBridge.coreGUIService.DisplayToastMessage("Hello from Unity!");
}
}
using UnityEngine;
using SpatialSys.UnitySDK;
public class MessagePopup : MonoBehaviour
{
void Start()
{
SpatialBridge.coreGUIService.DisplayToastMessage("Hello from Unity!");
}
}

Continue Learning

If you are new to Unity and want to learn more about scripting you can follow Unity's scripting documentation and guides.

Otherwise, dive into the Spatial C# API Reference, check out our Templates or join the Spatial Community for more help and resources. We can't wait to see what you build!