x


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

more ▼

asked Jul 13 '10 at 10:49 AM

Jaywalker gravatar image

Jaywalker
514 15 21 30

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

Jul 13 '10 at 12:18 PM spinaljack

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

Jul 13 '10 at 02:55 PM Jaywalker
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jul 13 '10 at 03:43 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

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!

Jul 13 '10 at 07:48 PM Jaywalker
(comments are locked)
10|3000 characters needed characters left

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();
}
more ▼

answered Apr 08 '11 at 08:25 AM

Andrea 1 gravatar image

Andrea 1
92 2 3 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3680
x1358
x110
x69

asked: Jul 13 '10 at 10:49 AM

Seen: 6817 times

Last Updated: Jul 13 '10 at 03:01 PM