Modifying default Inspector GUI creator

I script in C# and recently created a set of scripts that will expose the properties and fields of a class to the inspector. Works like a dream, but it’s a bit annoying to copy paste a new custom script changing out only the class type each and every time. Using properties let’s me limit impossible settings and makes everything a lot cleaner. Really for C# scripting there’s never a reason I’d prefer the default exposure to my own implementation so was wondering if there was a way to override their implementation somehow?

Im not sure you can create a generic implementation of the CustomEditor but you might be able to do something like :

public class MyBaseEditor<T> : Editor where T:MonoBehaviour
{
    T thing;
    ExposeProperties.PropertyField[] properties;
    public override void OnInspectorGUI()
    {
       if (thing == null)
            return;

        ExposeProperties.Expose(properties);
    }

    public void OnEnable()
    {
        thing = target as T;
        properties = ExposeProperties.GetProperties(thing);
    }
}

then implement it each time like:

[CustomEditor(typeof(HealingPotion))]
public class HealingPotionEditor<HealingPotion> : MyBaseEditor {}

?? Untested …

Yes, that’s possible. Have all you classes inherit from a base class, then do this:

[CustomEditor(typeof(MyBaseClass), true)]

The “true” tells Unity to also call the Editor for all child classes.

Sorry for not being clear.

I created a set of Attributes [ExposeProperty],[ExposeField],[ExposeClass]

If you use [ExposeProperty] it will display the a gui element based on the property it is assigned too. Ie. if it’s a string create a textfield, int creates an intfield, so on and so forth. When it does this it uses the get/set method provided by your code. This lets you do some cool things with properties. Example, if you tried to set Health > MaxHealth a check in your property would make Health = MaxHealth, no way to accidently misassign values in your editor because the same checks that are applied to your property code in game are applied to the values passed to the editor. Since in C# you should be using properties for several values anyways this is really useful.

[ExposeField] is simply my methods attribute that will expose a field just like in Unity.

Finally [ExposeClass] is a method that will do [ExposeField],[ExposeProperty] for each public Property or Attribute.

So to implement this I need to create a custom editor for each class like so

[CustomEditor(typeof(SmallPotion))]
public class HealingPotionEditor : Editor
{
    HealingPotion thing;
    ExposeProperties.PropertyField[] properties;
    public override void OnInspectorGUI()
    {
        //CharacterGUILayout(this,character);
        if (thing == null)
            return;

        ExposeProperties.Expose(properties);
    }

    public void OnEnable()
    {
        thing = target as HealingPotion;
        properties = ExposeProperties.GetProperties(thing);
    }
}

The only way this code differentiates itself from each class is replacing HealingPotion with any other class.

So I was wondering if there was a way to override Unity to use a generic version of that code for every class I submit rather than having to copy/paste then refactor it everyt ime.