- Home /
OnEnable/Disable on property drawers
Hello everyone,
I have written a custom inspector for a custom class, and I make use of OnEnable, OnDisable and OnDestroy events to manage some things.
The thing is that now I would like it to not be a MonoBehaviour, in order to make it more versatile and usable. So i dropped the MonoBehaviour inheritance and wrote a custom property drawer for it. So far so good.
The problem comes when I try to do things on OnEnable/Disable/Destroy-like events; which are essential for it to work properly.
Say my custom class is called CC (for which a have a custom property drawer); and there's another Monobehaviour class named MC attached to a gameobject named GO. MC has a public variable of type CC.
Basically, I want to get notified in the property drawer whenever I select or diselect GO, and whenever MC gets destroyed.
I know there are no such events for a property drawer (tried it), so my question is: Is there anyway I could simulate those events from a property drawer? Any delegate or something?
Thanks in advance.
Answer by NTG-Sera · Nov 07, 2019 at 02:37 PM
[CustomPropertyDrawer( typeof( Example ) )]
public class ExamplePropertyDrawer : PropertyDrawer
{
public ExamplePropertyDrawer() : base ()
{
Debug.Log( "ExamplePropertyDrawer Constructor" );
}
~ExamplePropertyDrawer()
{
// Note: This is called by the GC; and not necessarily on inspector selection change.
Debug.Log( "ExamplePropertyDrawer Destructor" );
}
}
This is what I've been using, seems to be working just fine.
However if you need a solution that fires for absolutely every property drawer individually:
private bool initialized = false;
public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
{
if ( !initialized )
Init( property );
}
private void Init( SerializedProperty property )
{
initialized = true;
}
Answer by DESTRUKTORR · Jan 03, 2015 at 05:32 PM
The best method I could think of would be to use a custom MonoBehaviour (with the ExecuteInEditMode annotation) with the sole purpose of checking for and sending that event.
You'd need to somehow tell if and when the inspector was changed by something in the editor (either directly by the user or by some other script, or even the editor itself), and then you'd need to check to see if the script that the inspector was checking had any components in them that had property drawers associated with them.
There's likely a much better way to do this, assuming you reverse engineered the internal workings of the Unity Editor, but I wouldn't suggest that, for a pretty large number of reasons. Not least of which is that it would be horrifically unreadable and hackish.
I wouldn't worry about making an OnEnable method that works like the one in Editor scripts. Frankly, a single if statement checking to see if you've initialized something too expensive to do every frame would be a pretty inexpensive addition to the "OnGUI" or even the "GetPropertyHeight" methods. However, keep in mind that GetPropertyHeight (at least in my experience) is called at least 3 times per OnGUI call, and would be a lot less efficient to be doing checks in.
Answer by fwalkerCirca · Nov 20, 2018 at 06:34 PM
@Cobo3 Is has been a while. But I am dealing with this issue right now. Did you find an elegant solution for this?
Your answer
Follow this Question
Related Questions
Mixing custom editors with custom property drawers, possible ? 1 Answer
Is a PropertyDrawer "persistent" ? 1 Answer
When I Multi-select gameobjects and press 'delete' 'OnDestroy' func is not called? 0 Answers
Why PropertyDrawers shared same values? 2 Answers
OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers