Connect the UI to your game code
The generated C# class: react to clicks, set values, fill lists and hear item actions.
Your menus already work in Unity on their own. But your game still needs to talk to them: start the level when Play is clicked, update the health bar when the player is hit, fill the shop with real items. This page shows how. It is written for programmers.
The generated UI class
After every import (and after Live Sync), Rigonix writes a C# file for your UI, named after your project. For a project called "Space Runner" it is SpaceRunnerUI, in the Rigonix.Generated namespace. Find it in the imported folder under Scripts, or use Rigonix → Generate UI Code to write it now.
The class mirrors your project: one class per screen, one property per element, plus your variables, data lists, groups and sounds. The names are your names, so "Play Button" on "Main Menu" becomes SpaceRunnerUI.MainMenu.PlayButton. Your code editor autocompletes them.
using Rigonix.Generated;
using UnityEngine;
public class GameFlow : MonoBehaviour
{
void Start()
{
// React to a click.
SpaceRunnerUI.MainMenu.PlayButton.onClick.AddListener(StartGame);
}
void StartGame()
{
// Change a variable: everything bound to it updates.
SpaceRunnerUI.Vars.Coins.Value += 10;
}
}
Common jobs
Show a value. Bind the element to a variable in Rigonix (see Variables), then change the variable from code:
SpaceRunnerUI.Vars.Health.Value = 72; // the health bar moves
SpaceRunnerUI.Vars.Score.Value += 100; // "Score {score}" updates
Show or hide a screen (with an optional transition):
SpaceRunnerUI.Settings.Show("fade");
Fill a list (shop, leaderboard, levels). Your rows replace the sample rows, and every repeater showing the list updates:
SpaceRunnerUI.Data.ShopItems.Set(items);
Hear item actions. A Buy button inside a shop card with the action Item Action → buy reaches your code with the item:
SpaceRunnerUI.Shop.ShopItems.ItemAction += (action, index, item) =>
{
if (action == "buy") TryToBuy(index);
};
Play a UI sound:
SpaceRunnerUI.PlaySound(SpaceRunnerUI.Sounds.Coin); // Coin: one of your sounds
Good to know
- Rename things in Rigonix and the generated names change too. Your code editor shows you what to update.
- The file is only rewritten when something your code can use changes. Moving or recolouring things never forces a recompile.
- Variables with Save between sessions on are saved on the player's device and loaded on the next start, with no code needed.
- Prefer a different setup? Every element also has a
RigonixComponentyou can find withGetComponentlike any Unity component.