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 Sprakle · Jun 25, 2014 at 01:51 AM · editorpropertiespropertydrawer

Caching data for a PropertyDrawer

I'm creating a PropertyDrawer for an object that takes a lot of CPU power to get or set it. Since an instance of a PropertyDrawer is reused for multiple properties, I need a way to cache the data in a dictionary.

In order for this to work, there must be some way to uniquely identity a property. I've tried the following in OnGUI():

  • property.GetHashCode - doesn't work because a new instance of SerializedProperty is created every time

  • property.name - Seemed to work at first, but not in an array. All names become "data"

  • property.CountInProperty() - Crashes Unity with "`m_ByteOffset < m_Data->size ()`"

My code looks like this:

 public class Drawer : PropertyDrawer
     {
         private Dictionary<string, Data> allData = new Dictionary<string, Data>();
 
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             Data data;
             allData.TryGetValue(property.somethingUnique, out data);
 
             if (data == null)
             {
                 data = new Data(property);
                 allData.Add(property.somethingUnique, data);
             }
 
             // do things with data
         }
     }




Comment
Add comment · Show 10
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 vexe · Jun 25, 2014 at 01:55 AM 0
Share

Since an instance of a PropertyDrawer is reused for multiple properties

Where did you get this info? Could you elaborate more on that?

avatar image Sprakle · Jun 25, 2014 at 01:57 AM 0
Share

@vexe

If I call Debug.Log(GetHashCode()) in OnGUI(), the same thing is output no matter how many properties there are.

avatar image vexe · Jun 25, 2014 at 02:03 AM 0
Share

I'm confused here. You said a drawer instance is used on multiple properties. But now you're logging the hash code for your properties, and saying that they're different? - I'm not sure what you mean.

Do you mean that if you get the enumerator for your serializedObject, iterate on all the SerializedProperties you'd get from that, they'll all have the same hash code?

OR

do you mean that if you had something like:

 public class $$anonymous$$B : $$anonymous$$onoBehaviour
 {
   [Att]
   public int intValue;
 
   [Att]
   public float floatValue;
 }

and then in your drawer, you log the hash code for the drawer that's used to draw the [Att] on the intValue and floatValue, they're the same drawer? No way.

avatar image Sprakle · Jun 25, 2014 at 02:07 AM 0
Share

I fixed that comment in an edit. GetHashCode() is being called on the PropertyDrawer, this has nothing to do with SerializedProperty.GetHashCode()

So yea, I meant your second option. It definitely happens, but not all the time. I found the reuse occurs more often for an array of the same type.

avatar image vexe · Jun 25, 2014 at 02:47 AM 0
Share

@stuffses: it seems you're correct. I've seen this dictionary before but didn't look into it with further interest... It seems that they have a dictionary for type/drawers, so yea for a certain type you'll get the same drawer.

alt text

compile.png (24.9 kB)
Show more comments

1 Reply

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

Answer by Sprakle · Jun 25, 2014 at 01:56 AM

Oh, silly me. I can just use the GUIContent label text. That should be unique in almost all cases.

Edit: property.propertyPath works better because the result will be unique even with nesting.

Now I can have infinitely nested reorderable lists!

alt text

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 SureSight · Jul 23, 2016 at 03:26 AM 0
Share

Using a Dictionary keyed by property.propertyPath worked for me.

Can't thank you enough

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

23 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

Related Questions

Using DuplicateCommand and DeleteCommand with Properties 0 Answers

Reflection on class variable values null on editor startup 0 Answers

Mixing custom editors with custom property drawers, possible ? 1 Answer

Is a PropertyDrawer "persistent" ? 1 Answer

Why PropertyDrawers shared same values? 2 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