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 override void OnInspectorGUI()
     {
         // Get the renderer from the target object
         var renderer = (target as SortingLayerExposed).gameObject.GetComponent();

         // If there is no renderer, we can't do anything
         if (!renderer)
         {
             return;
         }

         // Expose the sorting layer name
         //string newSortingLayerName = EditorGUILayout.TextField("Sorting Layer", renderer.sortingLayerName);
         //if (newSortingLayerName != renderer.sortingLayerName)
         //{
         // Undo.RecordObject(renderer, "Edit Sorting Layer Name");
         // renderer.sortingLayerName = newSortingLayerName;
         // EditorUtility.SetDirty(renderer);
         //}

         // Expose the sorting layer ID
         //int newSortingLayerId = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID);
         //if (newSortingLayerId != renderer.sortingLayerID)
         //{
         // Undo.RecordObject(renderer, "Edit Sorting Layer ID");
         // renderer.sortingLayerID = newSortingLayerId;
         // EditorUtility.SetDirty(renderer);
         //}

         // Seanba: Use a popup that is populated with the acceptable sorting layers for the renderer
         // Also allow the player to bring up the Tag/Layers inspector if they choose so
         string[] sortLayerNames = GetSortingLayerNames();
         //int[] sortLayerIds = GetSortingLayerUniqueIDs();
         //{
         // StringBuilder builder = new StringBuilder("Sorting Layers = ");
         // for (int i = 0; i < sortLayerNames.Length; ++i)
         // {
         // builder.AppendFormat("({0} = {1},{2}) ", i, sortLayerIds[i], sortLayerNames[i]);
         // }
         // Debug.Log(builder.ToString());
         //}

         int sortLayerSelection = GetSortingLayerIndex(renderer, sortLayerNames);

         GUIContent[] sortingLayerContexts = GetSortingLayerContexts();
         int newSortingLayerIndex = EditorGUILayout.Popup(new GUIContent("Sorting Layer"), sortLayerSelection, sortingLayerContexts);
         if (newSortingLayerIndex == sortingLayerContexts.Length - 1)
         {
             EditorApplication.ExecuteMenuItem("Edit/Project Settings/Tags and Layers");
         }
         else if (newSortingLayerIndex != sortLayerSelection)
         {
             //int newSortingLayerId = sortLayerIds[newSortingLayerIndex];
             string newSortingLayerName = sortLayerNames[newSortingLayerIndex];

             Undo.RecordObject(renderer, "Edit Sorting Layer ID");
             renderer.sortingLayerName = newSortingLayerName;
             //renderer.sortingLayerID = newSortingLayerId;

             EditorUtility.SetDirty(renderer);
         }

         // Expose the manual sorting order within a sort layer
         int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", renderer.sortingOrder);
         if (newSortingLayerOrder != renderer.sortingOrder)
         {
             Undo.RecordObject(renderer, "Edit Sorting Order");
             renderer.sortingOrder = newSortingLayerOrder;
             EditorUtility.SetDirty(renderer);
         }
     }