示例#1
0
    public void OnSpawnMessage(P2PNetworkMessage net_msg, bool take_ownership)
    {
        net_msg.ReadMessage <P2PObjectSpawnMessage>(Replicator.s_ObjectSpawnMessage);
        GameObject gameObject = GuidManager.ResolveGuid(Replicator.s_ObjectSpawnMessage.guid_bytes);

        if (P2PLogFilter.logDev)
        {
            Debug.Log(string.Format("OnSpawnMessage {0} channel: {1} take_ownership: {2}", new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes), net_msg.m_ChannelId, take_ownership));
        }
        if (!gameObject)
        {
            GameObject prefabById = P2PAssetIdCache.Instance.GetPrefabById(Replicator.s_ObjectSpawnMessage.assetId);
            if (prefabById)
            {
                bool activeSelf = prefabById.activeSelf;
                prefabById.SetActive(false);
                gameObject = UnityEngine.Object.Instantiate <GameObject>(prefabById, Replicator.s_ObjectSpawnMessage.position, Quaternion.identity);
                ReplicationComponent component = gameObject.GetComponent <ReplicationComponent>();
                component.ReplOnChangedOwner(net_msg.m_Connection.m_Peer);
                gameObject.GetComponent <GuidComponent>().ForceGuid(Replicator.s_ObjectSpawnMessage.guid_bytes);
                prefabById.SetActive(activeSelf);
                gameObject.SetActive(activeSelf);
                component.ReplOnSpawned();
                component.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true);
                if (take_ownership)
                {
                    component.ReplRequestOwnership();
                    return;
                }
            }
            else if (P2PLogFilter.logError)
            {
                Debug.LogError(string.Format("OnSpawnMessage no asset found {0}", Replicator.s_ObjectSpawnMessage.assetId));
                return;
            }
        }
        else
        {
            if (P2PLogFilter.logWarn)
            {
                Debug.LogWarning(string.Format("OnSpawnMessage found target for supposedly new object to spawn {0} {1}", gameObject.name, new Guid(Replicator.s_ObjectSpawnMessage.guid_bytes)));
            }
            ReplicationComponent replComponentForGameObject = this.GetReplComponentForGameObject(gameObject, true);
            replComponentForGameObject.Deserialize(Replicator.s_ObjectSpawnMessage.payload, true);
            if (take_ownership)
            {
                replComponentForGameObject.ReplRequestOwnership();
            }
        }
    }