Beispiel #1
0
        // ------------------------ NetworkScene pass-throughs ---------------------

        internal static string GetStringForAssetId(NetworkHash128 assetId)
        {
            GameObject prefab;

            if (NetworkScene.GetPrefab(assetId, out prefab))
            {
                return(prefab.name);
            }

            SpawnDelegate handler;

            if (NetworkScene.GetSpawnHandler(assetId, out handler))
            {
                return(handler.GetMethodName());
            }

            return("unknown");
        }
Beispiel #2
0
        static void OnSpawnPrefab(NetworkMessage netMsg)
        {
            SpawnPrefabMessage msg = new SpawnPrefabMessage();

            netMsg.ReadMessage(msg);

            if (!msg.assetId.IsValid())
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("OnObjSpawn netId: " + msg.netId + " has invalid asset Id");
                }
                return;
            }
            if (LogFilter.logDebug)
            {
                Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + "]");
            }

            NetworkIdentity localNetworkIdentity;

            if (s_NetworkScene.GetNetworkIdentity(msg.netId, out localNetworkIdentity))
            {
                // this object already exists (was in the scene), just apply the update to existing object
                localNetworkIdentity.Reset();
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, null);
                return;
            }

            GameObject    prefab;
            SpawnDelegate handler;

            if (NetworkScene.GetPrefab(msg.assetId, out prefab))
            {
                var obj = (GameObject)Object.Instantiate(prefab, msg.position, msg.rotation);
                if (LogFilter.logDebug)
                {
                    Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
                }

                localNetworkIdentity = obj.GetComponent <NetworkIdentity>();
                if (localNetworkIdentity == null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
                    }
                    return;
                }
                localNetworkIdentity.Reset();
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, obj);
            }
            // lookup registered factory for type:
            else if (NetworkScene.GetSpawnHandler(msg.assetId, out handler))
            {
                GameObject obj = handler(msg.position, msg.assetId);
                if (obj == null)
                {
                    if (LogFilter.logWarn)
                    {
                        Debug.LogWarning("Client spawn handler for " + msg.assetId + " returned null");
                    }
                    return;
                }
                localNetworkIdentity = obj.GetComponent <NetworkIdentity>();
                if (localNetworkIdentity == null)
                {
                    if (LogFilter.logError)
                    {
                        Debug.LogError("Client object spawned for " + msg.assetId + " does not have a network identity");
                    }
                    return;
                }
                localNetworkIdentity.Reset();
                localNetworkIdentity.SetDynamicAssetId(msg.assetId);
                ApplySpawnPayload(localNetworkIdentity, msg.position, msg.payload, msg.netId, obj);
            }
            else
            {
                if (LogFilter.logError)
                {
                    Debug.LogError("Failed to spawn server object, did you forget to add it to the NetworkManager? assetId=" + msg.assetId + " netId=" + msg.netId);
                }
            }
        }