Search Unity

C Sharp Error on play (editor hierarchy script)

Discussion in 'Scripting' started by pachermann, Oct 14, 2017.

  1. pachermann

    pachermann

    Joined:
    Dec 18, 2013
    Posts:
    133
    Hi everyone, i intent to add Hierarchy Icons, because i have a very flat and large Hierarchy with hundreds of objects.
    At the moment i use Tags for it, but that is only temporary till i find another way to do this.
    I get no compiler complains until i go on Play (VR VIVE).

    2 Things i must solve:

    1.) i want to use the conditional statement in order to have this only in editor, but i don't know how to put the whole class into the statement, if i do so it still executes some code on play. ->
    Code (csharp):
    1. #if UNITY_EDITOR .... #endif
    2.) on Play it tells me and error:
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. HierarchyLabels.HierarchyItemCB (Int32 instanceID, Rect selectionRect) (at Assets/_cutomScripts/MyHierarchyIcons.cs:86)
    4.  
    plus:
    Code (csharp):
    1.  
    2. GUI Error: You are pushing more GUIClips than you are popping. Make sure they are balanced)
    3. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
    4.  
    Of coarse if i can stop unity from executing this code on play this is obsolete...

    any ideas or tipps?

    it looks already fine:




    Code (csharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4. using System.Collections.Generic;
    5. //using UnityEngine.UI;
    6.  
    7.  [InitializeOnLoad]
    8.  class HierarchyLabels
    9.  {
    10.     // m_hierarchyLabelTexture
    11.     static Texture2D m_hierarchyLabelTexture;
    12.     static List<int> m_markedhierarchyLabelObjects;
    13.  
    14.     // m_hierarchyLabelLight
    15.     static Texture2D m_hierarchyLabelLightTexture;
    16.     static List<int> m_markedLightLabelObjects;
    17.  
    18.     // m_hierarchyLabelInstance
    19.     static Texture2D m_hierarchyLabelInstanceTexture;
    20.     static List<int> m_markedInstanceLabelObjects;
    21.  
    22.     // this function muste be replaced with a editor input field to upload custom icons (future)
    23.     static HierarchyLabels()
    24.      {
    25.         // Get Icon types from AssetDatabase Resource (folder)
    26.         m_hierarchyLabelTexture = AssetDatabase.LoadAssetAtPath ("Assets/Resources/labels/hierarchy_label--gray.jpg", typeof(Texture2D)) as Texture2D;
    27.         m_hierarchyLabelLightTexture = AssetDatabase.LoadAssetAtPath("Assets/Resources/labels/hierarchy_light_label.jpg", typeof(Texture2D)) as Texture2D;
    28.         m_hierarchyLabelInstanceTexture = AssetDatabase.LoadAssetAtPath("Assets/Resources/labels/hierarchy_instance_label.jpg", typeof(Texture2D)) as Texture2D;
    29.  
    30.         EditorApplication.update += UpdateCB;
    31.         EditorApplication.hierarchyWindowItemOnGUI += HierarchyItemCB;
    32.      }
    33.    
    34.  
    35.     // this function be updated to dynamicly get allicon types that have a tag accociated with
    36.      static void UpdateCB ()
    37.      {
    38.          // collect all GameObject of type GameObject and laod em into array
    39.          GameObject[] go = Object.FindObjectsOfType (typeof(GameObject)) as GameObject[];
    40.  
    41.  
    42.         // hierarchy Labels
    43.         m_markedhierarchyLabelObjects = new List<int> ();
    44.  
    45.         // hierarchy light Objects
    46.         m_markedLightLabelObjects = new List<int>();
    47.  
    48.         // hierarchy Instanced (duplicated) Objects
    49.         m_markedInstanceLabelObjects = new List<int>();
    50.  
    51.         foreach (GameObject g in go)
    52.          {
    53.             // mark all m_markedhierarchyLabelObjects
    54.             if (g.CompareTag("HierarchyLabel"))
    55.             {
    56.                 m_markedhierarchyLabelObjects.Add(g.GetInstanceID());
    57.              }
    58.  
    59.             // mark all m_markedLightLabelObjects
    60.             if (g.CompareTag("LightGameObject"))
    61.             {
    62.                 m_markedLightLabelObjects.Add(g.GetInstanceID());
    63.             }
    64.  
    65.             // mark all m_markedInstanceLabelObjects
    66.             if (g.CompareTag("duplicatedGameObject"))
    67.             {
    68.                 m_markedInstanceLabelObjects.Add(g.GetInstanceID());
    69.             }
    70.         }
    71.  
    72.  
    73.          
    74.     }
    75.  
    76.      static void HierarchyItemCB (int instanceID, Rect selectionRect)
    77.      {
    78.         // place the icons to the right of the list:
    79.  
    80.             Rect r = new Rect (selectionRect);
    81.             r.x = r.width - 40;
    82.             r.width = 80;
    83.  
    84.        // Debug.Log(m_markedhierarchyLabelObjects.Contains(instanceID));
    85.  
    86.         if (m_markedhierarchyLabelObjects.Contains(instanceID))
    87.          {
    88.             GUI.Label(r, m_hierarchyLabelTexture);
    89.             // Draw the texture if it has the label HierarchyLabel
    90.         }
    91.  
    92.         if (m_markedLightLabelObjects.Contains(instanceID))
    93.         {
    94.             GUI.Label(r, m_hierarchyLabelLightTexture);
    95.         }
    96.  
    97.         if (m_markedInstanceLabelObjects.Contains(instanceID))
    98.         {
    99.             GUI.Label(r, m_hierarchyLabelInstanceTexture);
    100.         }
    101.  
    102.  
    103.     }
    104.  
    105. }
    106.  
    107.  
    108.  
     
    Last edited: Oct 14, 2017