Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
7
Question by Jake-L · Dec 30, 2011 at 09:31 AM · editorlistserializedpropertycustomeditorserializedobject

Editor: How to do PropertyField for List elements?

Hi there,

I finally got a array handling with serialized properties working, but I need the same for generic lists. It comes down to to the question of "How to access List.Count and List[dataindex] when using FindProperty()" ???

Here's my working array code so far:

 void ArrayGUI(SerializedObject obj,string name)
     {
         int no = obj.FindProperty(name + ".Array.size").intValue;
         EditorGUI.indentLevel = 3;
         int c = EditorGUILayout.IntField("Size", no);
         if (c != no)
             obj.FindProperty(name + ".Array.size").intValue = c;
 
         for (int i=0;i<no;i++) {
             var prop = obj.FindProperty(string.Format("{0}.Array.data[{1}]", name, i));
             EditorGUILayout.PropertyField(prop);
         }
     }


Thanks in advance

Jake

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 hawken · Sep 13, 2015 at 09:38 AM 0
Share

trying the above solution with a fixed array of 4 labels for the editor (ins$$anonymous$$d of lement0 etc) but I get an error:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 listVisibility = true;
 
 [CustomEditor(typeof(buttonImage))]
 public class buttonStateEditor : Editor {
 
     public string StatusNames = new String{
         "inactive",
         "live",
         "selected",
         "pressed",
     };
 
     public override void OnInspectorGUI() {
         serializedObject.Update();
         EditorGUIUtility.LookLikeInspector();
         ListIterator(StatusNames, ref listVisibility);
         serializedObject.Apply$$anonymous$$odifiedProperties();
     }
 
     public void ListIterator(string propertyPath, ref bool visible) {
         SerializedProperty listProperty = serializedObject.FindProperty(propertyPath);
         visible = EditorGUILayout.Foldout(visible, listProperty.name);
         if (visible) {
             EditorGUI.indentLevel++;
             for (int i = 0; i < listProperty.arraySize; i++) {
                 SerializedProperty elementProperty = listProperty.GetArrayElementAtIndex(i);
                 Rect drawZone = GUILayoutUtility.GetRect(0f, 16f);
                 bool showChildren = EditorGUI.PropertyField(drawZone, elementProperty); 
             }
             EditorGUI.indentLevel--;
         }
     }
 
 }

3 Replies

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

Answer by steinbitglis · Feb 24, 2012 at 02:04 PM

DrawDefaultInpector() does something like this:

 override def OnInspectorGUI():
     serializedObject.Update()
 
     EditorGUIUtility.LookLikeInspector()
 
     myIterator = serializedObject.FindProperty("myArrayField")
     while true:
         myRect = GUILayoutUtility.GetRect(0f, 16f)
         showChildren = EditorGUI.PropertyField(myRect, myIterator)
         break unless myIterator.NextVisible(showChildren)
 
     serializedObject.ApplyModifiedProperties()

Update:

These days it's probably better to follow Luna4Dev1's advice:

In newer versions of Unity, the PropertyField now has an IncludeChildren flag

Comment
Add comment · Show 5 · 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 Jake-L · Feb 24, 2012 at 02:44 PM 0
Share

Thanks a lot for answering this. I don't have the time to test this in the near future, but I'll assume this will do the trick, so I'll flag this answer as correct.

Thanks mate!

avatar image termway · Jun 28, 2012 at 09:53 AM 0
Share

Thanks, very usefull !

I can't find this on Unity Documentation.

avatar image Luna4Dev1 · May 06, 2015 at 08:59 AM 0
Share

It was a bit difficult to find, but it's EditorGUILayout.PropertyField, or EditorGUI.PropertyField

http://docs.unity3d.com/ScriptReference/30_search.html?q=PropertyField

http://docs.unity3d.com/ScriptReference/EditorGUI.PropertyField.html http://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html

avatar image Harinezumi · Dec 11, 2015 at 02:30 AM 0
Share

Thanks for the update about using the includeChildren flag, was exactly what I was looking for! :)

avatar image Glurth · Mar 25, 2016 at 05:15 PM 0
Share

Perhaps this was in 2012, but now(2016) in, the best answer is below by Luna4Dev1. (use showChildren flag in function parameter.)

avatar image
20

Answer by Luna4Dev1 · Feb 13, 2015 at 01:23 PM

Resurrecting this old thread because I always forget the solution and it's the first hit on Google;

In newer versions of Unity, the PropertyField now has an IncludeChildren flag - simply set that to true and your PropertyField will draw arrays like the default inspector.

Comment
Add comment · Show 3 · 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 nicloay · May 06, 2015 at 08:19 AM 0
Share

best answer =). don't know why everyone like first dirty solutions

avatar image steinbitglis · May 06, 2015 at 08:30 AM 2
Share

added a mention in my "first dirty solution", thx

avatar image jinoh · Dec 08, 2018 at 02:08 AM 0
Share

THank you so much!

avatar image
9
Wiki

Answer by termway · Jun 28, 2012 at 10:25 AM

C# version of steinbitglis answered

 public override void OnInspectorGUI()
 {
     serializedObject.Update();
     EditorGUIUtility.LookLikeInspector();
     ListIterator("myArrayField");
     serializedObject.ApplyModifiedProperties();
 }

 public void ListIterator(string listName)
     {
         //List object
         SerializedProperty listIterator = serializedObject.FindProperty(listName);
         Rect drawZone = GUILayoutUtility.GetRect(0f, 16f);
         bool showChildren = EditorGUI.PropertyField(drawZone, listIterator);
         listIterator.NextVisible(showChildren);
     
         //List size
         drawZone = GUILayoutUtility.GetRect(0f, 16f);
         showChildren = EditorGUI.PropertyField(drawZone, listIterator);
         bool toBeContinued = listIterator.NextVisible(showChildren);

         //Elements
         int listElement = 0;
         while (toBeContinued)
         {
             drawZone = GUILayoutUtility.GetRect(0f, 16f);
             showChildren = EditorGUI.PropertyField(drawZone, listIterator);
             toBeContinued = listIterator.NextVisible(showChildren);
             listElement++;
         }

}

Update : I encountered problem with expending and nested list (like Unity crash >_<)

So here a new version which work for me.

  private listVisibility = true;
 
 ...
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         EditorGUIUtility.LookLikeInspector();
         ListIterator("myArrayField", ref listVisibility);
         serializedObject.ApplyModifiedProperties();
     }
 
 
  public void ListIterator(string propertyPath, ref bool visible)
     {
         SerializedProperty listProperty = serializedObject.FindProperty(propertyPath);
         visible = EditorGUILayout.Foldout(visible, listProperty.name);
         if (visible)
         {
             EditorGUI.indentLevel++;
             for (int i = 0; i < listProperty.arraySize; i++)
             {
                 SerializedProperty elementProperty = listProperty.GetArrayElementAtIndex(i);
                 Rect drawZone = GUILayoutUtility.GetRect(0f, 16f);
                 bool showChildren = EditorGUI.PropertyField(drawZone, elementProperty); 
             }
             EditorGUI.indentLevel--;
         }
     }
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

13 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

Related Questions

Type casting SerializedProperty.objectReferenceValue doesn't work? 3 Answers

How to get fields of a custom script through SerializedProperty.FindPropertyRelative? 1 Answer

Null SerializedProperty SerializedObject upon Removal from list 1 Answer

Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer

Override Transform Inspector using SerializedObject and SerializedProperty 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