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:

     void DrawOldObservedItem()
     {
         EditorGUILayout.BeginHorizontal();

         // Using a lower version then 3.4? Remove the TRUE in the next line to fix an compile error
         string typeOfObserved = string.Empty;
         if( m_Target.observed != null )
         {
             int firstBracketPos = m_Target.observed.ToString().LastIndexOf( '(' );
             if( firstBracketPos > 0 )
             {
                 typeOfObserved = m_Target.observed.ToString().Substring( firstBracketPos );
             }
         }


         Component componenValue = (Component)EditorGUILayout.ObjectField( "Observe: " + typeOfObserved, m_Target.observed, typeof( Component ), true );
         if( m_Target.observed != componenValue )
         {
             if( m_Target.observed == null )
             {
                 m_Target.synchronization = ViewSynchronization.UnreliableOnChange; // if we didn't observe anything yet. use unreliable on change as default
             }
             if( componenValue == null )
             {
                 m_Target.synchronization = ViewSynchronization.Off;
             }

             m_Target.observed = componenValue;
         }

         EditorGUILayout.EndHorizontal();
     }