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
9
Question by tylo · Aug 29, 2010 at 11:23 PM · editorarrayinspectoreditor-scriptingcustom-inspector

How can I recreate the Array Inspector element for a custom Inspector GUI?

I've been running into troubles doing this and could use some extra thoughts. Here is the code I have so far.

I have an array stored in a script component named Enemy. I access this script component, and display it in my custom Inspector, but I always receive errors that not all elements are initialized.

Essentially, I am getting hung up on the idea of how this works: alt text

How can I get None (Transform) to appear in my Inspector instead of an error?

private bool collapsed; private int arraySize; private Enemy e;

public override void OnInspectorGUI() { EditorGUIUtility.LookLikeControls();

 e = target as Enemy;

 collapsed = EditorGUILayout.Foldout(collapsed, "Array");
 if (collapsed)
 {
     Rect r = EditorGUILayout.BeginVertical();

     arraySize = EditorGUILayout.IntField

     if (e.array.Length > 0)
     {
         Transform[] temp = new Transform[arraySize];

         e.array.CopyTo(temp, 0);

         e.array = new Transform[arraySize];

         temp.CopyTo(e.array, 0);
     }
     else
         e.array = new Transform[arraySize];

     for (int i = 0; i < arraySize; i++)
     {
         Transform t = e.array[i];
         t = EditorGUILayout.ObjectField(t.name, t, typeof(Transform)) as Transform;
     }

     EditorGUILayout.EndVertical();
 }

}

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 Techsuport · Aug 30, 2010 at 01:15 AM 0
Share

your kinda vague, if your asking how to take a built in array with some array indexes empty, and resize it to not have those empty slots, i went through each index in your array(array A, looks like your using a built in array, idk i use JS), asked if its not empty (if !=null), then put it in another array (array B, i used a JS array), then make array A = array B(if JS array, A=B.ToBuiltin(var type(for you, Transform)).

please clarify your question if i have not already answered it.

7 Replies

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

Answer by Darclaw · Jul 15, 2013 at 03:31 PM

If anyone interested, here's another example for array in custom editor: Base class:

    public class RoomItem : MonoBehaviour {
         public Transform[] targetPoints;
     // other data
     }

Custom editor:

 [CustomEditor(typeof(RoomItem))]
 public class RoomItemEditor : Editor {
     public override void OnInspectorGUI() {
         serializedObject.Update();
         var controller = target as RoomItem;
         EditorGUIUtility.LookLikeInspector();
         SerializedProperty tps = serializedObject.FindProperty ("targetPoints");
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PropertyField(tps, true);
         if(EditorGUI.EndChangeCheck())
             serializedObject.ApplyModifiedProperties();
         EditorGUIUtility.LookLikeControls();
     // ...
     }
 }
Comment
Add comment · Show 7 · 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 rocket5tim · Jul 27, 2013 at 03:40 AM 0
Share

That's exactly what I was looking for. Thanks!

avatar image justinl · Aug 12, 2013 at 11:36 PM 1
Share

Thank you! This was perfect and I thought much cleaner than the other solutions.

avatar image softrare · Dec 11, 2013 at 10:17 PM 0
Share

Perfect! +1

avatar image dmcclurg · Jun 20, 2014 at 07:58 PM 2
Share

Note that if you want an array of some class (not Transform), you'll need to mark it as Serializable.

    public class RoomItem : $$anonymous$$onoBehaviour {
 
         [System.Serializable]
         public class $$anonymous$$yClass {
             public Transform transform;
             public AnimationClip animClip;
         }
     
         public $$anonymous$$yClass[] targetPoints;
     }
avatar image zee_ola05 · Dec 14, 2017 at 11:08 PM 1
Share

EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeControls();

are now obsolete. What's the alternative?

Show more comments
avatar image
6

Answer by booferei · Jan 10, 2018 at 02:51 PM

If you want to mimic the default Array display, try this:

 SerializedProperty arrayProp = serializedObject.FindProperty("array");
 
 arrayProp.isExpanded = EditorGUILayout.Foldout(arrayProp.isExpanded, "Array");
 if (arrayProp.isExpanded) {
     arrayProp.arraySize = EditorGUILayout.IntField("Size", arrayProp.arraySize);
 
     for (int i = 0; i < arrayProp.arraySize; ++i) {
         SerializedProperty transformProp = arrayProp.GetArrayElementAtIndex(i);
         EditorGUILayout.PropertyField(transformProp, new GUIContent("Element " + i));
     }
 }
 
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 Tralafgar-Law · Feb 13, 2019 at 12:16 PM 0
Share

Thanks a lot, it confused me for a long time.

avatar image
5

Answer by steinbitglis · Feb 24, 2012 at 01:53 PM

This is roughly what DrawDefaultInspector() does:

 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()


It works like it should with bold font on prefab overrides etc. It also handles undo operations, 'delete' operations and 'shift+delete' operations as expected. It works with all the kinds of arrays that Unity originally supports. It also looks like the original.

Remember to call EditorGUIUtility.LookLikeControls() after drawing the array.

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 mepine · Dec 20, 2012 at 07:23 AM 0
Share

Thanks man! Almost the same as the default inspector! You saved my day. Now I need to figure out what each method does...

avatar image
4

Answer by Steven-Walker · Nov 16, 2010 at 02:22 AM

For anyone else who is searching for the solution to this, here is working code in Javascript. It doesn't look the same as the built-in array controls, but it works fine.

 // MyObject.js
 public var ElementsExpand : boolean = true;
 public var ElementsSize : int = 1;
 public var Elements : Transform[] = new Transform[ElementsSize];
 
 
 // MyObjectEditor.js - OnInspectorGUI()
 target.ElementsExpand = EditorGUILayout.Foldout(target.ElementsExpand, "Transforms");
 if(target.ElementsExpand) {
     var x : int = 0;
     target.ElementsSize = EditorGUILayout.IntField("Size", target.ElementsSize);
     if(target.Elements.length != target.ElementsSize) {
         var newArray : Transform[] = new Transform[target.ElementsSize];
         for(x = 0; x &lt; target.ElementsSize; x++) {
             if(target.Elements.length &gt; x) {
                 newArray[x] = target.Elements[x];
             }
         }
         target.Elements = newArray;
     }
     for(x = 0; x &lt; target.Elements.length; x++) {
         target.Elements[x] = EditorGUILayout.ObjectField("Element "+x, target.Elements[x], typeof(Transform));
     }
 }
             



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 cupsster · Apr 01, 2011 at 10:26 PM 0
Share

you made my day.. thanx!

avatar image
4

Answer by zee_ola05 · Dec 14, 2017 at 11:16 PM

 SerializedProperty property = serializedObject.FindProperty("myArray");
 EditorGUILayout.PropertyField(property, new GUIContent("My Array"), true);

Since, EditorGUIUtility.LookLikeInspector() and EditorGUIUtility.LookLikeControls() are now obsolete, here is an alternative.

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 ptracy · Dec 06, 2020 at 05:47 PM 0
Share

Best answer! This is super clean and effective. It also allows us to customize the string value as well, thank you! This should be the best answer I$$anonymous$$HO.

  • 1
  • 2
  • ›

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 can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

Editor scripting: Object reference not set to an instance of an object 0 Answers

OnInspectorGUI changes reset when played in editor or building 2 Answers

Script to create instance from in inspector 2 Answers

Custom Inspector & Arrays 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