Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

GameObject custom editor prevents drawing 3D models preview

Discussion in 'Editor & General Support' started by DaCookie, Nov 1, 2020.

  1. DaCookie

    DaCookie

    Joined:
    Nov 4, 2014
    Posts:
    44
    Hi there!

    I'm trying to make a custom editor for GameObject. It works fine, and I'm glad to share the trick with you. But wait, Unity acts really weird when you're using 3D models with a custom editor for GameObject in your project!
    As an example, I made the following script, which will list all the components on a selected GameObject:

    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(GameObject))]
    7. [CanEditMultipleObjects]
    8. public class GameObjectCustomEditorDemo : Editor
    9. {
    10.     private Editor m_DefaultGameObjectEditor = null;
    11.  
    12.     private void OnEnable()
    13.     {
    14.         // Creates the default inspector view of GameObject using reflection (needed to access to the internal UnityEditor classes)
    15.         m_DefaultGameObjectEditor = CreateEditor(target, Type.GetType("UnityEditor.GameObjectInspector, UnityEditor"));
    16.     }
    17.  
    18.     private void OnDisable()
    19.     {
    20.         if (m_DefaultGameObjectEditor != null)
    21.         {
    22.             // Check if the preview cache property is set
    23.             object previewCache = m_DefaultGameObjectEditor.GetType()
    24.                 .GetField("m_PreviewCache", BindingFlags.Instance | BindingFlags.NonPublic)
    25.                 .GetValue(m_DefaultGameObjectEditor);
    26.  
    27.             // If the preview cache is not defined, call OnEnable() method to initialize the GameObject editor, avoiding exceptions
    28.             if (previewCache == null)
    29.                 m_DefaultGameObjectEditor.GetType().GetMethod("OnEnable").Invoke(m_DefaultGameObjectEditor, null);
    30.  
    31.             DestroyImmediate(m_DefaultGameObjectEditor);
    32.         }
    33.     }
    34.  
    35.     protected override void OnHeaderGUI()
    36.     {
    37.         // Draw the default GameObject editor header
    38.         m_DefaultGameObjectEditor.DrawHeader();
    39.     }
    40.  
    41.     public override void OnInspectorGUI()
    42.     {
    43.         // Draw the default GameObject editor inspector GUI
    44.         m_DefaultGameObjectEditor.OnInspectorGUI();
    45.  
    46.         // Draw a list of all the components on the first selected GameObject
    47.         EditorGUILayout.LabelField("Components on this GameObject:", EditorStyles.boldLabel);
    48.         EditorGUI.indentLevel++;
    49.         Component[] components = (target as GameObject).GetComponents<Component>();
    50.         for (int i = 0; i < components.Length; i++)
    51.         {
    52.             EditorGUILayout.LabelField($"[{i}] {components[i].GetType().Name}");
    53.         }
    54.         EditorGUI.indentLevel--;
    55.         EditorGUILayout.Space();
    56.     }
    57. }
    With this script in your project, you should see the components list in the inspector when you select a GameObject:



    Pretty cool, right? This really works like a charm (and the trick is quite the same for other native components by the way)... But there is bad side effects with 3D models:
    • Their preview are no more visible in the Project window
    • Their preview is not displayed in the Model Importer view (in the Inspector)
    • I can't drag and drop them from my Project view to the Scene view (but dragging them to the Hierarchy still works)




    Unfortunately, these bugs makes the tool unusable for productions. I searched for a solution by tryings things with reflection and using the Unity's C# reference on GitHub, but that was unsuccessful.

    So, I would like to find:
    • why Unity behaves that way when creating a custom GameObject editor?
    • and how can I fix this issue?
    Does someone have at least a track I can explore?
    Thanks in advance for any answer!
    (and sorry to the Unity Technologies guys who will see this post for doing too many experiments with your wonderful game engine!)
     
  2. DaCookie

    DaCookie

    Joined:
    Nov 4, 2014
    Posts:
    44
  3. DaCookie

    DaCookie

    Joined:
    Nov 4, 2014
    Posts:
    44