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
2
Question by Amimoller · Feb 04, 2014 at 08:42 PM · assetsscriptableobjectreflection

Calling method via reflection on parent asset inside PropertyDrawer

Im trying to make a PropertyDrawer that will call a method on the object when a field is set. I got it working for objects that are in the scene, but it wont work for assets (or at least not ScriptableObjects)

Here is the full code:

 [CustomPropertyDrawer(typeof(CallbackAttribute))]
 public class CallbackDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         EditorGUI.BeginChangeCheck();
         var result = EditorGUI.ObjectField(position, label, property.objectReferenceValue, fieldInfo.FieldType, false);
         if (EditorGUI.EndChangeCheck())
         {
             property.objectReferenceValue = result;
             var method = property.serializedObject.targetObject.GetType().GetMethod((attribute as CallbackAttribute).MethodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
             object obj = property.serializedObject.targetObject;
             method.Invoke(obj, null);
         }
         EditorGUI.EndProperty();
     }
 }


Inside the method that is called the field will not be set. I tried loading the asset manually via the AssetDatabase, but that didn't work either. Is this a bug?

Comment
Add comment · Show 2
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 gfoot · Feb 04, 2014 at 09:21 PM 0
Share

The Property system is holding back the change until your method exits. You could try to bypass that by writing the change directly yourself, but you'd be liable to break things like multi-object editing and undo.

avatar image Amimoller · Feb 05, 2014 at 07:59 PM 0
Share

yes that why it fails, i fixed it as described in the answer, thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Amimoller · Feb 05, 2014 at 08:19 PM

So the solution is to wait a few frames before calling the method. I did this by making a static class and marking it with [InitializeOnLoad] and registering for updates. Then i use this class to hold a queue of callbacks that it will wait a few frames before calling

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to use Scriptable Objects outside editor 2 Answers

Auto-refresh assets after changing scriptable object 0 Answers

Destroying unused child subassets 1 Answer

Scriptable Object not saving changes to a list. 0 Answers

Referencing a ScriptableObject asset programatically 0 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