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 Malogic · Apr 22, 2013 at 05:00 PM · editoreditor-scriptingpropertydrawercustomeditor

Draw inspector of array elements?

Hello community.

The short version is I wish to draw custom inspectors for array elements within the Unity editor. The long version is this:

I am playing around with PropertyDrawer and CustomEditor classes for improving my inspectors in the editor.

I have created custom .asset files from ScriptableObjects as I want to use the .asset files as configurations files as in this post: http://www.jacobpennock.com/Blog/?p=670

First I create the code for the ScriptableObject:

 /** Base class */
 using UnityEngine;
 using System.Collections;
 
 public abstract class Decorator : ScriptableObject 
 {
 }
 
 /** Derived class (value object) */
 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class DcRigidBody : Decorator
 {
     public bool freezeVertical        = false;
     public bool freezeHorizontal    = false;
     public bool freezeRotation    = false;
     public float mass            = 1.0f;
     public float drag            = 0.0f;
     public float angularDrag        = 0.0f;
     [Compact]
     public Vector3 constantForce    = Vector3.zero;
     public PhysicMaterial material    = null;
 }

I turn the script into an .asset file and name the .asset file "RigidBody (falling)". It shows up in the inspector like this:

Inspector A

alt text

I then create a second ScriptableObject .asset that contains an array of of the first type of assets:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Decorated : ScriptableObject 
 {
     public List<Decorator> decorators;
 }

I name the .asset file "Matter (falling) and it shows up in the inspector like this:

Inspector B

alt text

What I wish to do is to create an custom inspector for the array elements, so the inspector for asset A shows up when I expand the array in asset B. This is a mock-up of what I wish to achieve:

Inspector C (combines inspector A and inspector B - mock-up)

alt text

So far I have tried to create a custom editor for the ScriptableObject that contains the array. It look like this:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(Decorated))]
 public class DecoratedEditor : Editor 
 {
     override public void OnInspectorGUI () 
     {
         Decorated decorated = target as Decorated;
     
         EditorGUIUtility.LookLikeControls();
            DrawDefaultInspector();
     
         foreach( Decorator d in decorated.decorators )
            {
             Debug.Log( d.ToString() );
         }
     }
 }

This draws Inspector A and then output the array elements to the console. What I want to do is call something like DrawDefaultInspector() for each array element and let it all render in the same inspector so i end up with the mocked-up Inspector C, but I can't figure out how to do it?

Any advices?

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 whydoidoit · Apr 22, 2013 at 05:30 PM 0
Share

I seem to remember that there's some doc/thread somewhere that says that PropertyDrawers won't work for array elements - in any case, I found I had to do $$anonymous$$e myself.

1 Reply

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

Answer by Malogic · Apr 24, 2013 at 10:40 AM

Hi.

I came up with a solution using reflection to inspect the array / List<> elements and draw an appropriate GUI. This is my initial (untested version) - screenshot below:

alt text

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using System.Reflection;
 
 [CustomEditor(typeof(Decorated))]
 public class DecoratedEditor : Editor 
 {
     // properties
     Object of = null;
     
     override public void OnInspectorGUI () 
     {
         // DrawDefaultInspector();
 
         // helper vars
         Decorated decorated = target as Decorated;
         List<Decorator> decorators = decorated.decorators;
         
         // create add button
         EditorGUILayout.BeginHorizontal();
         bool addButton = GUILayout.Button("Add", GUILayout.Width(70.0f));
         if(addButton)
         {
             if(of != null)
             {
                 decorators.Add(of as Decorator);
                 of = null;
             }
         }
         of = EditorGUILayout.ObjectField(of, typeof(Decorator), false);
         EditorGUILayout.EndHorizontal();        
         EditorGUILayout.Separator();
         
         // draw inspectors for each element
         foreach( Decorator decorator in decorators )
         {
             // title bar for decorator
             EditorGUIUtility.LookLikeControls();
             EditorGUILayout.Separator();
             EditorGUILayout.BeginHorizontal();
 
             // create remove button
             bool removeButton = GUILayout.Button("Remove", GUILayout.Width(70.0f));
             if(removeButton)
             {
                 decorators.Remove(decorator);
                 return;
             }
             
             // object selector
             object df = EditorGUILayout.ObjectField(decorator, typeof(Decorator), false);
             if(df != decorator)
             {
                 int index = decorators.IndexOf(decorator);
                 decorators[index] = df as Decorator;
             }
             
             EditorGUILayout.EndHorizontal();
             EditorGUIUtility.LookLikeInspector();
 
             // parse fields using reflection
             FieldInfo[] info = decorator.GetType().GetFields();
             
             foreach(FieldInfo field in info )
             {
                 // handle bool
                 if(field.FieldType == typeof(System.Boolean))
                 {
                     bool b = (bool)field.GetValue(decorator);
                     bool bf = EditorGUILayout.Toggle(field.Name, b);
                     field.SetValue(decorator, bf);
                 }
                 
                 // handle float
                 if(field.FieldType == typeof(System.Single))
                 {
                     float f = (float)field.GetValue(decorator);
                     float ff = EditorGUILayout.FloatField(field.Name, f);
                     field.SetValue(decorator, ff);
                 }
                 
                 // handle Vector3
                 if(field.FieldType == typeof(UnityEngine.Vector3))
                 {
                     Vector3 v = (Vector3)field.GetValue(decorator);
                     Vector3 vf = EditorGUILayout.Vector3Field(field.Name, v);
                     field.SetValue(decorator, vf);
                 }
             }
             
             // store changes
             if(GUI.changed)
             {
                 EditorUtility.SetDirty(decorator);
             }
             
         }
     }
 }


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

12 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

Related Questions

Why PropertyDrawers shared same values? 2 Answers

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

Why does my custom property drawer's DragAndDrop add items twice? 2 Answers

'CustomEditor' is not a attribute class 1 Answer

PropertyDrawer let's dissapear my INT 0 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