Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Justinger · Dec 14, 2018 at 08:55 AM · custom-inspectordrawer

Property drawer using a field's method on a button

Hello!

I have made a custom dictionary class. I would like it to have a "validate" button with each Dictionary. This would run a method on demand that checks the dictionary for duplicates, empty entries, etc.

alt text I am getting a null exception because serializedObject.targetObject is referring to my SaveScriptEditor, while my field expects it to be looking at my dictionary class called "floatDictionaryP". I'm sure I am missing the proper way to do this with properties and fieldInfo, but I've been at it for hours and can't figure it out.

     var obj = fieldInfo.GetValue(_dict.serializedObject.targetObject);
                     Debug.Log("Found field info of " + obj); 
                     DictionaryPairBase myDataClass = obj as DictionaryPairBase;
                     if (obj.GetType().IsArray)
                     {
                         var index = Convert.ToInt32(new string(property.propertyPath.Where(c => char.IsDigit(c)).ToArray()));
                         myDataClass = ((DictionaryPairBase[])obj)[index];
                     }
                     myDataClass.CleanEntries();



So I am wondering, what is the proper syntax to refer to my nested property? (the serializedObject.targetObject's floatDictionaryP)
Is this the proper approach for making a validate button that will always appears with my dictionaries?

dictionaryvalidateerror.png (52.9 kB)
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

1 Reply

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

Answer by Bunny83 · Dec 15, 2018 at 11:48 AM

PropertyDrawers are not meant to work with actual objects at all. The whole inspector works only on the abstracted serialized data of your classes. So you don't have direct access to object instances. Also note that "_dict.serializedObject.targetObject" can only refer to the topmost UnityEngine.Object derived type which is usually either a MonoBehaviour or a ScriptableObject.


There are some hacky ways to get access to the class instances you're looking for, however this usually isn't the way to go. With SerializedObject / SerializedProperty you can actually inspect several instances at once since Unity has added multi object editing. So when writing property drawers you should always keep the hasMultipleDifferentValues field in mind.


If you want to keep using a propertydrawer first you should make sure you handle multi editing propertly. Since editing a dictionary from two different object at once doesn't really work unless they are the same you probably want to not view your UI when there are multiple different values.


With a propertydrawer you generally have two options:

  • manually use reflection (like in the hacky post i've linked above) and figure out the actual object instance this serialized property belongs to

  • Implement the validation method inside your propertydrawer and work through the SerializedProperties.

The second solution is probably the better one that has a higher chance to be compatible with future Unity versions.

Comment
Add comment · Show 1 · 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 Justinger · Dec 15, 2018 at 08:40 PM 0
Share

Thank you so much for your answer. It is very clear and detailed. I have a better idea of property drawer's limitations and usage now.

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

159 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn off or hide varibles from inspector 1 Answer

Simple Custom editor using Handles.PositionHandle does nothing. Please help! 0 Answers

EditorGUI PrefixLabel for multiple property fields. 1 Answer

Custom editor is not applying the settings. 1 Answer

How is the Avatar Mask creation in the Inspector made? 1 Answer


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