Search Unity

How to make two custom editors co-exist?

Discussion in 'Immediate Mode GUI (IMGUI)' started by TMPxyz, Dec 15, 2014.

  1. TMPxyz

    TMPxyz

    Joined:
    Jul 20, 2012
    Posts:
    766
    Hi,

    I want to know if there's a method to make two different editor for one component to co-exist in a project.

    To be specific, my extension is using a custom editor on SkinnedMeshRenderer, and there's another extension also having custom editor on SkinnedMeshRenderer.
    In the end, there seems to be only one is active.

    Although I felt it might be not very possible to make them co-exist, but if there is some workaround, that would be great.

    Thanks
     
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hello there,

    Yes this is completely possible but there is a bit of setup for it. Basically (how I did it)

    Make a base class (quick example below)

    Code (CSharp):
    1. public class BaseEditorComponent
    2. {
    3.     public abstract bool IsValidInspector();
    4.  
    5.     public abstract void OnGUI();
    6. }
    Have a manager class

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(Object))]
    5. public class EditorManager()
    6. {
    7.    public List<BaseEditorComponent> components = new List<BaseEditorComponent>();
    8.  
    9.     public void OnEnable()
    10.     {
    11.          //Get a list of all classes that inherit from our BaseEditorComponent.
    12.          Type[] types = (All the types);
    13.  
    14.          //Create an instance for each on and put it in a list.
    15.         components = (new instances of all types);
    16.  
    17.          //Filter out the ones who IsValidInspector function returns false.
    18.          components.Remove( the ones we dont want)
    19.     }
    20.  
    21.     public void OnInspectorGUI()
    22.     {
    23.          for(int i = 0; i < componets.Length; ++i)
    24.          {
    25.              components[i].OnGUI();
    26.          }
    27.       }
    28. }
    Notice that "[CustomEditor(typeof(Object))]"?

    That is the key. This allows you to make an inspector for any type of thing that inherits from UnityEngine.Object (which is pretty much everything)

    This is also sudo code and will not compile in Unity.


    Regards,
     
  3. TMPxyz

    TMPxyz

    Joined:
    Jul 20, 2012
    Posts:
    766
    Hi, BMayne,

    Thanks for your great answer, very inspiring. I learned new things today. :)

    If I didn't misunderstand your code, it seems to require that we must know the target editors beforehand, and make them inherit the BaseEditorComponent.

    This seems to be a good central management pattern, but in my case, I wish to handle extensions made by other devs, that cannot be known beforehand and not inherit the base class.

    Although I could add some routine to make specific treatment for that single extension, it doesn't look like a very scale-able solution.

    Regards,