This repository is a template for validating and testing aggregate applications written in Collektive using Unity game engine as simulator. This integration allows to test high-fidelity simulation of Collective Adaptive Systems (CASs), thanks to the powerful physics engine provived by the game development platform. This project is equipped with two example scenarios to demonstrates the capabilities of the Unity engine to model three-dimensional simulations of nodes running aggregate computation.
Companion artifact for the paper entitled "High-Fidelity Simulation of Aggregate Computing Systems with Collektivity"
Submitted to Coordination 2025 conference
- Filippo Gurioli (filippo.gurioli@studio.unibo.it)
- Martina Baiardi (m.baiardi@unibo.it)
- Angela Cortecchia (angela.cortecchia@unibo.it)
- Danilo Pianini (danilo.pianini@unibo.it)
Since the device behavior is defined by the aggregate program, while the environment behavior (including physics, position in the world, and neighborhood relationships) pertain to the game engine, the natural division of responsibilities is to have the game engine provide sensory data to the aggregate program, while the aggregate program computes the actuation commands back to the engine.
This architectural choice implication is that, when designing new case studies, users must, as first step, define the data types that represent sensors and actuators.
This is done using Protocol Buffers.
Protocol Buffers (protobuf) is a language-neutral, platform-neutral mechanism for serializing structured data.
It defines data models using a declarative syntax (.proto files) that describes their structure in an implementation-agnostic way.
From a single .proto definition, the protoc compiler can generate serialization source code for multiple languages automatically, including C#, Kotlin, Java, and many others.
To work, the Unity game engine invokes the aggregate program directly through Foreign-Function Interface (FFI) giving Sensor Data information,
which sends back its computation result as Actuation Data.
The directory structure for the Collektivity project is organized as follows:
collektivity/
├── collektive-backend/ # Backend library for Collektive computation
│ ├── lib/
│ │ ├── src/
│ │ │ ├── commonMain/
│ │ │ │ ├── kotlin/
│ │ │ │ │ └── it/unibo/collektive/unity/examples # Collektive entrypoint for the aggregrate program
│ │ │ │ └── proto/
│ │ │ │ └── user-defined-schema.proto # Protobuf definition for user defined data schema
│ │ │ └── ...
│ └── ...
├── Collektive.Unity/ # Unity engine integration to invoke Collektive computation
│ ├── Runtime/
│ │ ├── Node.cs # Unity Game Object Behavior, allows the binding with Collektive computation
│ │ ├── Example/ # Example for a Collektive gradient descent logic specialization
│ │ └── ...
│ of the `Node` class
│ └── ...
├── rich-scenario/ # Unity project for Rick Scenario Execution
│ ├── Assets/
│ │ ├── Scenes/
│ │ │ ├── Oasis/
│ │ │ │ └── Oasis.unity
│ │ │ └── ...
│ │ └── ...
│ └── ...
├── Sandbox.Collektive.Unity/ # Unity project for Simple Scenario Execution
│ ├── Assets/
│ │ ├── Scenes/
│ │ │ └── Robots and Obstacles.unity
│ │ └── ...
│ └── ...
└── ...
- Linux or macOS Operating Systems
- Unity 6000.3.8f1, downloadable from Unity Hub
- Java 21
- Install Java 21:
- Verify the installation with:
java -version. It should print a version string containing21.
- Verify the installation with:
- Install Unity Hub:
- Follow the official instructions from https://unity.com/download.
- Launch Unity Hub.
- Create a Unity account and log in.
- Install Unity Editor 6000.3.8f1:
- Open Unity Hub.
- Go to the
Installstab. - Click
Install Editor. - Select version
6000.3.8f1from the list and install it.
- Import the Project:
- Clone the project repository using
git clone git@github.com:pslab-unibo/collektivity.git - In Unity Hub, go to the \texttt{Projects} tab.
- Click
AddandAdd Project from Disk. - Browse to the
Sandbox.Collektive.Unityfolder in the artefact. - Select it and confirm.
- Install the recommended Unity packages if prompted (the installation may take a few minutes).
- Clone the project repository using
- Open the Project in Unity Editor:
- Double-click the project entry in Unity Hub.
- Read and accept the Unity Editor Software terms and conditions if prompted.
- Unity Editor will launch and load the project (note that it is not executable yet).
- In the top bar, click
Tools, then run bothProto > GenerateandNative > Rebuild backend.- If the environment does not respond after two minutes, send a kill signal to the Unity Editor process and restart it, then repeat this step.
- In the
Projecttab, navigate toAssets/Scenesand double-clickRobots and Obstacles.unityto open the scene. - Press the
Startbutton at the top center of the Unity Editor to launch the simulation.
- Clone the repository
- Open an unity editor project pointing at the
collektivity/Sandbox.Collektive.Unitydirectory - On the top bar click
Toolsbutton and then launch bothProto > GenerateandNative > Rebuild backend - From the
projecttab (usually at the bottom of the editor) open theAssets/Scenesdirectory and double click on theRobots and Obstacles.unityfile to open the scene - Start the simulation by pressing the start button at the center top of the unity editor.
- Open an unity editor project pointing at the
collektivity/rich-scenariodirectory - On the top bar click
Toolsbutton and then launch bothProto > GenerateandNative > Rebuild backend - From the
projecttab (usually at the bottom of the editor) open theAssets/Scenes/Oasisdirectory and double click on theOasis.unityfile to open the scene - Start the simulation by pressing the start button at the center top of the unity editor.
- Update the file at
collektivity/collektive-backend/lib/src/commonMain/proto/user-defined-schema.protoindicating the data schema forSensorDataandActuatorDataSensorDatathe data structure Unity will send to the Collektive computationActuatorDataoutput obtained from Collektive computation that is sent back to Unity
- Launch
./gradlew buildinside thecollektive-backenddirectory- this triggers the
protocompiler to generate data structures in Kotlin anc C#, that will then be used in your code
- this triggers the
- Update the
collketivity/collektive-backend/lib/src/commonMain/kotlin/it/unibo/collektive/unity/examples/UserDefinedEntrypoint.ktthe aggregate program you want to simulate - Open the Uunity editor pointing at the
collektivity/Sandbox.Collektive.Unitydirectory - In the top bar click
Toolsbutton and then launch bothProto > GenerateandNative > Rebuild backend - Create your own node behavior by developing a specialization of the the
collektivity/Collektive.Unity/Runtime/Node.csclass (a specialization of the UnityMonoBehaviour)- Implement the abstract methods
ActandSense
- Implement the abstract methods
- In the Unity editor, create a new scene and add your game objects to the scene, these will represent the node executed by the simulation
- Attach to those game objects the new class you've implemented as a specialization of
Node - Add the
Simulation Managerprefab to the scene (can be found atcollektivity/Collektive.Unity/Runtime/Prefabs/Simulation Manager.prefab) - Create a new
MonoBehaviourthat interact with theSimulationManager.Instancesingleton APIs to choose a neighborhood logic (see theProximityNeighborhoodBehaviourin thecollektive.unity/Collektive.Unity/Runtime/Example/directory for an example)- Attach to your game object the newly implemented neighboorhood logic
- Start your simulation by pressing the start button at the center top of the unity editor.

