Getting Started

Introduction

Installation

Project Configuration

Testing in Your Sandbox

Samples & Examples

Core Concepts & Glossary

Package Types

Spaces & Space Templates

Custom Avatars

Custom Avatar Animations

Avatar Attachments

Custom Prefab Objects

Embedded Packages

Drivable Vehicles

Scene Setup

Testing In Unity vs Sandbox

Controlling the Camera

Custom Collision, Layers, and Tags

Audio Mixers and Groups

Key Differences from Standard Unity3D development

Economy

Quests and Rewards

Economy Overview

Monetization

Items

Consumable Items

Rewarding Items

World Currency

Selling Items

Scripting

Components

Entrance Point

Camera Passthrough

Interactable

Trigger Event

Point Of Interest

Environment Settings Overrides

Render Pipeline Settings Overrides

Movement Materials

Climbable

Avatar Teleporter

Empty Frame

Projector Surface

Seat Hotspot

Guidelines

Supported Features and Limitations

Performance Guidelines

Lighting

Publishing to Spatial

Finding Published Packages

Support

FAQs

Help and Support

Release Notes

Asset Import Settings

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.

<aside> <img src="/icons/warning_yellow.svg" alt="/icons/warning_yellow.svg" width="40px" /> Remember! Only scripts inside the assembly folder will be included in your sandbox and published spaces.

</aside>

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!

Screenshot 2023-11-09 at 9.37.13 AM.png

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.

<aside> 🗒️ The [SpatialBridge](<https://cs.spatial.io/reference/SpatialSys.UnitySDK.SpatialBridge>) is the main object that provides access to Spatial services and acts as a bridge between user code and Spatial core functionality.

</aside>

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

Getting Started

Next →

C# Limitations