Skip to content

๐ŸŽฎ Spawning

Overviewโš“๏ธ

Omni supports spawning objects on both the server and client, providing several methods for network object instantiation. In this guide, weโ€™ll walk you through the available options, from the simplest to the most advanced approaches, enabling you to choose the best method based on your projectโ€™s requirements.

Each method allows for a different level of control over networked objects, from basic automatic spawning to more complex, customizable instantiation processes.

Simple Instantiationโš“๏ธ

The Network Identity Spawner component provides a quick and efficient way to instantiate networked objects, making it ideal for prototyping stages. This component simplifies the process by automatically handling the instantiation of both prefabs and scene objects over the network.

With Network Identity Spawner, you can set up networked objects with minimal configuration, allowing you to focus on testing gameplay and mechanics rather than complex network setups.

  • Add the Network Identity Spawner component to any game object in your scene.
Field Description
Local Player A NetworkIdentity reference for the object that will be controlled by the local player. This object will receive input and commands directly from the local player.
Identities to Spawn A list of NetworkIdentity references for objects to be managed by the server. These objects will be instantiated over the network and controlled by the server.

Using this setup, the Network Identity Spawner manages instantiation and synchronization, simplifying networked spawning for both player-controlled and server-managed objects.

Object Assignment Flexibility

  • Local Player: Can be assigned either a prefab or scene object that will be controlled by the local player
  • Identities to Spawn: Accepts both prefabs and scene objects that need network synchronization
  • Each object must have a NetworkIdentity component attached
  • This component is designed for rapid prototyping only and should not be used in production environments

Manual Instantiationโš“๏ธ

If you want to instantiate a networked object manually in a straightforward way, here are some examples to guide you. Manual instantiation provides you with more control over when and where the networked object appears in the scene, ideal for scenarios where specific logic or conditions dictate object creation.

Follow these examples to quickly and easily get started with manual instantiation of networked objects in your project.

First example:

C#
   // Cache to store all spawn-related messages.
   private DataCache m_SpawnCache = new DataCache(CachePresets.ServerNew);

   protected override void OnServerPeerConnected(NetworkPeer peer, Phase phase)
   {
       // Ensure actions occur only after the client is fully connected and authenticated.
       if (phase == Phase.End) // Phase.End: Indicates the client is authenticated and ready for network interactions
       {
           // Retrieve the first registered prefab from the NetworkManagerโ€™s prefab list. 
           // Note: you can also use:
           // var prefab = NetworkManager.GetPrefab("PrefabName");
           NetworkIdentity prefab = NetworkManager.GetPrefab(0);

           // Spawn the prefab for the connected peer and pass the spawn cache
           prefab.Spawn(peer, dataCache: m_SpawnCache);

           // Send the cached spawn data to the connected peer, allowing late-joining players to 
           // receive all relevant spawn information and ensuring they have a consistent game state.
           m_SpawnCache.SendToPeer(peer);
       }
   }

Tip

  • NetworkManager.GetPrefab offers two overloads: one for retrieving a prefab by its name and another for accessing a prefab by its index in the registered prefab list.
  • prefab.Spawn offers two overloads and optional arguments to control the spawn process.

Check de API reference for more details.