C# scripting
Gameplay code in Neux is C#. A script is a class that derives from Behaviour; put it on an object with Add Component > Scripts & Data > Script, or drag the .cs file onto the object. Scripts compile when you save them and reload while the game plays, keeping the values of their fields.
You need the .NET SDK 8 or later on the machine that edits the game. A built game carries what it needs.
A first script
public class Spinner : Behaviour
{
public float speed = 90f; // degrees a second; shows in the Inspector
public override void Update(float deltaTime)
{
transform.Rotate(0f, speed * deltaTime, 0f);
if (Input.GetKeyDown("Space"))
speed = -speed;
}
}
Assets > Create > C# Script makes the file from a template and opens it in the code editor.
How a script runs
| Method | When |
|---|---|
Awake() | Once, when the object comes into the game. |
Start() | Once, before its first Update. IEnumerator Start() runs as a coroutine. |
Update(float deltaTime) | Every frame. Note the time is passed in, in seconds. |
FixedUpdate() | 50 times a second of game time, for physics. |
LateUpdate() | After every Update: cameras that follow. |
OnEnable(), OnDisable() | The script, or its object, is switched on or off (OnEnable also runs after Awake). |
OnCollisionEnter/Stay/Exit(GameObject other) | Contact with another object starts, goes on (every frame) or ends. |
OnTriggerEnter/Stay/Exit(GameObject other) | Another object enters, stays in or leaves a trigger zone. |
OnMouseDown(), OnMouseUp(), OnMouseEnter()... | The pointer and this object. |
OnDestroy() | The object is destroyed or its scene unloads. |
Fields in the Inspector
Public fields show in the Inspector with their own editor: numbers, text, colours, vectors, curves, enums, lists, asset references and references to other objects. The attributes [Header], [Tooltip], [Range], [Space], [Min], [TextArea], [Multiline] and [HideInInspector] shape how a field shows there.
Coroutines
IEnumerator Start()
{
yield return new WaitForSeconds(2f);
Debug.Log("two seconds later");
}
StartCoroutine, StopCoroutine, WaitForSeconds, WaitUntil, WaitWhile and WaitForEndOfFrame are there.
Saving the game
SaveGame keeps whole saved games in slots that survive restarts (a file on computers and phones, the browser's storage on the web):
var d = new SaveData();
d.SetInt("coins", coins);
d.SetString("level", "Caves");
SaveGame.SaveObjects("1", d); // also where every named object is, and whether it is on
var back = SaveGame.LoadObjects("1"); // puts the objects back and returns the values
coins = back.GetInt("coins");
SaveGame.Slots() lists the slots that hold a save (newest first), SaveGame.SavedAt(slot) says when, and PlayerPrefs stays for small settings.
Text in several languages
Localization.Get("menu.play") reads the string tables in Localization/*.csv. Setting Localization.language = "es" switches every text at once, labels in the game's UI included; Localization.languages lists the columns.
Coming from Unity
Most names match: GameObject, Transform, Input, Time, Physics.Raycast, PlayerPrefs, Debug.Log, Mathf, Vector3, Quaternion. The differences to know:
Updatetakes the frame time as an argument:Update(float deltaTime).- Keys are named by text:
Input.GetKey("W"),Input.GetKeyDown("Space"). - Scripts are screened before they compile: no file, network, process, reflection or native calls. Use
PlayerPrefs,ContentPacksand the engine's own API instead.
Unity is a trademark of Unity Technologies. Neux is not affiliated with or endorsed by Unity Technologies; the names above are common words for these ideas, and Neux's code and documentation are its own.
Editor scripts
[MenuItem("Tools/My Tool")], EditorWindow, [CustomEditor], Handles and OnSceneGUI extend the editor itself.
The whole API
Every type and member, generated from the file scripts are compiled against: API reference.
Complete scripts for the jobs most games have (a character that walks and jumps, a coin, a door, a guard, health, saving, a pause menu and more), each ready to copy: script cookbook.