Instead of hard referencing Prefabs in your scripts to instantiate them using Unity's own Instantiate(), you can reference a CoherenceSyncConfig and instantiate your local Prefab instances through our API.
This will utilize the internal INetworkObjectProvider and INetworkObjectInstantiator interfaces to load and instantiate the Prefab in a given networked scene (a scene with a CoherenceBridge component in it).
For instance:
usingCoherence.Toolkit;usingUnityEngine;publicclassCoherenceSyncConfigExample:MonoBehaviour{publicCoherenceSyncConfig prefabSyncConfig;publicCoherenceBridge bridge;privateCoherenceSync instance1;privateCoherenceSync instance2;voidStart() { // Load the prefab and instantiate it in the current scene instance1 =prefabSyncConfig.GetInstance(); // Load the prefab and instantiate it in the specific networked scene for the given CoherenceBridge instance2 =prefabSyncConfig.GetInstance(bridge,Vector3.zero,Quaternion.identity); }privatevoidOnDestroy() { // Instances have to be destroyed via the ReleaseInstance method, so that its destruction is handle by the // internal INetworkObjectInstantiatorprefabSyncConfig.ReleaseInstance(instance1);prefabSyncConfig.ReleaseInstance(instance2); }}
You can also hard reference the Prefab in your script, and use our extensions to instantiate the Prefab easily using the internal INetworkObjectInstantiator interface implementation.
The main difference is that the previous approach doesn't need a Prefab hard reference, and you won't have to change the code if the way that the Prefab is loaded into memory changes (for example, if you go from Resources to load it via Addressables).
usingSystem;usingCoherence.Toolkit;usingUnityEngine;publicclassInstantiatorViaCoherenceSyncComponent:MonoBehaviour{ // Serialized prefab referencepublicCoherenceSync prefab;publicCoherenceBridge bridge;privateCoherenceSync instance1;privateCoherenceSync instance2;voidStart() { // Instantiate it in the current scene instance1 =prefab.GetInstance(); // Instantiate it in the specific networked scene for the given CoherenceBridge instance2 =prefab.GetInstance(bridge,Vector3.zero,Quaternion.identity); }privatevoidOnDestroy() { // Instances have to be destroyed via the ReleaseInstance method, so that its destruction is handle by the // internal INetworkObjectInstantiatorinstance1.ReleaseInstance();instance2.ReleaseInstance(); }}