UnityEngine.Networking.NetworkIdentity.RebuildObservers C# (CSharp) Method

RebuildObservers() public method

This causes the set of players that can see this object to be rebuild. The OnRebuildObservers callback function will be invoked on each NetworkBehaviour.

public RebuildObservers ( bool initialize ) : void
initialize bool True if this is the first time.
return void
        public void RebuildObservers(bool initialize)
        {
            if (this.m_Observers != null)
            {
                bool flag = false;
                bool flag2 = false;
                HashSet<NetworkConnection> observers = new HashSet<NetworkConnection>();
                HashSet<NetworkConnection> set2 = new HashSet<NetworkConnection>(this.m_Observers);
                for (int i = 0; i < this.m_NetworkBehaviours.Length; i++)
                {
                    NetworkBehaviour behaviour = this.m_NetworkBehaviours[i];
                    flag2 |= behaviour.OnRebuildObservers(observers, initialize);
                }
                if (!flag2)
                {
                    if (initialize)
                    {
                        for (int j = 0; j < NetworkServer.connections.Count; j++)
                        {
                            NetworkConnection conn = NetworkServer.connections[j];
                            if ((conn != null) && conn.isReady)
                            {
                                this.AddObserver(conn);
                            }
                        }
                        for (int k = 0; k < NetworkServer.localConnections.Count; k++)
                        {
                            NetworkConnection connection2 = NetworkServer.localConnections[k];
                            if ((connection2 != null) && connection2.isReady)
                            {
                                this.AddObserver(connection2);
                            }
                        }
                    }
                }
                else
                {
                    foreach (NetworkConnection connection3 in observers)
                    {
                        if (connection3 != null)
                        {
                            if (!connection3.isReady)
                            {
                                if (LogFilter.logWarn)
                                {
                                    Debug.LogWarning(string.Concat(new object[] { "Observer is not ready for ", base.gameObject, " ", connection3 }));
                                }
                            }
                            else if (initialize || !set2.Contains(connection3))
                            {
                                connection3.AddToVisList(this);
                                if (LogFilter.logDebug)
                                {
                                    Debug.Log(string.Concat(new object[] { "New Observer for ", base.gameObject, " ", connection3 }));
                                }
                                flag = true;
                            }
                        }
                    }
                    foreach (NetworkConnection connection4 in set2)
                    {
                        if (!observers.Contains(connection4))
                        {
                            connection4.RemoveFromVisList(this, false);
                            if (LogFilter.logDebug)
                            {
                                Debug.Log(string.Concat(new object[] { "Removed Observer for ", base.gameObject, " ", connection4 }));
                            }
                            flag = true;
                        }
                    }
                    if (initialize)
                    {
                        for (int m = 0; m < NetworkServer.localConnections.Count; m++)
                        {
                            if (!observers.Contains(NetworkServer.localConnections[m]))
                            {
                                this.OnSetLocalVisibility(false);
                            }
                        }
                    }
                    if (flag)
                    {
                        this.m_Observers = new List<NetworkConnection>(observers);
                        this.m_ObserverConnections.Clear();
                        for (int n = 0; n < this.m_Observers.Count; n++)
                        {
                            this.m_ObserverConnections.Add(this.m_Observers[n].connectionId);
                        }
                    }
                }
            }
        }

Usage Example

コード例 #1
0
 private bool SetupLocalPlayerForConnection(NetworkConnection conn, NetworkIdentity uv, PlayerController newPlayerController)
 {
     if (LogFilter.logDev)
     Debug.Log((object) ("NetworkServer SetupLocalPlayerForConnection netID:" + (object) uv.netId));
       ULocalConnectionToClient connectionToClient = conn as ULocalConnectionToClient;
       if (connectionToClient == null)
     return false;
       if (LogFilter.logDev)
     Debug.Log((object) "NetworkServer AddPlayer handling ULocalConnectionToClient");
       if (uv.netId.IsEmpty())
     uv.OnStartServer();
       uv.RebuildObservers(true);
       this.SendSpawnMessage(uv, (NetworkConnection) null);
       connectionToClient.localClient.AddLocalPlayer(newPlayerController);
       uv.SetLocalPlayer(newPlayerController.playerControllerId);
       return true;
 }