Set up and run the sample
The Runtime Serialization sample shows how to save and load the full state of behavior agents at runtime.
Use this section to do the following:
- Import the sample
- Explore its contents
- Run the scene
- Test serialization by demonstrating how agent positions and their behavior graph states are preserved across play sessions
Import and open the sample scene
To use the Runtime Serialization sample, follow these steps:
- Open the Package Manager window.
- Select In Project > Behavior > Samples.
- Select Import next to the Runtime Serialization sample.
- To locate the sample components, go to Project > Assets> Sample > Behavior > [version-number] > Runtime Serialization.
The Runtime Serialization folder contains the following components:
Component | Name |
---|---|
Scene | Serialization.unity |
Behavior graph | Agent.graph |
Prefabs | Agent and Weapon |
Materials | Two materials for visuals |
Scripts | * SerializationExampleSceneController.cs * ChooseTargetPosition.cs * GameObjectResolver.cs (embedded example) |
Run the sample
The Runtime Serialization sample shows how to save and restore agent state at runtime.
To run the Serialization.unity
scene, follow these steps:
Double-click the
Serialization.unity
scene to open it.Enter Play mode.
The scene shows 15 capsule agents that move around wielding swords.
Select Save to serialize the current state of all the agents.
Select Load to restore that state at any time, even after you exit and re-enter Play mode.
Runtime serialization restores the agent positions and saves the full state of each agent's behavior graph. When you reload the graph, the agents resume their behavior from the exact point they left off.