- Home /
Inspector foldout with inheritance
I'm a complete n00b when it comes to editor scripting, and I have something I want to achieve, but not sure if it's possible, nor where to start. I have multiple classes that have multiple levels of inheritance. At each level I am using UnityEvents to provide callbacks in the inspector. Example:
public class Weapon
{
public bool isEquiped = false;
public UnityEvent OnEquipCallback;
public void Equip()
{
OnEquipCallback.Invoke();
}
}
public class Gun:Weapon
{
public UnityEvent OnFireCallback;
public int ammo = 100;
public void Fire()
{
OnFireCallback.Invoke();
}
}
This works great, but when there are a bunch of other public variables, it gets really messy in the inspector.
Is it possible to allow both UnityEvents to appear under one foldout in the inspector?
Can a property drawer achieve this?
Ideally I would only have to had an attribute [Callback] above the UnityEvent variable and an editor script would know to group all [Callback]'s under one foldout.
Here's a visual...
Okay, after more digging I did end up finding a way to achieve this... buy Advanced Inspector. Haha.
https://www.assetstore.unity3d.com/en/#!/content/18025
It allows for this functionality in the exact manner I was looking for. It's as easy as importing the asset into your project and then in your $$anonymous$$onoBehaviour scripts adding...
using AdvancedInspector;
public class Gun:Weapon
{
[Group("Callbacks")]
public UnityEvent OnFireCallback;
}
With absolutely no editor scripting required, every public variable tagged with the same group attribute will be added to a foldout, and it works through inheritance. Brilliant!