Network prefabs provide a way to create a prefab on either the Client or the Server with only a single
ushort identifier. This works by giving a prefab the
NetworkPrefab component, then on both the Client and Server these prefabs must be loaded in the same order. The Server will assign a unique
ushort identifier to each one based on this order, and the identifiers are then automatically sent to each Client so that the prefab can be located there, too.
Network prefabs only provide a way to identify prefabs on both the Client and the Server, they cannot be used to actually instantiate a prefab across the network. For this, you must use Entities which are a core concept of Zapnet. Entities are automatically spawned for players and have their state synchronized.
If you can't instantiate network prefabs remotely, why would you ever load prefabs this way instead of using an entity?
Entities should be used for any object in your game that you want to be synchronized. They have Synchronized Variables, and States, and even Subsystems so they are a complex but powerful feature of Zapnet.
However, if you just wanted to instantiate a simple effect such as an explosion whenever something happens, you may want to instantiate an explosion prefab. Network prefabs provide a way of giving prefabs a tiny identifier that both the Client and the Server recognize.
This means that you can simply just send this tiny network prefab identifier across the network and create a prefab that has no other overhead such as having its position and rotation synchronized.
NetworkPrefab component automatically assigns itself a unique identifier. This unique identifier can be edited to be anything you want, but it must be unique.
Do not worry about performance when it comes to transmitting these identifiers across the network, because the string is not actually sent, but a single
For your network prefab to be used in any meaningful way, you must load them on both the Server and the Client. To do so, you can use any of the following two methods and you should call one of them as soon as your game scene has loaded or before:
These methods must be called after you have started a Server or a Client and not before!
// Load all network prefabs in the provided directory. The directory must // live inside a Resources folder in your Unity project. Zapnet.Prefab.LoadAll(string directory);
// Load all network prefabs in the provided array. Make sure the array passed in // contains prefabs in the same order for both the Client and Server. Zapnet.Prefab.LoadAll(NetworkPrefab prefabs);
For Entities which have a
NetworkPrefab component attached to them, you only need to specify the unique name of the network prefab on the server to create one.
If you're not using an entity, and just want to instantiate a prefab over the network, you can use the following methods:
Grab the unique
ushort identifier for the prefab and send this identifier using an Event to the Server or the Client depending on where you would like to instantiate it.
var shortId = Zapnet.Prefab.GetId(uniqueName);
Read the unique
ushort identifier from the received Event, find the network prefab with that identifier, and then instantiate it like any normal Unity prefab.
var prefab = Zapnet.Prefab.Find(shortId); GameObject.Instantiate(prefab);