Skip to main content

Unity Template Reference

This page documents what the SIMPLE Unity template contains and how to use its C# scripting API.


Scenes

The template ships with scenes organized in Assets/Scenes/.

Code Examples

Located in Assets/Scenes/Code Examples/. Each scene pairs with a matching GAMA model in the plugin's models/LinkToUnity/Models/Code Examples/ folder.

SceneWhat it demonstrates
Limit Player MovementRestricting player movement in Unity via GAMA commands
Receive DEM DataReceiving a grid/heightmap from GAMA and modifying it at runtime
Receive Dynamic DataReceiving dynamic geometry/agent positions from GAMA every step
Receive Static DataReceiving static geometries from GAMA once at initialization
Receive Water DataReceiving water surface data and updating it at runtime
Send Receive MessageBidirectional messaging between Unity and GAMA
User InteractionsDefining and triggering player interactions from GAMA

Located in Assets/Scenes/Menu/.

SceneRole
Startup MenuEntry point. Loads the IP Menu and Main Scene. Also lets the player choose whether to use the WebPlatform or connect directly to GAMA.
IP MenuLets the player enter the IP address of the machine running the WebPlatform. The entered IP is persisted between sessions.
End of Game MenuDisplays end-of-session information and offers a restart button.

Demo

Located in Assets/Scenes/Demo/. Complete playable experiences:

SceneDescription
Single Player GameA single-player game where the player selects cars and motorbikes to remove, grabs a tree, and designates buildings as hotspots.
Multi-Player GameA multi-player race where each player collects treasures. Player synchronization is handled through GAMA.

Scene Templates

Two minimal templates for starting a new VU scene from scratch:

TemplatePlayer type
Main Scene - FPS PlayerFirst-person player: walks on the ground and can teleport
Main Scene - Sky View PlayerTop-down/flying player: no gravity, moves horizontally and vertically

Prefabs

Located in Assets/Resources/Prefabs/.

PrefabRole
Connection ManagerManages the WebSocket connection between Unity and the WebPlatform. Required in every Main Scene.
Game ManagerManages game state and handles messages sent to/from GAMA. Required in every Main Scene.

Player

PrefabDescription
FPS PlayerFirst-person player. Walks on a ground surface and can teleport.
Sky View PlayerFlying player. No gravity; moves horizontally and vertically. Useful for top-down simulation overviews.

Utils

PrefabDescription
Debug OverlayDisplays all Debug.Log() output directly on-screen inside the headset. Useful during development.

C# API

All *Manager scripts use the Singleton pattern — they are instantiated automatically on the Managers GameObject at startup. Do not instantiate them manually. To call a method from any manager script, use:

NameOfClassManager.Instance.SomeMethod();

ConnectionManager

Extends WebSocketConnector. Manages the connection lifecycle between Unity and the WebPlatform. The Connection Manager prefab must be present in your scene.

Connection states

ConnectionManager maintains a state machine (ConnectionState enum). Transitions fire the OnConnectionStateChange event.

Events

EventSignatureFired when
OnConnectionStateChange(ConnectionState newState)The connection state transitions
OnConnectionStateReceived(JObject payload)A "json_state" message is received from the server
OnConnectionAttempted(bool connectionSuccess)A connection attempt completes (true = success)
OnServerMessageReceivedA "json_simulation" message is received

Methods

MethodReturnsDescription
TryConnectionToServer()Attempts to connect to the WebPlatform
IsConnectionState(ConnectionState s)boolReturns true if the current state matches s
GetConnectionId()stringReturns the player ID generated at connection time (based on headset IP)
SendExecutableExpression(string expression)Sends a GAML expression string to GAMA for immediate execution in the experiment context. The expression is compiled by GAMA — mind special characters (;, ", …).
SendExecutableAsk(string action, Dictionary<string,string> args)Asks an agent in the simulation to trigger action with the given argument values. Faster than SendExecutableExpression but only supports simple values, not complex GAML expressions.
UpdateConnectionState(ConnectionState newState)Manually sets the connection state. Use with caution — can break the default connection flow.

SimulationManager

The core script for handling GAMA simulation data in Unity. Manages game state and processes incoming geometry messages.

Game states

SimulationManager maintains a GameState enum. State transitions fire OnGameStateChanged.

Events

EventSignatureFired when
OnGameStateChanged(GameState newState)The game state changes
OnGameRestartedRestartGame() is called
OnGeometriesInitialized(GAMAGeometry geometries)Initial geometries from GAMA are fully converted into Unity polygons. Fires just before the state transitions from LOADING_DATA to GAME.
note

OnGeometriesInitialized fires when geometry data is processed, not when it is received. Hook to this event to separate geometry loading logic from game state transitions.

Methods

MethodReturnsDescription
GetCurrentState()GameStateReturns the current game state
IsGameState(GameState state)boolReturns true if the current state matches state
UpdateGameState(GameState newState)Manually sets the game state. Use with caution — can break initialization and connection flows.