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 DrawObservedComponentsList()
     {
         GUILayout.Space( 5 );
         SerializedProperty listProperty = serializedObject.FindProperty( "ObservedComponents" );

         if( listProperty == null )
         {
             return;
         }

         float containerElementHeight = 22;
         float containerHeight = listProperty.arraySize * containerElementHeight;

         bool isOpen = PhotonGUI.ContainerHeaderFoldout( "Observed Components (" + GetObservedComponentsCount() + ")", serializedObject.FindProperty( "ObservedComponentsFoldoutOpen" ).boolValue );
         serializedObject.FindProperty( "ObservedComponentsFoldoutOpen" ).boolValue = isOpen;

         if( isOpen == false )
         {
             containerHeight = 0;
         }

         //Texture2D statsIcon = AssetDatabase.LoadAssetAtPath( "Assets/Photon Unity Networking/Editor/PhotonNetwork/PhotonViewStats.png", typeof( Texture2D ) ) as Texture2D;

         Rect containerRect = PhotonGUI.ContainerBody( containerHeight );
         bool wasObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;
         if( isOpen == true )
         {
             for( int i = 0; i < listProperty.arraySize; ++i )
             {
                 Rect elementRect = new Rect( containerRect.xMin, containerRect.yMin + containerElementHeight * i, containerRect.width, containerElementHeight );
                 {
                     Rect texturePosition = new Rect( elementRect.xMin + 6, elementRect.yMin + elementRect.height / 2f - 1, 9, 5 );
                     ReorderableListResources.DrawTexture( texturePosition, ReorderableListResources.texGrabHandle );

                     Rect propertyPosition = new Rect( elementRect.xMin + 20, elementRect.yMin + 3, elementRect.width - 45, 16 );
                     EditorGUI.PropertyField( propertyPosition, listProperty.GetArrayElementAtIndex( i ), new GUIContent() );

                     //Debug.Log( listProperty.GetArrayElementAtIndex( i ).objectReferenceValue.GetType() );
                     //Rect statsPosition = new Rect( propertyPosition.xMax + 7, propertyPosition.yMin, statsIcon.width, statsIcon.height );
                     //ReorderableListResources.DrawTexture( statsPosition, statsIcon );

                     Rect removeButtonRect = new Rect( elementRect.xMax - PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
                                                         elementRect.yMin + 2,
                                                         PhotonGUI.DefaultRemoveButtonStyle.fixedWidth,
                                                         PhotonGUI.DefaultRemoveButtonStyle.fixedHeight );

                     GUI.enabled = listProperty.arraySize > 1;
                     if( GUI.Button( removeButtonRect, new GUIContent( ReorderableListResources.texRemoveButton ), PhotonGUI.DefaultRemoveButtonStyle ) )
                     {
                         listProperty.DeleteArrayElementAtIndex( i );
                     }
                     GUI.enabled = true;

                     if( i < listProperty.arraySize - 1 )
                     {
                         texturePosition = new Rect( elementRect.xMin + 2, elementRect.yMax, elementRect.width - 4, 1 );
                         PhotonGUI.DrawSplitter( texturePosition );
                     }
                 }
             }
         }

         if( PhotonGUI.AddButton() )
         {
             listProperty.InsertArrayElementAtIndex( Mathf.Max( 0, listProperty.arraySize - 1 ) );
         }

         serializedObject.ApplyModifiedProperties();

         bool isObservedComponentsEmpty = m_Target.ObservedComponents.FindAll( item => item != null ).Count == 0;

         if( wasObservedComponentsEmpty == true && isObservedComponentsEmpty == false && m_Target.synchronization == ViewSynchronization.Off )
         {
             m_Target.synchronization = ViewSynchronization.UnreliableOnChange;
             EditorUtility.SetDirty( m_Target );
             serializedObject.Update();
         }

         if( wasObservedComponentsEmpty == false && isObservedComponentsEmpty == true )
         {
             m_Target.synchronization = ViewSynchronization.Off;
             EditorUtility.SetDirty( m_Target );
             serializedObject.Update();
         }

     }