docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Getting started with the Game State Machine sample

    Prerequisites

    This sample requires the following:

    • Unity 6000.7 or later
    • TextMeshPro Essential Resources, imported through Window > TextMeshPro > Import TMP Essential Resources.

    The sample reads input through either of Unity's two input systems, so the Input System package is optional.

    Import the sample

    1. Open Package Manager
    2. Select the Graph Toolkit package
    3. Navigate to the Samples tab
    4. Import the Game State Machine sample

    The Package Manager copies sample files in your Assets folder, that's now ready to use.

    Test the sample

    1. Open GameFlowStateScene.unity (Assets/Samples/Graph Toolkit/<installed package version>/GameStateMachine Sample/Assets/GameFlowStateScene.unity). The scene contains the endless runner game and a GameFlowRunner with the state machine already assigned.
    2. Open Game Flow State Machine.gameflow, in the same folder. Double-click it to open the state machine editor, and arrange the two windows so you can see both.
    3. Select any state and look at the graph inspector. The fields shown there are the state options of that state's type, such as the Duration of the Countdown state.
    4. Enter Play Mode.

    The game starts on the splash screen and moves to the title screen on its own. From there:

    • Press Space or the up arrow, click, or tap the screen to start a run, to jump, and to play again after a game over.
    • Press Escape to pause and unpause.

    While the game plays, the state machine editor shows its runtime status: the active state is animated, its exit transitions are colored by whether their conditions were met, and each condition gets a true, false or pending icon on its row in the transition inspector. An active transition is held green for a moment so that it is visible. Use the Debug View toggle in the state machine editor's toolbar to turn all of that off or on.

    The state machine usually changes state when gameplay code raises a signal. The Main Game state raises one when the player dies, the Ready and Game Over states raise one on a button press, and each is matched by a GameSignal condition on a transition. The name of each signal is a state option, so it can be changed in the graph inspector.

    Create your own game flow

    1. Click Assets > Create > Graph Toolkit Samples > Game Flow State Machine.
    2. Name the new state machine asset (for example, My Own Game Flow).
    3. Double-click it to open the state machine editor.
    4. Right-click in the canvas and create a state of each type the game needs, for example Splash, Ready or Pause.
    5. Select each state and set its options in the graph inspector.
    6. Right-click the state the game should start in and create an Entry transition on it. Without one, the runner starts in the first state that was authored and logs a warning on import.
    7. Connect the states, and add GameSignal, TimeInState, or blackboard variable conditions to the transition rules.

    Saving the state machine automatically builds the runtime asset and links it to the same disk asset. Drag the asset onto a GameFlowRunner component in a scene to run it.

    To add a state behaviour the sample does not cover, write two classes:

    1. A runtime class that derives from RuntimeGameState, is marked [Serializable], and overrides the methods you need.
    2. An editor class that derives from GameState, is marked [Serializable] and [State], declares its options in OnDefineOptions, and returns a new instance of your runtime class from CreateRuntimeState.
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)