Going









How do I use Going
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
1411     public void OnStatusChanged(StatusCode statusCode)
1412     {
1413         if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
1414             Debug.Log(string.Format("OnStatusChanged: {0}", statusCode.ToString()));
1415
1416         switch (statusCode)
1417         {
1418             case StatusCode.Connect:
1419                 if (this.State == global::PeerState.ConnectingToNameServer)
1420                 {
1421                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
1422                         Debug.Log("Connected to NameServer.");
1423
1424                     this.server = ServerConnection.NameServer;
1425                     if (this.CustomAuthenticationValues != null)
1426                     {
1427                         this.CustomAuthenticationValues.Secret = null; // when connecting to NameServer, invalidate any auth values
1428                     }
1429                 }
1430
1431                 if (this.State == global::PeerState.ConnectingToGameserver)
1432                 {
1433                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
1434                         Debug.Log("Connected to gameserver.");
1435
1436                     this.server = ServerConnection.GameServer;
1437                     this.State = global::PeerState.ConnectedToGameserver;
1438                 }
1439
1440                 if (this.State == global::PeerState.ConnectingToMasterserver)
1441                 {
1442                     if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
1443                         Debug.Log("Connected to masterserver.");
1444
1445                     this.server = ServerConnection.MasterServer;
1446                     this.State = global::PeerState.ConnectedToMaster;
1447
1448                     if (this.IsInitialConnect)
1449                     {
1450                         this.IsInitialConnect = false; // after handling potential initial-connect issues with special messages, we are now sure we can reach a server
1451                         SendMonoMessage(PhotonNetworkingMessage.OnConnectedToPhoton);
1452                     }
1453                 }
1454
1455                 this.EstablishEncryption(); // always enable encryption
1456
1457                 if (this.IsAuthorizeSecretAvailable)
1458                 {
1459                     // if we have a token we don't have to wait for encryption (it is encrypted anyways, so encryption is just optional later on)
1460                     this.didAuthenticate = this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString());
1461                     if (this.didAuthenticate)
1462                     {
1463                         this.State = global::PeerState.Authenticating;
1464                     }
1465                 }
1466                 break;
1467
1468             case StatusCode.EncryptionEstablished:
1469                 // on nameserver, the "process" is stopped here, so the developer/game can either get regions or authenticate with a specific region
1470                 if (this.server == ServerConnection.NameServer)
1471                 {
1472                     this.State = global::PeerState.ConnectedToNameServer;
1473
1474                     if (!this.didAuthenticate && this.CloudRegion == CloudRegionCode.none)
1475                     {
1476                         // this client is not setup to connect to a default region. find out which regions there are!
1477                         this.OpGetRegions(this.mAppId);
1478                     }
1479                 }
1480
1481                 // we might need to authenticate automatically now, so the client can do anything at all
1482                 if (!this.didAuthenticate && (!this.IsUsingNameServer || this.CloudRegion != CloudRegionCode.none))
1483                 {
1484                     // once encryption is availble, the client should send one (secure) authenticate. it includes the AppId (which identifies your app on the Photon Cloud)
1485                     this.didAuthenticate = this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString());
1486                     if (this.didAuthenticate)
1487                     {
1488                         this.State = global::PeerState.Authenticating;
1489                     }
1490                 }
1491                 break;
1492
1493             case StatusCode.EncryptionFailedToEstablish:
1494                 Debug.LogError("Encryption wasn't established: " + statusCode + ". Going to authenticate anyways.");
1495                 this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, this.CloudRegion.ToString()); // TODO: check if there are alternatives
1496                 break;
1497
1498             case StatusCode.Disconnect:
1499                 this.didAuthenticate = false;
1500                 this.isFetchingFriends = false;
1501                 if (server == ServerConnection.GameServer) this.LeftRoomCleanup();
1502                 if (server == ServerConnection.MasterServer) this.LeftLobbyCleanup();
1503
1504                 if (this.State == global::PeerState.DisconnectingFromMasterserver)
1505                 {
1506                     if (this.Connect(this.mGameserver, ServerConnection.GameServer))
1507                     {
1508                         this.State = global::PeerState.ConnectingToGameserver;
1509                     }
1510                 }
1511                 else if (this.State == global::PeerState.DisconnectingFromGameserver || this.State == global::PeerState.DisconnectingFromNameServer)
1512                 {
1513                     if (this.Connect(this.MasterServerAddress, ServerConnection.MasterServer))
1514                     {
1515                         this.State = global::PeerState.ConnectingToMasterserver;
1516                     }
1517                 }
1518                 else
1519                 {
1520                     if (this.CustomAuthenticationValues != null)
1521                     {
1522                         this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
1523                     }
1524
1525                     this.State = global::PeerState.PeerCreated; // if we set another state here, we could keep clients from connecting in OnDisconnectedFromPhoton right here.
1526                     SendMonoMessage(PhotonNetworkingMessage.OnDisconnectedFromPhoton);
1527                 }
1528                 break;
1529
1530             case StatusCode.SecurityExceptionOnConnect:
1531             case StatusCode.ExceptionOnConnect:
1532                 this.State = global::PeerState.PeerCreated;
1533                 if (this.CustomAuthenticationValues != null)
1534                 {
1535                     this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
1536                 }
1537
1538                 DisconnectCause cause = (DisconnectCause)statusCode;
1539                 SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
1540                 break;
1541
1542             case StatusCode.Exception:
1543                 if (this.IsInitialConnect)
1544                 {
1545                     Debug.LogError("Exception while connecting to: " + this.ServerAddress + ". Check if the server is available.");
1546                     if (this.ServerAddress == null || this.ServerAddress.StartsWith("127.0.0.1"))
1547                     {
1548                         Debug.LogWarning("The server address is 127.0.0.1 (localhost): Make sure the server is running on this machine. Android and iOS emulators have their own localhost.");
1549                         if (this.ServerAddress == this.mGameserver)
1550                         {
1551                             Debug.LogWarning("This might be a misconfiguration in the game server config. You need to edit it to a (public) address.");
1552                         }
1553                     }
1554
1555                     this.State = global::PeerState.PeerCreated;
1556                     cause = (DisconnectCause)statusCode;
1557                     SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
1558                 }
1559                 else
1560                 {
1561                     this.State = global::PeerState.PeerCreated;
1562
1563                     cause = (DisconnectCause)statusCode;
1564                     SendMonoMessage(PhotonNetworkingMessage.OnConnectionFail, cause);
1565                 }
1566
1567                 this.Disconnect();
1568                 break;
1569
1570             case StatusCode.TimeoutDisconnect:
1571             case StatusCode.ExceptionOnReceive:
1572             case StatusCode.DisconnectByServer:
1573             case StatusCode.DisconnectByServerLogic:
1574             case StatusCode.DisconnectByServerUserLimit:
1575                 if (this.IsInitialConnect)
1576                 {
1577                     Debug.LogWarning(statusCode + " while connecting to: " + this.ServerAddress + ". Check if the server is available.");
1578
1579                     cause = (DisconnectCause)statusCode;
1580                     SendMonoMessage(PhotonNetworkingMessage.OnFailedToConnectToPhoton, cause);
1581                 }
1582                 else
1583                 {
1584                     cause = (DisconnectCause)statusCode;
1585                     SendMonoMessage(PhotonNetworkingMessage.OnConnectionFail, cause);
1586                 }
1587                 if (this.CustomAuthenticationValues != null)
1588                 {
1589                     this.CustomAuthenticationValues.Secret = null; // invalidate any custom auth secrets
1590                 }
1591
1592                 this.Disconnect();
1593                 break;
1594
1595             case StatusCode.SendError:
1596                 // this.mListener.clientErrorReturn(statusCode);
1597                 break;
1598
1599             case StatusCode.QueueOutgoingReliableWarning:
1600             case StatusCode.QueueOutgoingUnreliableWarning:
1601             case StatusCode.QueueOutgoingAcksWarning:
1602             case StatusCode.QueueSentWarning:
1603                 // this.mListener.warningReturn(statusCode);
1604                 break;
1605
1606             case StatusCode.QueueIncomingReliableWarning:
1607             case StatusCode.QueueIncomingUnreliableWarning:
1608                 Debug.Log(statusCode + ". This client buffers many incoming messages. This is OK temporarily. With lots of these warnings, check if you send too much or execute messages too slow. " + (PhotonNetwork.isMessageQueueRunning? "":"Your isMessageQueueRunning is false. This can cause the issue temporarily.") );
1609                 break;
1610
1611             // // TCP "routing" is an option of Photon that's not currently needed (or supported) by PUN
1612             //case StatusCode.TcpRouterResponseOk:
1613             // break;
1614             //case StatusCode.TcpRouterResponseEndpointUnknown:
1615             //case StatusCode.TcpRouterResponseNodeIdUnknown:
1616             //case StatusCode.TcpRouterResponseNodeNotReady:
1617
1618             // this.DebugReturn(DebugLevel.ERROR, "Unexpected router response: " + statusCode);
1619             // break;
1620
1621             default:
1622
1623                 // this.mListener.serverErrorReturn(statusCode.value());
1624                 Debug.LogError("Received unknown status code: " + statusCode);
1625                 break;
1626         }
1627
1628         this.externalListener.OnStatusChanged(statusCode);
1629     }
File name: NetworkingPeer.cs Copy
1962     public void ExecuteRPC(Hashtable rpcData, PhotonPlayer sender)
1963     {
1964         if (rpcData == null || !rpcData.ContainsKey((byte)0))
1965         {
1966             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
1967             return;
1968         }
1969
1970         // ts: updated with "flat" event data
1971         int netViewID = (int)rpcData[(byte)0]; // LIMITS PHOTONVIEWS&PLAYERS
1972         int otherSidePrefix = 0; // by default, the prefix is 0 (and this is not being sent)
1973         if (rpcData.ContainsKey((byte)1))
1974         {
1975             otherSidePrefix = (short)rpcData[(byte)1];
1976         }
1977
1978         string inMethodName;
1979         if (rpcData.ContainsKey((byte)5))
1980         {
1981             int rpcIndex = (byte)rpcData[(byte)5]; // LIMITS RPC COUNT
1982             if (rpcIndex > PhotonNetwork.PhotonServerSettings.RpcList.Count - 1)
1983             {
1984                 Debug.LogError("Could not find RPC with index: " + rpcIndex + ". Going to ignore! Check PhotonServerSettings.RpcList");
1985                 return;
1986             }
1987             else
1988             {
1989                 inMethodName = PhotonNetwork.PhotonServerSettings.RpcList[rpcIndex];
1990             }
1991         }
1992         else
1993         {
1994             inMethodName = (string)rpcData[(byte)3];
1995         }
1996
1997         object[] inMethodParameters = null;
1998         if (rpcData.ContainsKey((byte)4))
1999         {
2000             inMethodParameters = (object[])rpcData[(byte)4];
2001         }
2002
2003         if (inMethodParameters == null)
2004         {
2005             inMethodParameters = new object[0];
2006         }
2007
2008         PhotonView photonNetview = this.GetPhotonView(netViewID);
2009         if (photonNetview == null)
2010         {
2011             int viewOwnerId = netViewID/PhotonNetwork.MAX_VIEW_IDS;
2012             bool owningPv = (viewOwnerId == this.mLocalActor.ID);
2013             bool ownerSent = (viewOwnerId == sender.ID);
2014
2015             if (owningPv)
2016             {
2017                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! View was/is ours." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID);
2018             }
2019             else
2020             {
2021                 Debug.LogWarning("Received RPC \"" + inMethodName + "\" for viewID " + netViewID + " but this PhotonView does not exist! Was remote PV." + (ownerSent ? " Owner called." : " Remote called.") + " By: " + sender.ID + " Maybe GO was destroyed but RPC not cleaned up.");
2022             }
2023             return;
2024         }
2025
2026         if (photonNetview.prefix != otherSidePrefix)
2027         {
2028             Debug.LogError(
2029                 "Received RPC \"" + inMethodName + "\" on viewID " + netViewID + " with a prefix of " + otherSidePrefix
2030                 + ", our prefix is " + photonNetview.prefix + ". The RPC has been ignored.");
2031             return;
2032         }
2033
2034         // Get method name
2035         if (inMethodName == string.Empty)
2036         {
2037             Debug.LogError("Malformed RPC; this should never occur. Content: " + SupportClass.DictionaryToString(rpcData));
2038             return;
2039         }
2040
2041         if (PhotonNetwork.logLevel >= PhotonLogLevel.Full)
2042             Debug.Log("Received RPC: " + inMethodName);
2043
2044
2045         // SetReceiving filtering
2046         if (photonNetview.group != 0 && !allowedReceivingGroups.Contains(photonNetview.group))
2047         {
2048             return; // Ignore group
2049         }
2050
2051         Type[] argTypes = new Type[0];
2052         if (inMethodParameters.Length > 0)
2053         {
2054             argTypes = new Type[inMethodParameters.Length];
2055             int i = 0;
2056             for (int index = 0; index < inMethodParameters.Length; index++)
2057             {
2058                 object objX = inMethodParameters[index];
2059                 if (objX == null)
2060                 {
2061                     argTypes[i] = null;
2062                 }
2063                 else
2064                 {
2065                     argTypes[i] = objX.GetType();
2066                 }
2067
2068                 i++;
2069             }
2070         }
2071
2072         int receivers = 0;
2073         int foundMethods = 0;
2074         MonoBehaviour[] mbComponents = photonNetview.GetComponents(); // NOTE: we could possibly also cache MonoBehaviours per view?!
2075         for (int componentsIndex = 0; componentsIndex < mbComponents.Length; componentsIndex++)
2076         {
2077             MonoBehaviour monob = mbComponents[componentsIndex];
2078             if (monob == null)
2079             {
2080                 Debug.LogError("ERROR You have missing MonoBehaviours on your gameobjects!");
2081                 continue;
2082             }
2083
2084             Type type = monob.GetType();
2085
2086             // Get [RPC] methods from cache
2087             List cachedRPCMethods = null;
2088             if (this.monoRPCMethodsCache.ContainsKey(type))
2089             {
2090                 cachedRPCMethods = this.monoRPCMethodsCache[type];
2091             }
2092
2093             if (cachedRPCMethods == null)
2094             {
2095                 List entries = SupportClass.GetMethods(type, typeof(RPC));
2096
2097                 this.monoRPCMethodsCache[type] = entries;
2098                 cachedRPCMethods = entries;
2099             }
2100
2101             if (cachedRPCMethods == null)
2102             {
2103                 continue;
2104             }
2105
2106             // Check cache for valid methodname+arguments
2107             for (int index = 0; index < cachedRPCMethods.Count; index++)
2108             {
2109                 MethodInfo mInfo = cachedRPCMethods[index];
2110                 if (mInfo.Name == inMethodName)
2111                 {
2112                     foundMethods++;
2113                     ParameterInfo[] pArray = mInfo.GetParameters();
2114                     if (pArray.Length == argTypes.Length)
2115                     {
2116                         // Normal, PhotonNetworkMessage left out
2117                         if (this.CheckTypeMatch(pArray, argTypes))
2118                         {
2119                             receivers++;
2120                             object result = mInfo.Invoke((object)monob, inMethodParameters);
2121                             if (mInfo.ReturnType == typeof(IEnumerator))
2122                             {
2123                                 monob.StartCoroutine((IEnumerator)result);
2124                             }
2125                         }
2126                     }
2127                     else if ((pArray.Length - 1) == argTypes.Length)
2128                     {
2129                         // Check for PhotonNetworkMessage being the last
2130                         if (this.CheckTypeMatch(pArray, argTypes))
2131                         {
2132                             if (pArray[pArray.Length - 1].ParameterType == typeof(PhotonMessageInfo))
2133                             {
2134                                 receivers++;
2135
2136                                 int sendTime = (int)rpcData[(byte)2];
2137                                 object[] deParamsWithInfo = new object[inMethodParameters.Length + 1];
2138                                 inMethodParameters.CopyTo(deParamsWithInfo, 0);
2139                                 deParamsWithInfo[deParamsWithInfo.Length - 1] = new PhotonMessageInfo(sender, sendTime, photonNetview);
2140
2141                                 object result = mInfo.Invoke((object)monob, deParamsWithInfo);
2142                                 if (mInfo.ReturnType == typeof(IEnumerator))
2143                                 {
2144                                     monob.StartCoroutine((IEnumerator)result);
2145                                 }
2146                             }
2147                         }
2148                     }
2149                     else if (pArray.Length == 1 && pArray[0].ParameterType.IsArray)
2150                     {
2151                         receivers++;
2152                         object result = mInfo.Invoke((object)monob, new object[] { inMethodParameters });
2153                         if (mInfo.ReturnType == typeof(IEnumerator))
2154                         {
2155                             monob.StartCoroutine((IEnumerator)result);
2156                         }
2157                     }
2158                 }
2159             }
2160         }
2161
2162         // Error handling
2163         if (receivers != 1)
2164         {
2165             string argsString = string.Empty;
2166             for (int index = 0; index < argTypes.Length; index++)
2167             {
2168                 Type ty = argTypes[index];
2169                 if (argsString != string.Empty)
2170                 {
2171                     argsString += ", ";
2172                 }
2173
2174                 if (ty == null)
2175                 {
2176                     argsString += "null";
2177                 }
2178                 else
2179                 {
2180                     argsString += ty.Name;
2181                 }
2182             }
2183
2184             if (receivers == 0)
2185             {
2186                 if (foundMethods == 0)
2187                 {
2188                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" marked with the [RPC](C#) or @RPC(JS) property! Args: " + argsString);
2189                 }
2190                 else
2191                 {
2192                     Debug.LogError("PhotonView with ID " + netViewID + " has no method \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString);
2193                 }
2194             }
2195             else
2196             {
2197                 Debug.LogError("PhotonView with ID " + netViewID + " has " + receivers + " methods \"" + inMethodName + "\" that takes " + argTypes.Length + " argument(s): " + argsString + ". Should be just one?");
2198             }
2199         }
2200     }
File name: NetworkingPeer.cs Copy
3657     protected internal void SetLevelInPropsIfSynced(object levelId)
3658     {
3659         if (!PhotonNetwork.automaticallySyncScene || !PhotonNetwork.isMasterClient || PhotonNetwork.room == null)
3660         {
3661             return;
3662         }
3663         if (levelId == null)
3664         {
3665             Debug.LogError("Parameter levelId can't be null!");
3666             return;
3667         }
3668
3669         // check if "current level" is already set in props
3670         if (PhotonNetwork.room.customProperties.ContainsKey(NetworkingPeer.CurrentSceneProperty))
3671         {
3672             object levelIdInProps = PhotonNetwork.room.customProperties[NetworkingPeer.CurrentSceneProperty];
3673             if (levelIdInProps is int && Application.loadedLevel == (int)levelIdInProps)
3674             {
3675                 return;
3676             }
3677             if (levelIdInProps is string && Application.loadedLevelName.Equals((string)levelIdInProps))
3678             {
3679                 return;
3680             }
3681         }
3682
3683         // current level is not yet in props, so this client has to set it
3684         Hashtable setScene = new Hashtable();
3685         if (levelId is int) setScene[NetworkingPeer.CurrentSceneProperty] = (int)levelId;
3686         else if (levelId is string) setScene[NetworkingPeer.CurrentSceneProperty] = (string)levelId;
3687         else Debug.LogError("Parameter levelId must be int or string!");
3688
3689         PhotonNetwork.room.SetCustomProperties(setScene);
3690         this.SendOutgoingCommands(); // send immediately! because: in most cases the client will begin to load and not send for a while
3691     }
File name: PhotonHandler.cs Copy
59     protected void Update()
60     {
61         if (PhotonNetwork.networkingPeer == null)
62         {
63             Debug.LogError("NetworkPeer broke!");
64             return;
65         }
66
67         if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated || PhotonNetwork.connectionStateDetailed == PeerState.Disconnected || PhotonNetwork.offlineMode)
68         {
69             return;
70         }
71
72         // the messageQueue might be paused. in that case a thread will send acknowledgements only. nothing else to do here.
73         if (!PhotonNetwork.isMessageQueueRunning)
74         {
75             return;
76         }
77
78         bool doDispatch = true;
79         while (PhotonNetwork.isMessageQueueRunning && doDispatch)
80         {
81             // DispatchIncomingCommands() returns true of it found any command to dispatch (event, result or state change)
82             UnityEngine.Profiling.Profiler.BeginSample("DispatchIncomingCommands");
83             doDispatch = PhotonNetwork.networkingPeer.DispatchIncomingCommands();
84             UnityEngine.Profiling.Profiler.EndSample();
85         }
86
87         int currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000); // avoiding Environment.TickCount, which could be negative on long-running platforms
88         if (PhotonNetwork.isMessageQueueRunning && currentMsSinceStart > this.nextSendTickCountOnSerialize)
89         {
90             PhotonNetwork.networkingPeer.RunViewUpdate();
91             this.nextSendTickCountOnSerialize = currentMsSinceStart + this.updateIntervalOnSerialize;
92             this.nextSendTickCount = 0; // immediately send when synchronization code was running
93         }
94
95         currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000);
96         if (currentMsSinceStart > this.nextSendTickCount)
97         {
98             bool doSend = true;
99             while (PhotonNetwork.isMessageQueueRunning && doSend)
100             {
101                 // Send all outgoing commands
102                 UnityEngine.Profiling.Profiler.BeginSample("SendOutgoingCommands");
103                 doSend = PhotonNetwork.networkingPeer.SendOutgoingCommands();
104                 UnityEngine.Profiling.Profiler.EndSample();
105             }
106
107             this.nextSendTickCount = currentMsSinceStart + this.updateInterval;
108         }
109     }
File name: PhotonLagSimulationGui.cs Copy
57     private void NetSimWindow(int windowId)
58     {
59         GUILayout.Label(string.Format("Rtt:{0,4} +/-{1,3}", this.Peer.RoundTripTime, this.Peer.RoundTripTimeVariance));
60
61         bool simEnabled = this.Peer.IsSimulationEnabled;
62         bool newSimEnabled = GUILayout.Toggle(simEnabled, "Simulate");
63         if (newSimEnabled != simEnabled)
64         {
65             this.Peer.IsSimulationEnabled = newSimEnabled;
66         }
67
68         float inOutLag = this.Peer.NetworkSimulationSettings.IncomingLag;
69         GUILayout.Label("Lag " + inOutLag);
70         inOutLag = GUILayout.HorizontalSlider(inOutLag, 0, 500);
71
72         this.Peer.NetworkSimulationSettings.IncomingLag = (int)inOutLag;
73         this.Peer.NetworkSimulationSettings.OutgoingLag = (int)inOutLag;
74
75         float inOutJitter = this.Peer.NetworkSimulationSettings.IncomingJitter;
76         GUILayout.Label("Jit " + inOutJitter);
77         inOutJitter = GUILayout.HorizontalSlider(inOutJitter, 0, 100);
78
79         this.Peer.NetworkSimulationSettings.IncomingJitter = (int)inOutJitter;
80         this.Peer.NetworkSimulationSettings.OutgoingJitter = (int)inOutJitter;
81
82         float loss = this.Peer.NetworkSimulationSettings.IncomingLossPercentage;
83         GUILayout.Label("Loss " + loss);
84         loss = GUILayout.HorizontalSlider(loss, 0, 10);
85
86         this.Peer.NetworkSimulationSettings.IncomingLossPercentage = (int)loss;
87         this.Peer.NetworkSimulationSettings.OutgoingLossPercentage = (int)loss;
88
89         // if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
90         if (GUI.changed)
91         {
92             this.WindowRect.height = 100;
93         }
94
95         GUI.DragWindow();
96     }
File name: PhotonNetwork.cs Copy
2231     public static void SendOutgoingCommands()
2232     {
2233         if (!VerifyCanUseNetwork())
2234         {
2235             return;
2236         }
2237
2238         while (networkingPeer.SendOutgoingCommands())
2239         {
2240         }
2241     }
File name: PhotonStatsGui.cs Copy
77     public void TrafficStatsWindow(int windowID)
78     {
79         bool statsToLog = false;
80         TrafficStatsGameLevel gls = PhotonNetwork.networkingPeer.TrafficStatsGameLevel;
81         long elapsedMs = PhotonNetwork.networkingPeer.TrafficStatsElapsedMs / 1000;
82         if (elapsedMs == 0)
83         {
84             elapsedMs = 1;
85         }
86
87         GUILayout.BeginHorizontal();
88         this.buttonsOn = GUILayout.Toggle(this.buttonsOn, "buttons");
89         this.healthStatsVisible = GUILayout.Toggle(this.healthStatsVisible, "health");
90         this.trafficStatsOn = GUILayout.Toggle(this.trafficStatsOn, "traffic");
91         GUILayout.EndHorizontal();
92
93         string total = string.Format("Out|In|Sum:\t{0,4} | {1,4} | {2,4}", gls.TotalOutgoingMessageCount, gls.TotalIncomingMessageCount, gls.TotalMessageCount);
94         string elapsedTime = string.Format("{0}sec average:", elapsedMs);
95         string average = string.Format("Out|In|Sum:\t{0,4} | {1,4} | {2,4}", gls.TotalOutgoingMessageCount / elapsedMs, gls.TotalIncomingMessageCount / elapsedMs, gls.TotalMessageCount / elapsedMs);
96         GUILayout.Label(total);
97         GUILayout.Label(elapsedTime);
98         GUILayout.Label(average);
99
100         if (this.buttonsOn)
101         {
102             GUILayout.BeginHorizontal();
103             this.statsOn = GUILayout.Toggle(this.statsOn, "stats on");
104             if (GUILayout.Button("Reset"))
105             {
106                 PhotonNetwork.networkingPeer.TrafficStatsReset();
107                 PhotonNetwork.networkingPeer.TrafficStatsEnabled = true;
108             }
109             statsToLog = GUILayout.Button("To Log");
110             GUILayout.EndHorizontal();
111         }
112
113         string trafficStatsIn = string.Empty;
114         string trafficStatsOut = string.Empty;
115         if (this.trafficStatsOn)
116         {
117             trafficStatsIn = "Incoming: " + PhotonNetwork.networkingPeer.TrafficStatsIncoming.ToString();
118             trafficStatsOut = "Outgoing: " + PhotonNetwork.networkingPeer.TrafficStatsOutgoing.ToString();
119             GUILayout.Label(trafficStatsIn);
120             GUILayout.Label(trafficStatsOut);
121         }
122
123         string healthStats = string.Empty;
124         if (this.healthStatsVisible)
125         {
126             healthStats = string.Format(
127                 "ping: {6}[+/-{7}]ms\nlongest delta between\nsend: {0,4}ms disp: {1,4}ms\nlongest time for:\nev({3}):{2,3}ms op({5}):{4,3}ms",
128                 gls.LongestDeltaBetweenSending,
129                 gls.LongestDeltaBetweenDispatching,
130                 gls.LongestEventCallback,
131                 gls.LongestEventCallbackCode,
132                 gls.LongestOpResponseCallback,
133                 gls.LongestOpResponseCallbackOpCode,
134                 PhotonNetwork.networkingPeer.RoundTripTime,
135                 PhotonNetwork.networkingPeer.RoundTripTimeVariance);
136             GUILayout.Label(healthStats);
137         }
138
139         if (statsToLog)
140         {
141             string complete = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", total, elapsedTime, average, trafficStatsIn, trafficStatsOut, healthStats);
142             Debug.Log(complete);
143         }
144
145         // if anything was clicked, the height of this window is likely changed. reduce it to be layouted again next frame
146         if (GUI.changed)
147         {
148             this.statsRect.height = 100;
149         }
150
151         GUI.DragWindow();
152     }

Download file with original file name:Going

Going 142 lượt xem

Gõ tìm kiếm nhanh...