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:

     public void OnGUI()
     {
         if (this.Skin != null)
         {
             GUI.skin = this.Skin;
         }

         if (!PhotonNetwork.connected)
         {
             if (PhotonNetwork.connecting)
             {
                 GUILayout.Label("Connecting to: " + PhotonNetwork.ServerAddress);
             }
             else
             {
                 GUILayout.Label("Not connected. Check console output. Detailed connection state: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.ServerAddress);
             }

             if (this.connectFailed)
             {
                 GUILayout.Label("Connection failed. Check setup and use Setup Wizard to fix configuration.");
                 GUILayout.Label(String.Format("Server: {0}", new object[] {PhotonNetwork.ServerAddress}));
                 GUILayout.Label("AppId: " + PhotonNetwork.PhotonServerSettings.AppID);

                 if (GUILayout.Button("Try Again", GUILayout.Width(100)))
                 {
                     this.connectFailed = false;
                     PhotonNetwork.ConnectUsingSettings("0.9");
                 }
             }

             return;
         }

         Rect content = new Rect((Screen.width - WidthAndHeight.x)/2, (Screen.height - WidthAndHeight.y)/2, WidthAndHeight.x, WidthAndHeight.y);
         GUI.Box(content,"Join or Create Room");
         GUILayout.BeginArea(content);

         GUILayout.Space(40);

         // Player name
         GUILayout.BeginHorizontal();
         GUILayout.Label("Player name:", GUILayout.Width(150));
         PhotonNetwork.playerName = GUILayout.TextField(PhotonNetwork.playerName);
         GUILayout.Space(158);
         if (GUI.changed)
         {
             // Save name
             PlayerPrefs.SetString("playerName", PhotonNetwork.playerName);
         }
         GUILayout.EndHorizontal();

         GUILayout.Space(15);

         // Join room by title
         GUILayout.BeginHorizontal();
         GUILayout.Label("Roomname:", GUILayout.Width(150));
         this.roomName = GUILayout.TextField(this.roomName);

         if (GUILayout.Button("Create Room", GUILayout.Width(150)))
         {
             PhotonNetwork.CreateRoom(this.roomName, new RoomOptions() { maxPlayers = 10 }, null);
         }

         GUILayout.EndHorizontal();

         // Create a room (fails if exist!)
         GUILayout.BeginHorizontal();
         GUILayout.FlexibleSpace();
         //this.roomName = GUILayout.TextField(this.roomName);
         if (GUILayout.Button("Join Room", GUILayout.Width(150)))
         {
             PhotonNetwork.JoinRoom(this.roomName);
         }

         GUILayout.EndHorizontal();


         if (!string.IsNullOrEmpty(this.ErrorDialog))
         {
             GUILayout.Label(this.ErrorDialog);

             if (timeToClearDialog < Time.time)
             {
                 timeToClearDialog = 0;
                 this.ErrorDialog = "";
             }
         }

         GUILayout.Space(15);

         // Join random room
         GUILayout.BeginHorizontal();

         GUILayout.Label(PhotonNetwork.countOfPlayers + " users are online in " + PhotonNetwork.countOfRooms + " rooms.");
         GUILayout.FlexibleSpace();
         if (GUILayout.Button("Join Random", GUILayout.Width(150)))
         {
             PhotonNetwork.JoinRandomRoom();
         }


         GUILayout.EndHorizontal();

         GUILayout.Space(15);
         if (PhotonNetwork.GetRoomList().Length == 0)
         {
             GUILayout.Label("Currently no games are available.");
             GUILayout.Label("Rooms will be listed here, when they become available.");
         }
         else
         {
             GUILayout.Label(PhotonNetwork.GetRoomList().Length + " rooms available:");

             // Room listing: simply call GetRoomList: no need to fetch/poll whatever!
             this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
             foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList())
             {
                 GUILayout.BeginHorizontal();
                 GUILayout.Label(roomInfo.name + " " + roomInfo.playerCount + "/" + roomInfo.maxPlayers);
                 if (GUILayout.Button("Join", GUILayout.Width(150)))
                 {
                     PhotonNetwork.JoinRoom(roomInfo.name);
                 }

                 GUILayout.EndHorizontal();
             }

             GUILayout.EndScrollView();
         }

         GUILayout.EndArea();
     }