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.IsVisible)
         {
             return;
         }

         GUI.skin.label.wordWrap = true;
         //GUI.skin.button.richText = true; // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
         //GUILayout.Button("lala"); // as richText, html tags could be in text


         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
         {
             if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
             {
                 // focus on input -> submit it
                 GuiSendsMsg();
                 return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
             }
             else
             {
                 // assign focus to input
                 GUI.FocusControl("ChatInput");
             }
         }

         GUI.SetNextControlName("");
         GUILayout.BeginArea(this.GuiRect);

         GUILayout.FlexibleSpace();

         if (this.chatClient.State != ChatState.ConnectedToFrontEnd)
         {
             GUILayout.Label("Not in chat yet.");
         }
         else
         {
             List channels = new List(this.chatClient.PublicChannels.Keys); // this could be cached
             int countOfPublicChannels = channels.Count;
             channels.AddRange(this.chatClient.PrivateChannels.Keys);

             if (channels.Count > 0)
             {
                 int previouslySelectedChannelIndex = this.selectedChannelIndex;
                 int channelIndex = channels.IndexOf(this.selectedChannelName);
                 this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;

                 this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
                 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);

                 this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
                 this.selectedChannelName = channels[this.selectedChannelIndex];

                 if (this.selectedChannelIndex != previouslySelectedChannelIndex)
                 {
                     // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
                     this.scrollPos.y = float.MaxValue;
                     if (this.doingPrivateChat)
                     {
                         string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
                         this.userIdInput = pieces[1];
                     }
                 }

                 GUILayout.Label(ChatGui.WelcomeText);

                 if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
                 {
                     for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
                     {
                         string sender = this.selectedChannel.Senders[i];
                         object message = this.selectedChannel.Messages[i];
                         GUILayout.Label(string.Format("{0}: {1}", sender, message));
                     }
                 }

                 GUILayout.EndScrollView();
             }
         }


         GUILayout.BeginHorizontal();
         if (doingPrivateChat)
         {
             GUILayout.Label("to:", GUILayout.ExpandWidth(false));
             GUI.SetNextControlName("WhisperTo");
             this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
             string focussed = GUI.GetNameOfFocusedControl();
             if (focussed.Equals("WhisperTo"))
             {
                 if (this.userIdInput.Equals("username"))
                 {
                     this.userIdInput = "";
                 }
             }
             else if (string.IsNullOrEmpty(this.userIdInput))
             {
                 this.userIdInput = "username";
             }

         }
         GUI.SetNextControlName("ChatInput");
         inputLine = GUILayout.TextField(inputLine);
         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
         {
             GuiSendsMsg();
         }
         GUILayout.EndHorizontal();
         GUILayout.EndArea();
     }