Search Unity

Finding editor class for component

Discussion in 'Scripting' started by BrainMelter, Aug 19, 2014.

  1. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    Custom component editors can be defined with the UnityEditor.CustomEditor attribute. Is there any way to look up this custom editor class given the original component's class?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This came up recently, and I believe the conclusion was that no, they could not.
     
  3. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    As far as I know you can't. Not at runtime anyway, as you can't have references to classes in the editor assembly from the runtime assembly. Not sure if it works from within the editor assembly itself though. In that case, you could try to find all classes derriving from the Editor class, and then checking the attributes for each of them, and test against the class you are looking for.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    If you want to find the Editor from your MonoBehaviour, then no, you cannot.

    If you want to find it from another editor class;

    Get all the type from all the loaded assembly.
    Get all take those that derive from Editor, and get their "CustomEditor" attributes.
    Get the internal FieldInfo for the field named "m_InspectedType". GetValue and compare to the type you're seeking.

    There you go. A reflection job, but can only work from the editor assembly, not from gameplay assembly.
     
  5. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    Cool. Thanks for the help. This is going to be for an editor extension, so the editor assembly restriction isn't a problem. Anyway, this sounds like a good place to start investigating. Hmmm, I wonder if this can also work for Unity's pre-built custom inspectors and not just user-defined ones.
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Works for all of them, Unity's or yours.
     
  7. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    I'm running into some problems with reflection when getting attribute constructor arguments.

    For example:
    Code (csharp):
    1.  
    2. [CustomEditor(typeof(Foo))]
    3. public class FooEditor : Editor {
    4.    ...
    5. }
    6.  
    I'm having trouble looking up 'Foo' in the constructor arguments. The problem stems from CustomAttributeData.AttributeType only being available in .net 4.5, which isn't supported by Unity. Is there any other way to get this information?
     
    Last edited: Aug 22, 2014
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    No idea what you're talking about.
    You should get the internal FieldInfo for the field named "m_InspectedType".
     
  9. BrainMelter

    BrainMelter

    Joined:
    Nov 20, 2012
    Posts:
    572
    I was trying to use CustomAttributeData.GetCustomAttributes to solve the problem, but it only works with .net 4.5.

    Anyway, your method of using the FieldInfo works fine.

    Thank you :)