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:

     static PhotonNetwork()
     {
         #if UNITY_EDITOR
         if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
         {
             //Debug.Log(string.Format("PhotonNetwork.ctor() Not playing {0} {1}", UnityEditor.EditorApplication.isPlaying, UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode));
             return;
         }

         // This can happen when you recompile a script IN play made
         // This helps to surpress some errors, but will not fix breaking
         PhotonHandler[] photonHandlers = GameObject.FindObjectsOfType(typeof(PhotonHandler)) as PhotonHandler[];
         if (photonHandlers != null && photonHandlers.Length > 0)
         {
             Debug.LogWarning("Unity recompiled. Connection gets closed and replaced. You can connect as 'new' client.");
             foreach (PhotonHandler photonHandler in photonHandlers)
             {
                 //Debug.Log("Handler: " + photonHandler + " photonHandler.gameObject: " + photonHandler.gameObject);
                 photonHandler.gameObject.hideFlags = 0;
                 GameObject.DestroyImmediate(photonHandler.gameObject);
                 Component.DestroyImmediate(photonHandler);
             }
         }
         #endif

         Application.runInBackground = true;

         // Set up a MonoBehaviour to run Photon, and hide it
         GameObject photonGO = new GameObject();
         photonMono = (PhotonHandler)photonGO.AddComponent();
         photonGO.name = "PhotonMono";
         photonGO.hideFlags = HideFlags.HideInHierarchy;


         // Set up the NetworkingPeer and use protocol of PhotonServerSettings
         networkingPeer = new NetworkingPeer(photonMono, string.Empty, PhotonNetwork.PhotonServerSettings.Protocol);


         // Local player
         CustomTypes.Register();
     }