This guide provides a rapid introduction to integrating and using the AgentSimMiddleware with a Unity project. It focuses on the minimal steps required to get a basic simulation up and running, demonstrating how to bridge the C# Unity environment with the native C++ simulation core.
- A Unity project (version 2021.3 or newer recommended).
- The
AgentSimMiddlewareC++ DLL (dynamic link library) for your target platform. Follow the C++ Core Build Instructions to compile it.
Copy the following essential C# files from AgentSimMiddleware/src/csharp/ into your Unity project's Assets folder (e.g., Assets/AgentSimMiddleware/Scripts/):
UnityAdapter.cs(Handles P/Invoke calls to the C++ DLL)AgentBehaviour.cs(Example script for agents in Unity)SimulationManager.cs(Orchestrates the simulation in Unity)SimulationSettings.cs(ScriptableObject for simulation configuration)DataStructures.cs(Core data types shared between C# and C++)AgentCommand.cs(Defines commands agents send to Unity)WorldSnapshot.cs(Defines the world state structure)WorldSnapshotMarshaler.cs(Helper for marshaling WorldSnapshots)AgentDebugVisualizer.cs(For in-editor debugging, if desired)DebugSettings.cs(Debug toggles for visualizations)DebugUI.cs(UI Toolkit integration for debug toggles)
Place the compiled C++ DLL (e.g., AgentSimMiddleware.dll for Windows) into your Unity project's Assets/Plugins/ folder. Unity will automatically manage its loading.
The C++ simulation core uses JSON files to define agent goals and actions.
- Copy
AgentSimMiddleware/data/actions_goals.jsoninto your Unity project'sAssets/Resources/folder (you may need to create this folder if it doesn't exist). This file defines the GOAP logic for your agents.
Create a SimulationSettings ScriptableObject to manage your simulation parameters.
- In the Unity Editor, right-click in your Project window ->
Create->AgentSim->Simulation Settings. Name this assetQuickStartSimulationSettings. - Select
QuickStartSimulationSettingsin the Project window and configure its properties in the Inspector:- Max Agents: Set a reasonable number (e.g., 100).
- GOAP Json File Path: Set this to
actions_goals(matching the file you copied in Step 3). - Agent Prefab: Create a simple 3D GameObject (e.g., a Cube or Sphere), turn it into a Prefab, and drag that Prefab here. This will be the visual representation of your agents.
- Adjust
Full Simulation DistanceandLite Simulation Distanceas desired for LOD.
The SimulationManager MonoBehaviour orchestrates the entire simulation within Unity.
- Create an empty GameObject in your Unity scene (e.g.,
GameObject->Create Empty) and name itSimulationManagerObject. - Add the
SimulationManager.csscript to this new GameObject (Add Component-> search forSimulation Manager). - Drag your
QuickStartSimulationSettingsasset from the Project window to theSettingsfield of theSimulationManagercomponent in the Inspector. - (Optional for Debug UI) Add a
UIDocumentcomponent to theSimulationManagerObject(Add Component-> search forUI Document). Drag theDebugUI.uxmlfile (found inAssets/AgentSimMiddleware/UI/) to itsSource Assetfield. This will enable in-editor toggles for debug visualizations.
- Save your Unity scene.
- Press the Play button in the Unity Editor.
You should observe the following:
- Debug messages in the Unity console confirming the C++ simulation's initialization.
- The specified number of agent prefabs instantiated in your scene, moving and reacting according to the loaded GOAP logic.
- If enabled in
SimulationSettings, debug visualizations will be active in the Scene view (e.g., agent IDs, states, LOD tiers).
This quick start guides you through the essential setup. For more advanced configurations and detailed API usage, refer to the Unity Integration Guide and the C++ Core API Reference.