Network Spawner
1. Overview
The Network Spawner is a powerful utility in HoloMIT for instantiating GameObjects across all connected users. Unlike standard Unity instantiation, which only creates objects locally, the NetworkSpawner ensures that every client creates the same object with identical NetworkId identifiers, keeping the scene state consistent across the network.
This system is essential for spawning dynamic objects like projectiles, tools, or interactive items during a session.
2. NetworkSpawner (Core Component)
The NetworkSpawner acts as a factory. It maps a list of prefabs to unique indices that can be communicated over the network.
2.1. Inspector Reference
Figure 1: The NetworkSpawner component configuration.
- Spawnable Prefabs (
List<GameObject>): The database of objects this spawner can create. The index in this list is the "key" sent to other clients.
2.2. Public API
public GameObject Spawn(int prefabIndex, Vector3 position, Quaternion rotation):- Parameters:
int prefabIndex: The index of the prefab to instantiate, as defined in the Spawnable Prefabs list in the Inspector.Vector3 position: The world space position where the object will be created.Quaternion rotation: The initial world space orientation for the spawned object.
- Local Action: Instantiates the prefab at
prefabIndex. - Network Action: Collects all
NetworkIds from the hierarchy and broadcasts aNetworkSpawnDatamessage. - Returns: The locally created GameObject.
- Parameters:
3. SimpleSpawner (Sample Implementation)
To help developers understand how to use the system, HoloMIT includes a SimpleSpawner script. This script demonstrates how to bridge user input (Keyboard or VR Controllers) with the spawning logic.
3.1. Code Analysis
The SimpleSpawner uses the HoloMITControls system to listen for specific actions:
- Keyboard '1' / Button A: Spawns the prefab at Index 0.
- Keyboard '2' / Button B: Spawns the prefab at Index 1 with a slight offset.
// Example usage in SimpleSpawner.cs
if (m_Controls.Generic.Keyboard_1.WasPressedThisFrame()) {
Spawner.Spawn(0, transform.position, transform.rotation);
}
3.2. Customization
Developers are encouraged to use SimpleSpawner as a template. You can modify it to:
- Add more complex spawn logic (e.g., spawning on collision).
- Dynamically calculate spawn positions.
- Integrate with your own custom input actions.
4. SDK Sample: TestNetworkSpawner
The SDK provides a ready-to-use sample scene to test this feature:
- Scene Path:
Assets/DevelopmentTests/NetworkSpawner/TestNetworkSpawner.unity - Content: This scene contains a pre-configured
NetworkSpawnerand uses theSimpleSpawner.cscomponent to allow immediate testing in Play Mode.
5. Integration with Network Triggers
One of the most powerful aspects of the system is the ability to combine Spawners with other components. If the prefabs registered in the NetworkSpawner contain Network Triggers, you can create complex networked behaviors.
For example:
- Use
NetworkSpawnerto create a "Bomb" prefab. - The Bomb prefab has a
NetworkTriggerfor the explosion. - Once spawned, any client can activate the trigger, ensuring the explosion happens simultaneously for everyone.
6. Setup Guidelines & Best Practices
- Identical Prefab Lists: You MUST ensure that the
Spawnable Prefabslist is exactly the same on all clients. If the order differs, Client A might spawn a "Cube" while Client B spawns a "Sphere". - NetworkId at Root: Always ensure your spawnable prefabs have a
NetworkIdBehaviourcomponent at their root. - Master Client Relay: The system automatically handles forwarding spawn events if the initiator is not the Master Client, ensuring everyone receives the message.
7. Common Pitfalls
- Resource Loading: All prefabs in the list must be loaded into memory. If you have a very large number of spawnable items, consider using multiple specialized spawners.
- Initial State: If your spawned object needs to set up specific networked variables immediately after spawning, wait at least one frame or use the return value of
Spawn()on the initiator.