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. Dismiss Notice

Is there a way to check if a component has a custom editor?

Discussion in 'Editor & General Support' started by StonePaleAle, Feb 15, 2021.

  1. StonePaleAle

    StonePaleAle

    Joined:
    Aug 20, 2012
    Posts:
    28
    Hi All,
    I have an editor window that draws an item GUI for editing.

    I get the editor like so:
    Editor editor = Editor.CreateEditor(m_MyContentObject);

    But to write the GUI for the object, I need to either call editor.OnInspectorGUI(); for a custom editor, or editor.DrawDefaultInspector() if it doesn't have one.

    Example:
    I have two scriptable objects.
    One has a custom editor defined, one does not.
    OnInspectorGUI() only draws the object with the custom editor.

    Is there a way to check from the returned editor if it is custom?
    Or a way to check if the type has a custom editor defined?

    Or a way to check if OnInspectorGUI() draws anything?

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Can you perhaps use reflection to see if the OnInspectorGUI() has been overridden and is different than the base one? If so that might be one heuristic to decide there is a custom editor in place.
     
    StonePaleAle likes this.
  3. StonePaleAle

    StonePaleAle

    Joined:
    Aug 20, 2012
    Posts:
    28
    Thanks for the idea!
    This is what I came up with. Seems to do the trick for what I need.

    Code (CSharp):
    1.  
    2. Editor editor = Editor.CreateEditor(m_ContentObject);
    3. MethodInfo methodInfo = editor.GetType().GetMethods().First(p => p.Name == "OnInspectorGUI");
    4.  
    5. string editorType = editor.GetType().ToString();
    6.  
    7. if (methodInfo != null)
    8. {
    9.     if (editorType.Substring(0, editor.GetType().ToString().IndexOf('.', 0)) == "UnityEditor")
    10.     {
    11.         // Not a custom class
    12.         editor.DrawDefaultInspector();
    13.     }
    14.     else
    15.     {
    16.         // Custom (non-unity) editor class
    17.         if (methodInfo.DeclaringType == editor.GetType())
    18.         {
    19.             // Has override for OnInspectorGUI
    20.             editor.OnInspectorGUI();
    21.         }
    22.         else
    23.         {
    24.             // No override in custom class
    25.             editor.DrawDefaultInspector();
    26.         }
    27.     }
    28. }
    29. else
    30. {
    31.     // Don't think I should ever hit this.
    32.     editor.DrawDefaultInspector();
    33. }
    34.  
    35.  
    36.  
     
    Kurt-Dekker likes this.