- Home /
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
you declare game object arrays like this: var GOArray : GameObject[];
Hey, Thanks for the comment! not the issue though.. ill edit the question to elaborate!
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.
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!
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;
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 ...
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!
What about for in a Custom Editor Window, not for a Custom Inspector?
Editor Windows are a little different: http://blogs.unity3d.com/2012/10/25/unity-serialization/
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?
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();
}
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
Follow this Question
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