Example #1
0
 internal static bool InvokeUnSpawnHandler(NetworkHash128 assetId, GameObject obj)
 {
     if (s_UnspawnHandlers.ContainsKey(assetId) && s_UnspawnHandlers[assetId] != null)
     {
         UnSpawnDelegate handler = s_UnspawnHandlers[assetId];
         handler(obj);
         return(true);
     }
     return(false);
 }
Example #2
0
        /// <summary>
        /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="assetId">Custom assetId string.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (logger.LogEnabled())
            {
                logger.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[assetId]   = spawnHandler;
            unspawnHandlers[assetId] = unspawnHandler;
        }
 /// <summary>
 /// Invokes the unspawn handler of an asset.
 /// </summary>
 /// <param name="assetIndex">Index of the asset.</param>
 /// <param name="obj">The object.</param>
 /// <returns></returns>
 public bool InvokeUnSpawnHandler(int assetIndex, GameObject obj)
 {
     if (_unspawnHandlers.ContainsKey(assetIndex) && _unspawnHandlers[assetIndex] != null)
     {
         UnSpawnDelegate handler = _unspawnHandlers[assetIndex];
         handler(obj);
         return(true);
     }
     return(false);
 }
Example #4
0
        /// <summary>
        /// This is an advanced spawning function that registers a custom prefabHash with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an prefabHash - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="prefabHash"></param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterSpawnHandler(int prefabHash, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (logger.LogEnabled())
            {
                logger.Log("RegisterSpawnHandler asset '" + prefabHash + "' " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name);
            }

            spawnHandlers[prefabHash]   = spawnHandler;
            unspawnHandlers[prefabHash] = unspawnHandler;
        }
Example #5
0
        /// <summary>
        /// This is an advanced spawning function that registers a custom prefabHash with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an prefabHash - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="prefabHash"></param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterSpawnHandler(int prefabHash, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (logger.LogEnabled())
            {
                var spawnName   = spawnHandler?.Method.Name ?? "<NULL>";
                var unspawnName = unspawnHandler?.Method.Name ?? "<NULL>";
                logger.Log($"RegisterSpawnHandler PrefabHash:'{prefabHash}' Spawn:{spawnName} UnSpawn:{unspawnName}");
            }

            _spawnHandlers[prefabHash]   = spawnHandler;
            _unspawnHandlers[prefabHash] = unspawnHandler;
        }
 internal static void RegisterSpawnHandler(NetworkHash128 assetId, QSpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     if (spawnHandler == null || unspawnHandler == null)
     {
         Debug.LogError($"RegisterSpawnHandler custom spawn function null for {assetId}");
     }
     else
     {
         spawnHandlers[assetId]   = spawnHandler;
         unspawnHandlers[assetId] = unspawnHandler;
     }
 }
Example #7
0
        public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (spawnHandler == null || unspawnHandler == null)
            {
                Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
                return;
            }

            if (LogFilter.Debug) { Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); }

            spawnHandlers[assetId] = spawnHandler;
            unspawnHandlers[assetId] = unspawnHandler;
        }
Example #8
0
        /// <summary>
        /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="assetId">Custom assetId string.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (spawnHandler == null || unspawnHandler == null)
            {
                throw new InvalidOperationException("RegisterSpawnHandler custom spawn function null for " + assetId);
            }

            if (logger.LogEnabled())
            {
                logger.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[assetId]   = spawnHandler;
            unspawnHandlers[assetId] = unspawnHandler;
        }
Example #9
0
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The ClientObjectManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of PrefabHash and prefab references.</para>
        /// </summary>
        /// <param name="identity">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterPrefab(NetworkIdentity identity, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            var prefabHash = identity.PrefabHash;

            if (prefabHash == 0)
            {
                throw new InvalidOperationException("RegisterPrefab game object " + identity.name + " has no " + nameof(identity) + ". Use RegisterSpawnHandler() instead?");
            }

            if (logger.LogEnabled())
            {
                logger.Log("Registering custom prefab '" + identity.name + "' as asset:" + prefabHash + " " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name);
            }

            _spawnHandlers[prefabHash]   = spawnHandler;
            _unspawnHandlers[prefabHash] = unspawnHandler;
        }
        /// <summary>
        /// Registers a spawn handler.
        /// </summary>
        /// <param name="assetIndex">Id of the asset.</param>
        /// <param name="spawnHandler">The spawn handler.</param>
        /// <param name="unspawnHandler">The unspawn handler.</param>
        public void RegisterSpawnHandler(int assetIndex, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (spawnHandler == null || unspawnHandler == null)
            {
                if (TinyNetLogLevel.logError)
                {
                    TinyLogger.LogError("RegisterSpawnHandler custom spawn function null for " + assetIndex);
                }
                return;
            }

            if (TinyNetLogLevel.logDebug)
            {
                TinyLogger.Log("RegisterSpawnHandler asset '" + assetIndex + "' " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name);
            }

            _spawnHandlers[assetIndex]   = spawnHandler;
            _unspawnHandlers[assetIndex] = unspawnHandler;
        }
Example #11
0
        static internal void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (spawnHandler == null || unspawnHandler == null)
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
                }
                return;
            }

            if (LogFilter.logDebug)
            {
                Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            s_SpawnHandlers[assetId]   = spawnHandler;
            s_UnspawnHandlers[assetId] = unspawnHandler;
        }
        public void SpawnDelegate_AddsHandlerToSpawnHandlers()
        {
            int handlerCalled = 0;

            Guid          guid         = Guid.NewGuid();
            SpawnDelegate spawnHandler = new SpawnDelegate((pos, rot) =>
            {
                handlerCalled++;
                return(null);
            });
            UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => {});

            ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler);

            Assert.IsTrue(spawnHandlers.ContainsKey(guid));

            // check spawnHandler above is called
            SpawnHandlerDelegate handler = spawnHandlers[guid];

            handler.Invoke(default);
        /// <summary>
        /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="assetId">Custom assetId string.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public static void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (spawnHandler == null)
            {
                logger.LogError($"Can not Register null SpawnHandler for {assetId}");
                return;
            }

            if (unspawnHandler == null)
            {
                logger.LogError($"Can not Register null UnSpawnHandler for {assetId}");
                return;
            }

            if (assetId == Guid.Empty)
            {
                logger.LogError("Can not Register SpawnHandler for empty Guid");
                return;
            }

            if (spawnHandlers.ContainsKey(assetId) || unspawnHandlers.ContainsKey(assetId))
            {
                logger.LogWarning($"Replacing existing spawnHandlers for {assetId}");
            }

            if (prefabs.ContainsKey(assetId))
            {
                // this is error because SpawnPrefab checks prefabs before handler
                logger.LogError($"assetId '{assetId}' is already used by prefab '{prefabs[assetId].name}'");
            }

            if (logger.LogEnabled())
            {
                logger.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[assetId]   = spawnHandler;
            unspawnHandlers[assetId] = unspawnHandler;
        }
Example #14
0
        public IEnumerator OnDestroySpawnHandlerTest() => UniTask.ToCoroutine(async() =>
        {
            spawnDelegateTestCalled = 0;
            var guid                 = Guid.NewGuid();
            var gameObject           = new GameObject();
            NetworkIdentity identity = gameObject.AddComponent <NetworkIdentity>();
            identity.AssetId         = guid;

            UnSpawnDelegate unspawnDelegate = Substitute.For <UnSpawnDelegate>();

            clientObjectManager.RegisterSpawnHandler(guid, SpawnDelegateTest, unspawnDelegate);
            clientObjectManager.RegisterPrefab(identity, guid);
            serverObjectManager.SendSpawnMessage(identity, connectionToClient);

            await AsyncUtil.WaitUntilWithTimeout(() => spawnDelegateTestCalled != 0);

            clientObjectManager.OnObjectDestroy(new ObjectDestroyMessage
            {
                netId = identity.NetId
            });
            unspawnDelegate.Received().Invoke(Arg.Any <NetworkIdentity>());
        });
Example #15
0
        public IEnumerator OnDestroySpawnHandlerTest() => UniTask.ToCoroutine(async() =>
        {
            spawnDelegateTestCalled = 0;
            int hash                 = Guid.NewGuid().GetHashCode();
            var gameObject           = new GameObject();
            NetworkIdentity identity = gameObject.AddComponent <NetworkIdentity>();
            identity.PrefabHash      = hash;
            identity.NetId           = (uint)Random.Range(0, int.MaxValue);

            UnSpawnDelegate unspawnDelegate = Substitute.For <UnSpawnDelegate>();

            clientObjectManager.RegisterSpawnHandler(hash, SpawnDelegateTest, unspawnDelegate);
            clientObjectManager.RegisterPrefab(identity, hash);
            serverObjectManager.SendSpawnMessage(identity, serverPlayer);

            await AsyncUtil.WaitUntilWithTimeout(() => spawnDelegateTestCalled != 0);

            clientObjectManager.OnObjectDestroy(new ObjectDestroyMessage
            {
                netId = identity.NetId
            });
            unspawnDelegate.Received().Invoke(Arg.Any <NetworkIdentity>());
        });
Example #16
0
        protected void CallRegisterPrefab(GameObject prefab, RegisterPrefabOverload overload)
        {
            SpawnDelegate        spawnHandler         = new SpawnDelegate((x, y) => null);
            SpawnHandlerDelegate spawnHandlerDelegate = new SpawnHandlerDelegate(x => null);
            UnSpawnDelegate      unspawnHandler       = new UnSpawnDelegate(x => {});

            switch (overload)
            {
            case RegisterPrefabOverload.Prefab:
                ClientScene.RegisterPrefab(prefab);
                break;

            case RegisterPrefabOverload.Prefab_NewAssetId:
                ClientScene.RegisterPrefab(prefab, anotherGuid);
                break;

            case RegisterPrefabOverload.Prefab_SpawnDelegate:
                ClientScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnDelegate_NewAssetId:
                ClientScene.RegisterPrefab(prefab, anotherGuid, spawnHandler, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnHandlerDelegate:
                ClientScene.RegisterPrefab(prefab, spawnHandlerDelegate, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnHandlerDelegate_NewAssetId:
                ClientScene.RegisterPrefab(prefab, anotherGuid, spawnHandlerDelegate, unspawnHandler);
                break;

            default:
                Debug.LogError("Overload not found");
                break;
            }
        }
        internal static void RegisterPrefab(GameObject prefab, QSpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            var component = prefab.GetComponent <QNetworkIdentity>();

            if (component == null)
            {
                Debug.LogError($"Could not register '{prefab.name}' since it contains no NetworkIdentity component");
            }
            else if (spawnHandler == null || unspawnHandler == null)
            {
                Debug.LogError($"RegisterPrefab custom spawn function null for {component.AssetId}");
            }
            else if (!component.AssetId.IsValid())
            {
                Debug.LogError($"RegisterPrefab game object {prefab.name} has no prefab. Use RegisterSpawnHandler() instead?");
            }
            else
            {
                spawnHandlers[component.AssetId]   = spawnHandler;
                unspawnHandlers[component.AssetId] = unspawnHandler;
            }
        }
 internal static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
   NetworkIdentity component = prefab.GetComponent<NetworkIdentity>();
   if ((Object) component == (Object) null)
   {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"));
   }
   else if (spawnHandler == null || unspawnHandler == null)
   {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("RegisterPrefab custom spawn function null for " + (object) component.assetId));
   }
   else if (!component.assetId.IsValid())
   {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?"));
   }
   else
   {
     if (LogFilter.logDebug)
       Debug.Log((object) ("Registering custom prefab '" + prefab.name + "' as asset:" + (object) component.assetId + " " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name));
     NetworkScene.s_SpawnHandlers[component.assetId] = spawnHandler;
     NetworkScene.s_UnspawnHandlers[component.assetId] = unspawnHandler;
   }
 }
 /// <summary>
 ///   <para>This is an advanced spawning funciotn that registers a custom assetId with the UNET spawning system.</para>
 /// </summary>
 /// <param name="assetId">Custom assetId string.</param>
 /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
 /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
 public static void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
   NetworkScene.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler);
 }
Example #20
0
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
        /// </summary>
        /// <param name="prefab">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            NetworkIdentity identity = prefab.GetComponent <NetworkIdentity>();

            if (identity == null)
            {
                throw new InvalidOperationException("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
            }

            if (identity.AssetId == Guid.Empty)
            {
                throw new InvalidOperationException("RegisterPrefab game object " + prefab.name + " has no " + nameof(prefab) + ". Use RegisterSpawnHandler() instead?");
            }

            if (logger.LogEnabled())
            {
                logger.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.AssetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[identity.AssetId]   = spawnHandler;
            unspawnHandlers[identity.AssetId] = unspawnHandler;
        }
 internal static void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
   if (spawnHandler == null || unspawnHandler == null)
   {
     if (!LogFilter.logError)
       return;
     Debug.LogError((object) ("RegisterSpawnHandler custom spawn function null for " + (object) assetId));
   }
   else
   {
     if (LogFilter.logDebug)
       Debug.Log((object) ("RegisterSpawnHandler asset '" + (object) assetId + "' " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name));
     NetworkScene.s_SpawnHandlers[assetId] = spawnHandler;
     NetworkScene.s_UnspawnHandlers[assetId] = unspawnHandler;
   }
 }
Example #22
0
 /// <summary>
 /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
 /// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
 /// </summary>
 /// <param name="assetId">Custom assetId string.</param>
 /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
 /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
 public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     RegisterSpawnHandler(assetId, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
 }
Example #23
0
 public static void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     NetworkScene.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler);
 }
Example #24
0
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The ClientObjectManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
        /// </summary>
        /// <param name="identity">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public void RegisterPrefab(NetworkIdentity identity, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (identity.AssetId == Guid.Empty)
            {
                throw new InvalidOperationException("RegisterPrefab game object " + identity.name + " has no " + nameof(identity) + ". Use RegisterSpawnHandler() instead?");
            }

            if (logger.LogEnabled())
            {
                logger.Log("Registering custom prefab '" + identity.name + "' as asset:" + identity.AssetId + " " + spawnHandler.Method.Name + "/" + unspawnHandler.Method.Name);
            }

            spawnHandlers[identity.AssetId]   = spawnHandler;
            unspawnHandlers[identity.AssetId] = unspawnHandler;
        }
Example #25
0
 internal static void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     if ((spawnHandler == null) || (unspawnHandler == null))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
         }
     }
     else
     {
         if (LogFilter.logDebug)
         {
             Debug.Log(string.Concat(new object[] { "RegisterSpawnHandler asset '", assetId, "' ", DotNetCompatibility.GetMethodName(spawnHandler), "/", DotNetCompatibility.GetMethodName(unspawnHandler) }));
         }
         s_SpawnHandlers[assetId] = spawnHandler;
         s_UnspawnHandlers[assetId] = unspawnHandler;
     }
 }
        /// <summary>
        /// This is an advanced spawning function that registers a custom assetId with the UNET spawning system.
        /// <para>This can be used to register custom spawning methods for an assetId - instead of the usual method of registering spawning methods for a prefab. This should be used when no prefab exists for the spawned objects - such as when they are constructed dynamically at runtime from configuration data.</para>
        /// </summary>
        /// <param name="assetId">Custom assetId string.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            // We need this check here because we don't want a null handler in the lambda expression below
            if (spawnHandler == null)
            {
                logger.LogError($"Can not Register null SpawnHandler for {assetId}");
                return;
            }

            RegisterSpawnHandler(assetId, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
        }
Example #27
0
 internal static void RegisterSpawnHandler(NetworkHash128 assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     if (spawnHandler == null || unspawnHandler == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId);
         }
         return;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log(string.Concat(new object[]
         {
             "RegisterSpawnHandler asset '",
             assetId,
             "' ",
             spawnHandler.Method.Name,
             "/",
             unspawnHandler.Method.Name
         }));
     }
     NetworkScene.s_SpawnHandlers[assetId] = spawnHandler;
     NetworkScene.s_UnspawnHandlers[assetId] = unspawnHandler;
 }
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
        /// </summary>
        /// <param name="prefab">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public static void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (prefab == null)
            {
                logger.LogError("Could not register handler for prefab because the prefab was null");
                return;
            }

            NetworkIdentity identity = prefab.GetComponent <NetworkIdentity>();

            if (identity == null)
            {
                logger.LogError("Could not register handler for '" + prefab.name + "' since it contains no NetworkIdentity component");
                return;
            }

            if (identity.sceneId != 0)
            {
                logger.LogError($"Can not Register '{prefab.name}' because it has a sceneId, make sure you are passing in the original prefab and not an instance in the scene.");
                return;
            }

            Guid assetId = identity.assetId;

            if (assetId == Guid.Empty)
            {
                logger.LogError($"Can not Register handler for '{prefab.name}' because it had empty assetid. If this is a scene Object use RegisterSpawnHandler instead");
                return;
            }

            if (spawnHandler == null)
            {
                logger.LogError($"Can not Register null SpawnHandler for {assetId}");
                return;
            }

            if (unspawnHandler == null)
            {
                logger.LogError($"Can not Register null UnSpawnHandler for {assetId}");
                return;
            }

            if (spawnHandlers.ContainsKey(assetId) || unspawnHandlers.ContainsKey(assetId))
            {
                logger.LogWarning($"Replacing existing spawnHandlers for prefab '{prefab.name}' with assetId '{assetId}'");
            }

            if (prefabs.ContainsKey(assetId))
            {
                // this is error because SpawnPrefab checks prefabs before handler
                logger.LogError($"assetId '{assetId}' is already used by prefab '{prefabs[assetId].name}', unregister the prefab first before trying to add handler");
            }

            NetworkIdentity[] identities = prefab.GetComponentsInChildren <NetworkIdentity>();
            if (identities.Length > 1)
            {
                logger.LogWarning($"Prefab '{prefab.name}' has multiple NetworkIdentity components. There should only be one NetworkIdentity on a prefab, and it must be on the root object.");
            }

            if (logger.LogEnabled())
            {
                logger.Log("Registering custom prefab '" + prefab.name + "' as asset:" + assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[assetId]   = spawnHandler;
            unspawnHandlers[assetId] = unspawnHandler;
        }
 /// <summary>
 ///   <para>Registers a prefab with the UNET spawning system.</para>
 /// </summary>
 /// <param name="prefab">A Prefab that will be spawned.</param>
 /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
 /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
 /// <param name="newAssetId">An assetId to be assigned to this prefab. This allows a dynamically created game object to be registered for an already known asset Id.</param>
 public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
   NetworkScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
 }
Example #30
0
 internal static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     NetworkIdentity component = prefab.GetComponent<NetworkIdentity>();
     if (component == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("Could not register '" + prefab.get_name() + "' since it contains no NetworkIdentity component");
         }
         return;
     }
     if (spawnHandler == null || unspawnHandler == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterPrefab custom spawn function null for " + component.assetId);
         }
         return;
     }
     if (!component.assetId.IsValid())
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterPrefab game object " + prefab.get_name() + " has no prefab. Use RegisterSpawnHandler() instead?");
         }
         return;
     }
     if (LogFilter.logDebug)
     {
         Debug.Log(string.Concat(new object[]
         {
             "Registering custom prefab '",
             prefab.get_name(),
             "' as asset:",
             component.assetId,
             " ",
             spawnHandler.Method.Name,
             "/",
             unspawnHandler.Method.Name
         }));
     }
     NetworkScene.s_SpawnHandlers[component.assetId] = spawnHandler;
     NetworkScene.s_UnspawnHandlers[component.assetId] = unspawnHandler;
 }
Example #31
0
 /// <summary>
 /// Registers a prefab with the spawning system.
 /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
 /// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
 /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
 /// </summary>
 /// <param name="prefab">A Prefab that will be spawned.</param>
 /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
 /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
 public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     RegisterPrefab(prefab, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
 }
Example #32
0
 internal static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     NetworkIdentity component = prefab.GetComponent<NetworkIdentity>();
     if (component == null)
     {
         if (LogFilter.logError)
         {
             Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
         }
     }
     else if ((spawnHandler == null) || (unspawnHandler == null))
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterPrefab custom spawn function null for " + component.assetId);
         }
     }
     else if (!component.assetId.IsValid())
     {
         if (LogFilter.logError)
         {
             Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
         }
     }
     else
     {
         if (LogFilter.logDebug)
         {
             Debug.Log(string.Concat(new object[] { "Registering custom prefab '", prefab.name, "' as asset:", component.assetId, " ", DotNetCompatibility.GetMethodName(spawnHandler), "/", DotNetCompatibility.GetMethodName(unspawnHandler) }));
         }
         s_SpawnHandlers[component.assetId] = spawnHandler;
         s_UnspawnHandlers[component.assetId] = unspawnHandler;
     }
 }
Example #33
0
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
        /// </summary>
        /// <param name="prefab">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public static void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            NetworkIdentity identity = prefab.GetComponent <NetworkIdentity>();

            if (identity == null)
            {
                Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
                return;
            }

            if (spawnHandler == null || unspawnHandler == null)
            {
                Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId);
                return;
            }

            if (identity.assetId == Guid.Empty)
            {
                Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
                return;
            }

            if (LogFilter.Debug)
            {
                Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
            }

            spawnHandlers[identity.assetId]   = spawnHandler;
            unspawnHandlers[identity.assetId] = unspawnHandler;
        }
Example #34
0
 public static void RegisterSpawnHandler(Guid assetId, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler) =>
 NetworkClient.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler);
Example #35
0
 public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
 {
     NetworkScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
 }
Example #36
0
 public static void RegisterPrefab(GameObject prefab, SpawnHandlerDelegate spawnHandler, UnSpawnDelegate unspawnHandler) =>
 NetworkClient.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
        /// <summary>
        /// Registers a prefab with the spawning system.
        /// <para>When a NetworkIdentity object is spawned on a server with NetworkServer.SpawnObject(), and the prefab that the object was created from was registered with RegisterPrefab(), the client will use that prefab to instantiate a corresponding client object with the same netId.</para>
        /// <para>The NetworkManager has a list of spawnable prefabs, it uses this function to register those prefabs with the ClientScene.</para>
        /// <para>The set of current spawnable object is available in the ClientScene static member variable ClientScene.prefabs, which is a dictionary of NetworkAssetIds and prefab references.</para>
        /// </summary>
        /// <param name="prefab">A Prefab that will be spawned.</param>
        /// <param name="spawnHandler">A method to use as a custom spawnhandler on clients.</param>
        /// <param name="unspawnHandler">A method to use as a custom un-spawnhandler on clients.</param>
        public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
        {
            if (prefab == null)
            {
                logger.LogError("Could not register handler for prefab because the prefab was null");
                return;
            }

            NetworkIdentity identity = prefab.GetComponent <NetworkIdentity>();

            if (identity == null)
            {
                logger.LogError("Could not register handler for '" + prefab.name + "' since it contains no NetworkIdentity component");
                return;
            }

            if (identity.sceneId != 0)
            {
                logger.LogError($"Can not Register '{prefab.name}' because it has a sceneId, make sure you are passing in the original prefab and not an instance in the scene.");
                return;
            }

            Guid assetId = identity.assetId;

            if (assetId == Guid.Empty)
            {
                logger.LogError($"Can not Register handler for '{prefab.name}' because it had empty assetid. If this is a scene Object use RegisterSpawnHandler instead");
                return;
            }

            // We need this check here because we don't want a null handler in the lambda expression below
            if (spawnHandler == null)
            {
                logger.LogError($"Can not Register null SpawnHandler for {assetId}");
                return;
            }

            RegisterPrefab(prefab, msg => spawnHandler(msg.position, msg.assetId), unspawnHandler);
        }
        protected void CallRegisterPrefab(GameObject prefab, RegisterPrefabOverload overload, UnSpawnDelegate unspawnHandler)
        {
            SpawnDelegate        spawnHandler         = new SpawnDelegate((x, y) => null);
            SpawnHandlerDelegate spawnHandlerDelegate = new SpawnHandlerDelegate(x => null);

            switch (overload)
            {
            case RegisterPrefabOverload.Prefab_SpawnDelegate:
                NetworkClient.RegisterPrefab(prefab, spawnHandler, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnDelegate_NewAssetId:
                NetworkClient.RegisterPrefab(prefab, anotherGuid, spawnHandler, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnHandlerDelegate:
                NetworkClient.RegisterPrefab(prefab, spawnHandlerDelegate, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab_SpawnHandlerDelegate_NewAssetId:
                NetworkClient.RegisterPrefab(prefab, anotherGuid, spawnHandlerDelegate, unspawnHandler);
                break;

            case RegisterPrefabOverload.Prefab:
            case RegisterPrefabOverload.Prefab_NewAssetId:
                Debug.LogError("Overload did not have UnSpawnDelegate parameter");
                break;

            default:
                Debug.LogError("Overload not found");
                break;
            }
        }