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
4
Question by Jaywalker · Jul 13, 2010 at 10:49 AM · guiarraygameobjectseditorgui

GameObject Array in Editor GUI

Hey,

I'm trying to get an array of gameobjects editable in a custom editor. Does anyone know a good way to do this?

For instance what if the enemyObject in the following code snippet was an Array[], how can I make this an editable Array list like in the normal inspector?

var enemyObject : GameObject;

 function OnGUI () 
     {
         enemyObject = EditorGUILayout.ObjectField ("enemyObject",enemyObject, typeof(GameObject));
     }

For clarity: I'm building a custom editor window (EditorGUI), and that's were I want to be able to see my GameObject Array. Doesn't have to be editable, just need to see whats in there.

Thanks a lot!

cheers

Comment
Add comment · Show 2
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 spinaljack · Jul 13, 2010 at 12:18 PM 0
Share

you declare game object arrays like this: var GOArray : GameObject[];

avatar image Jaywalker · Jul 13, 2010 at 02:55 PM 0
Share

Hey, Thanks for the comment! not the issue though.. ill edit the question to elaborate!

3 Replies

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

Answer by Tetrad · Jul 13, 2010 at 03:43 PM

You probably have to do it by hand.

Untested:

var scrollPosition : Vector2;

function OnGUI() { scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

     foreach( GameObject go in myGameObjectArray )
     {
           go = EditorGUILayout.ObjectField( go.name, go, typeof( GameObject ) );
     } 

 EditorGUILayout.EndScrollView();

}

You might be able to set up something with a Foldout object to make it collapsable, as well as maybe putting an int field there to resize your array. But that's the path I would suggest going down.

Comment
Add comment · Show 2 · 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 Jaywalker · Jul 13, 2010 at 07:48 PM 0
Share

Thanks! I ended up using only the for loop, as my array's are quite small. But awesome to learn that I can use them in this manner. Thanks again!

cheers!

avatar image shinriyo_twitter · Nov 12, 2015 at 11:28 AM 0
Share
  error CS1656: Cannot assign to `go' because it is a `foreach iteration variable'

it must happen the error. You must use for expression, and I guess It needs cast. go = EditorGUILayout.ObjectField( go.name, go, typeof( GameObject ) ) as GameObject;

avatar image
8

Answer by Cyril-Mishakin · Jun 25, 2014 at 07:40 PM

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor(typeof(MyClass))]
 
 public class BoardEditor : Editor {
     private SerializedObject m_Object;
     private SerializedProperty m_Property;
 
     void OnEnable() {
         m_Object = new SerializedObject(target);
     }
     
     public override void OnInspectorGUI(){
         m_Property = m_Object.FindProperty("MyProperty");
         EditorGUILayout.PropertyField(m_Property, new GUIContent("MyLabel"), true);
         m_Object.ApplyModifiedProperties();
     }
 }

Unity Editor GUI has function to make the same ARRAY view like in default inspector! All you need is to assign TRUE to IncludeChildren property in function EditorGUILayout.PropertyField() like in example! sorry for my English) best wishes) PS I found it thanks to your examples!) so thank you all ...

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 zeman97 · Mar 18, 2015 at 07:57 PM 0
Share

Thanks for the great answer! Odd how you have to allow scene objects for objects that cant be in the scene on their own (I needed it for AudioClips) but still, thanks!

avatar image JoshuaHodkinson · Mar 28, 2015 at 03:25 AM 0
Share

What about for in a Custom Editor Window, not for a Custom Inspector?

avatar image Tiki · Mar 28, 2015 at 12:07 PM 0
Share

Editor Windows are a little different: http://blogs.unity3d.com/2012/10/25/unity-serialization/

avatar image vuwij · Jun 25, 2015 at 09:54 PM 0
Share

this one works

avatar image AntonQvarfordt · Jan 06, 2016 at 08:21 AM 0
Share

I can't modify the array values for some reason, they just revert back to default after I type them in. Any ideas on that?

avatar image
5

Answer by Andrea 1 · Apr 08, 2011 at 08:25 AM

try this:

SerializedObject m_Object; SerializedProperty m_Property;

void OnEnable() { m_Object = new SerializedObject(target); }

public override void OnInspectorGUI() { m_Property = m_Object.FindProperty("NAMEOFARRAY"); EditorGUILayout.BeginVertical(); do { if (m_Property.propertyPath != "NAMEOFARRAY" && !m_Property.propertyPath.StartsWith("NAMEOFARRAY" + ".") ) { break; }
EditorGUILayout.PropertyField(m_Property); } while (m_Property.NextVisible(true)); EditorGUILayout.EndVertical();

 // Apply the property, handle undo
 m_Object.ApplyModifiedProperties();
 //DrawDefaultInspector();

}

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 Tiki · Apr 26, 2014 at 01:44 PM 0
Share

Anyone just trying to figure out how to make an array show up in Inspector with a custom editor script. Andrea has the solution you're looking for, though it needs some tweaking to be collapsable.

  do {
 if (m_Property.propertyPath != "NA$$anonymous$$EOFARRAY" && !m_Property.propertyPath.StartsWith("NA$$anonymous$$EOFARRAY" + ".") ) {
 break;
 }
 EditorGUILayout.PropertyField(m_Property);
 } while (m_Property.NextVisible(true));

Replace m_Property with your serialized property declaration, and replace NA$$anonymous$$EOFARRAY with your array's name in the targeted script. I.$$anonymous$$ Array named $$anonymous$$yTextures[] would replace NA$$anonymous$$EOFARRAY as "$$anonymous$$yTextures". Thanks Andrea!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Fire Points 2 Answers

How to show an array of custom objects in Inspector? 2 Answers

Puttings GUI's between objects 1 Answer

Member arrays not initialized in editor until after scripts are recompiled 1 Answer

Custom Material Editor 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