/// <summary>
 /// Unregister a spawnhandler on the client
 /// </summary>
 /// <param name="spawnData">Spawn data.</param>
 protected virtual void UnregisterSpawnHandlerOnClient(SpawnHandlerData spawnData)
 {
     if (spawnData.serverPrefab == null)
     {
         Debug.LogWarning("No serverPrefab set for spawnHandler!");
         return;
     }
     ClientScene.UnregisterSpawnHandler(spawnData.serverPrefab.GetComponent <NetworkIdentity>().assetId);
 }
        /// <summary>
        /// Register a spawn handler on the client
        /// </summary>
        protected virtual void RegisterSpawnHandlerOnClient(SpawnHandlerData spawnData)
        {
            if (spawnData.serverPrefab == null || spawnData.clientSpawnDelegate == null || spawnData.clientUnSpawnDelegate == null)
            {
                Debug.LogWarning("SpawnData is missing needed data (spawn/unspawn delegate or server prefab)");
                return;
            }

            //Create a custom handler for the server prefab, so we can spawn a custom client prefab
            ClientScene.RegisterSpawnHandler(spawnData.serverPrefab.GetComponent <NetworkIdentity>().assetId, spawnData.clientSpawnDelegate, spawnData.clientUnSpawnDelegate);
        }
        /// <summary>
        /// Adds new spawn handler, or overwrites it if the supplied id already exists.
        /// </summary>
        /// <param name="id">Identifier.</param>
        /// <param name="data">Data.</param>
        public virtual void AddSpawnHandler(string id, SpawnHandlerData data)
        {
            if (spawnHandlerData.ContainsKey(id))
            {
                spawnHandlerData[id] = data;
            }
            else
            {
                spawnHandlerData.Add(id, data);
            }

            #if !SERVER
            RegisterSpawnHandlerOnClient(data);
            #endif
        }
Exemplo n.º 4
0
        /// <summary>
        /// Called on server, when a client calls AddPlayer() function
        /// </summary>
        /// <param name="conn">Conn.</param>
        /// <param name="playerControllerId">Player controller identifier.</param>
        /// <param name="extraMessageReader">Extra message reader.</param>
        public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId, NetworkReader extraMessageReader)
        {
            //base.OnServerAddPlayer(conn, playerControllerId, extraMessageReader);

            //Read extra message
            AddPlayerExtraMessage extraMessage = extraMessageReader.ReadMessage <AddPlayerExtraMessage>();

            //Get spawn data
            SpawnHandlerData spawnData = GetSpawnHandlerData(extraMessage.spawnHandlerID);

            //Spawn gameobject on server depending on spawn data
            if (spawnData != null)
            {
                //Instantiate object
                GameObject serverGO = Instantiate(spawnData.serverPrefab);
                //Attach instance to client
                NetworkServer.AddPlayerForConnection(conn, serverGO, playerControllerId);
            }
            else
            {
                Debug.LogError("No spawnData for " + extraMessage.spawnHandlerID);
            }
        }