I want to implement a custom Unity inspector attribute, like usual ones [HideInInspector] or [Range(0, 100f)].
This attribute should hide a field from the inspector if it's inherited from a base type, rather being defined directly in the type of the object we're inspecting.
So, for example, if I have these classes...
public class Enemy : MonoBehaviour {
[SuperField]
public int EnemyHealth;
}
public class GiantMonster : Enemy {
[SuperField]
public int Armor;
}
then the inspector for a base Enemy should show a control for the EnemyHealth variable, but when inspecting a GiantMonster we should not see a control for this variable.
So far I have my attribute:
public class SuperFieldAttribute : PropertyAttribute {}
which has its own PropertyDrawer:
[CustomPropertyDrawer(typeof(SuperFieldAttribute))]
public class SuperFieldDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
// ...
}
}
Inside OnGUI I need to access/find the type where the field we're drawing was declared with the [SuperFieldAttribute].
So if SuperFieldDrawer.OnGUI() is called for the SerializedProperty corresponding to GiantMonster.EnemyHealth, I should be able to determine that the type where EnemyHealth was declared is Enemy and not GiantMonster. (I'll use this to hide the field so it's not drawn)
And if SuperFieldDrawer.OnGUI() is called for the SerializedProperty corresponding to GiantMonster.Armor, I should be able to determine that the type where Armor was declared is GiantMonster itself. (Meaning I should proceed and draw the field)
So far I've found ways to access the type of the component we're inspecting, but not the type where the field was first declared, so that's the missing piece I need to fill in.
It's OK if I use reflection and so on; any method is good, performance isn't a concern.
HideInDerivedapplication specifically. That tells us a lot about the context, what kinds of inputs we can expect (ie.SerializedPropertyrather thanSystem.TypeorFieldInfo), and might help the question catch the eye of other Unity devs who want to implement this feature. :) \$\endgroup\$