Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     internal IEnumerator PingAvailableRegionsCoroutine(bool connectToBest)
     {
         BestRegionCodeCurrently = CloudRegionCode.none;
         while (PhotonNetwork.networkingPeer.AvailableRegions == null)
         {
             if (PhotonNetwork.connectionStateDetailed != PeerState.ConnectingToNameServer && PhotonNetwork.connectionStateDetailed != PeerState.ConnectedToNameServer)
             {
                 Debug.LogError("Call ConnectToNameServer to ping available regions.");
                 yield break; // break if we don't connect to the nameserver at all
             }

             Debug.Log("Waiting for AvailableRegions. State: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.Server + " PhotonNetwork.networkingPeer.AvailableRegions " + (PhotonNetwork.networkingPeer.AvailableRegions != null));
             yield return new WaitForSeconds(0.25f); // wait until pinging finished (offline mode won't ping)
         }

         if (PhotonNetwork.networkingPeer.AvailableRegions == null || PhotonNetwork.networkingPeer.AvailableRegions.Count == 0)
         {
             Debug.LogError("No regions available. Are you sure your appid is valid and setup?");
             yield break; // break if we don't get regions at all
         }

         //#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IPHONE)
         //#pragma warning disable 0162 // the library variant defines if we should use PUN's SocketUdp variant (at all)
         //if (PhotonPeer.NoSocket)
         //{
         // if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
         // {
         // Debug.Log("PUN disconnects to re-use native sockets for pining servers and to find the best.");
         // }
         // PhotonNetwork.Disconnect();
         //}
         //#pragma warning restore 0162
         //#endif

         PhotonPingManager pingManager = new PhotonPingManager();
         foreach (Region region in PhotonNetwork.networkingPeer.AvailableRegions)
         {
             SP.StartCoroutine(pingManager.PingSocket(region));
         }

         while (!pingManager.Done)
         {
             yield return new WaitForSeconds(0.1f); // wait until pinging finished (offline mode won't ping)
         }


         Region best = pingManager.BestRegion;
         PhotonHandler.BestRegionCodeCurrently = best.Code;
         PhotonHandler.BestRegionCodeInPreferences = best.Code;

         Debug.Log("Found best region: " + best.Code + " ping: " + best.Ping + ". Calling ConnectToRegionMaster() is: " + connectToBest);


         if (connectToBest)
         {
             PhotonNetwork.networkingPeer.ConnectToRegionMaster(best.Code);
         }
     }