Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cobo3 · Sep 25, 2014 at 06:22 PM · editorpropertydrawerondestroyonenable

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges