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
0
Question by v21 · Jul 31, 2013 at 12:07 PM · editoreditorguipropertydrawerserializedproperty

Applying a PropertyDrawer to elements of a List<>

I have set up a PropertyDrawer, and it works for single elements:

 public class ResourceReferenceAttribute : PropertyAttribute
 {
     public ResourceReferenceAttribute()
     {
     }
 }

...

 [CustomPropertyDrawer(typeof(ResourceReferenceAttribute))]
 public class ResourceReferenceDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect pos,
                                 SerializedProperty prop,
                                 GUIContent label)
     {
         Object obj = AssetDatabase.LoadMainAssetAtPath(prop.stringValue);
         Object newObj = EditorGUI.ObjectField(pos, label, obj, typeof(GameObject), false);
         if (obj != newObj)
         {
             prop.stringValue = AssetDatabase.GetAssetPath(newObj);
         }
     }
 }

...

 [ResourceReference] public string healthyString;

Which works as you would expect.

I would also like to apply this to arrays of items, but am having no luck:

 [ResourceReference] public List<string> rootSoundsPaths = new List<string>();

The result of this is that I get the entire array field replaced in the Inspector with an ObjectField, and an error occurs, complaining that there's no prop.stringValue.

While researching this, I came across this post on the forums that suggests that this ought to be possible.

I have tried changing rootSoundsPaths to be of type string[], but no change. It's also possible this is breaking because this is a defined within a nested serialized class, but that seems to be clutching at straws...

Comment
Add comment · Show 1
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 yoyo · Sep 04, 2013 at 06:09 AM 0
Share

Hmm, Rune's example (in that forum post) doesn't work either. If I use this:

 [Range (0, 10)]
 public float[] myFloats;

The property drawer just displays an error, saying Range should only be used with float or int. Looks like array support is missing, broken, or not released yet. (I'm on Unity 4.1.5.)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Jamora · Aug 01, 2013 at 05:53 AM

The reason you got the error at prop.stringvalue is that prop wasn't a string; it was an array. So after a few tweaks and Unity crashes, I managed to get this cobbled together. It's not pretty; all the rects overlap eachother... but it works, at least for the List. Didn't check if it worked for the actual string...

 [CustomPropertyDrawer(typeof(ResourceReferenceAttribute))]
     public class ResourceReferenceDrawer : PropertyDrawer
 {
     List<string> debugList = new List<string>();
     int index = 0;
     public override void OnGUI (Rect pos,
     SerializedProperty prop,
     GUIContent label)
     {
         index = EditorGUI.IntField(pos,"Size:",index);
         
         if(prop.isArray && index > 0){
             while(index > prop.arraySize)
                 prop.InsertArrayElementAtIndex(prop.arraySize);
             while(index < prop.arraySize)
                 prop.DeleteArrayElementAtIndex(prop.arraySize-1);
         }
         
         if(prop.arraySize>0){
             for(int i = 0; i<prop.arraySize;i++){
                 Object obj = AssetDatabase.LoadMainAssetAtPath (prop.GetArrayElementAtIndex(i).stringValue);
                 Object newObj = EditorGUI.ObjectField (pos, label, obj, typeof(GameObject), false);
                 if (obj != newObj) {
                     prop.GetArrayElementAtIndex(i).stringValue = AssetDatabase.GetAssetPath (newObj);
                 }
                 debugList.Add(prop.GetArrayElementAtIndex(i).stringValue);
                 pos.y += 20;
             }
         }
         if(!prop.isArray || prop.arraySize == 0){
             Object newObj = null;
             newObj = EditorGUI.ObjectField (pos, label, newObj, typeof(GameObject), false);
             if (newObj != null) {
                 prop.InsertArrayElementAtIndex(0);
                 prop.GetArrayElementAtIndex(0).stringValue = AssetDatabase.GetAssetPath (newObj);
             }            
         }
         foreach(string s in debugList)
             Debug.Log(s);
         debugList.Clear();
     }
 }
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

17 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

Related Questions

How to show type text in EditorGUI.ObjectField? 1 Answer

Display Custom Inspectors for each class in a List<> 1 Answer

Call a function from CustomPropertyDrawer of Arbitrary class 1 Answer

PropertyDrawer not painted when scrolling upwards 0 Answers

GetPropertyHeight infinite recursion on drawer 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