Getting Started
Package Types
Scene Setup
Economy
Scripting
Components
Guidelines
Support
C# Getting Started: Your First Script
Code in a Unity project is written using C# script files. These can be created using Create/C# Script
.
By default scripts are of class MonoBehaviour
which can be attached to game objects. But Spatial also supports static
classes, ScriptableObjects
, custom class
& struct
types, and inheritance
.
Setup Your Code Environment
Programming is easier with a nice editor. We recommend using VSCode. Setup instructions can be found at the link below.
Example Script
Create a new script with Create/C# Script
inside our scripts folder and copy/paste the following code into the body of your new MonoBehaviour
public Vector3 rotationAngles = new Vector3(0.0f, 50.0f, 0.0f);
private void Update()
{
transform.Rotate(rotationAngles * Time.deltaTime);
}
Now, you can add your script to any game object as a component
using the Add Component
button or by dragging the script into the game object’s inspector. When you enter play mode, or publish your space, you should now see the object spinning!
A Spatial-Specific Example
Create a new C# script in your project and add it to an object in your scene’s hierarchy. Open the script in your favorite editor and add the following using
statement to the existing using statements:
using SpatialSys.UnitySDK;
Next, in the Start()
event, add the following line of code:
void Start()
{
SpatialBridge.coreGUIService.SetCoreGUIEnabled(SpatialCoreGUITypeFlags.Chat, false);
}
You can use the coreGUIService
to manage other Spatial UI elements to give you a bit more control over your game’s experience. The SetCoreGUIEnabled(SpatialCoreGUITypeFlags.Chat, false)
will disable Spatial’s chat and prevent users from communicating via text. See here for the other UI type flags available.
SpatialBridge
is the main object that provides access to Spatial services and acts as a bridge between user code and Spatial core functionality.The full code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SpatialSys.UnitySDK;
public class YourScriptFileName: MonoBehaviour
{
void Start()
{
SpatialBridge.coreGUIService.SetCoreGUIEnabled(SpatialCoreGUITypeFlags.Chat, false);
}
}
You’re now ready to start adding C# to your Spatial Creator Toolkit project! You can see the Spatial Creator Toolkit C# samples for more inspiration and examples.
← Previous
Next →