Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by marsonmao · Jul 14, 2015 at 06:34 AM · arraypropertydrawerserialize

How to handle this case: elements inside an array has a CustomPropertyDrawer?

The situation is like one that described in this thread

It says

On the plus side, elements inside arrays and lists do work with PropertyDrawers. For example, this will make every float in the array be displayed as a slider from 0 to 10:

Which is great, but what if each element needs it's own variable?

For example I have the following:

 // the class to be custom drawn
 class MyClass
 {}
 
 // this component has a array my custom drawn class
 class MyMono : MonoBehaviour
 {
     public MyClass[] mcs;
 }
 
 // all elements in the array share one instance of property drawer
 [CustomPropertyDrawer(typeof(MyClass))]
 public class MyClassPropertyDrawer : PropertyDrawer
 {
     private int index = 0;
 
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         index = EditorGUI.Popup(position, index, new string[] { "1", "2", "3" }); // **problem here**
     }
 }

The problem is that for each element in mcs, it calls OnGUI() 1 time; since there is only 1 MyClassPropertyDrawer instance, index is being shared; the result is that all popups has the same index (e.g. if you adjust the first popup, all the other popups change to the same index)

I can fix it using a List<int> to store indexes for each element, and implement some check logic to give each element its own index, (see below) but I'd like to know if there is an official way to handle this case?

Thanks.

P.S. The method I use to solve the problem right now, but I think there should be a more general solution.

     [CustomPropertyDrawer(typeof(MyClass))]
     public class MyClassPropertyDrawer : PropertyDrawer
     {
         private List<int> indexes = new List<int>();
     
         public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
         {
             int myIndex = GetMyIndex(property);
             indexes[myIndex] = EditorGUI.Popup(position, indexes[myIndex], new string[] { "1", "2", "3" });
         }
     }

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
0

Answer by HarshadK · Jul 16, 2015 at 06:24 AM

If the index is actually stored in your MyClass then you can simply use the SerializedProperty passed to the OnGUI() method to access the index value using SerializedProperty.intValue.

Something like:

 property.FindPropertyRelative("index").intValue = EditorGUI.Popup(position, property.FindPropertyRelative("index").intValue, new string[] { "1", "2", "3" });

Haven't tested the code but you get the point.

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

2 People are following this question.

avatar image avatar image

Related Questions

How to prevent overlap when using nested arrays in CustomPropertyDrawers? 1 Answer

EditorGUI.IntField always 0 after GameStart 0 Answers

Custom Editor/Custom Property Drawer combo, breaks Enable button 1 Answer

JsonUtility not working with Arrays 2 Answers

How do I assign 3 variables to every GameObject in the array "enemy"? 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