Naming array elements in editor

I’m setting up a system to spawn enemies in my game. Really this is just a cosmetic issue but I would just like to know how to set it up properly anyway.

I’ve created a new class that holds information about a round of enemy spawns, and then I’m using an array to store this information so that it appears in the editor like this:

alt text

What I want is instead of it saying “Element 0” I would prefer it to say “Wave 0” and then “Wave 1” etc. I know it’s just a cosmetic thing but it’s gonna bug me until I get it right :slight_smile:

I found I could add a string variable to the class with the name “name” and whatever I changed that variable to would update the text “element 0” to be that name, but I would like it to just handle this automatically if possible.

Also if setting this all up in an array is the wrong way to go about it or you have any other suggestions please let me know!

Thank you!

I believe I have somewhat of an answer to your question. And this is way out of date but in case someone is looking I’ll post and example here…

I have a simple container class that just holds the name of something and if it has been used or not:

    [System.Serializable]
    public class DragonsClass 
    {
        public string Name;
        [HideInInspector]
        public bool bUsed;
    }
    
    public DragonsClass[] DragonsInLevel; 

By filling out the name varaibles in the inspector, the “element#” is replaced by the string I enter:
alt text

No super useful for this example but you get the jist of an easy way to name your elements in the inspector.

If anyone else struggles with this, I’ve created a simple solution to name your array elements based on a given enum or array of strings:

Code Samples:

Attribute:

/**
 * LabeledArrayAttribute.cs
 * Created by: Joao Borks [joao.borks@gmail.com]
 * Created on: 28/12/17 (dd/mm/yy)
 * Reference from John Avery: https://forum.unity.com/threads/how-to-change-the-name-of-list-elements-in-the-inspector.448910/
 */

using UnityEngine;
using System;

public class LabeledArrayAttribute : PropertyAttribute
{
    public readonly string[] names;
    public LabeledArrayAttribute(string[] names) { this.names = names; }
    public LabeledArrayAttribute(Type enumType) { names = Enum.GetNames(enumType); }
}

Property Drawer:

/**
 * LabeledArrayDrawer.cs
 * Created by: Joao Borks [joao.borks@gmail.com]
 * Created on: 28/12/17 (dd/mm/yy)
 * Reference from John Avery: https://forum.unity.com/threads/how-to-change-the-name-of-list-elements-in-the-inspector.448910/
 */

using UnityEngine;
using UnityEditor;
using System.Linq;

// Don't forget to put this file inside an Editor folder!
[CustomPropertyDrawer(typeof(LabeledArrayAttribute))]
public class LabeledArrayDrawer : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property, label);
    }

    public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(rect, label, property);
        try
        {
            var path = property.propertyPath;
            int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
            EditorGUI.PropertyField(rect, property, new GUIContent(((LabeledArrayAttribute)attribute).names[pos]), true);
        }
        catch
        {
            EditorGUI.PropertyField(rect, property, label, true);
        }
        EditorGUI.EndProperty();
    }
}

Example:

using UnityEngine;

public class LabeledArrayExample : MonoBehaviour
{
	[LabeledArray(new string[] { "First", "Second", "Third" })]
	public int[] labeledValues;
	
	public enum Order
	{
		First,
		Second,
		Third
	}
	
	[LabeledArray(typeof(Order))]
	public int[] enumLabeledValues;
}

You can write a custom inspector, which duplicates the array functionality and adds the ability to change the element names. (By the way, it seems like a bug that having a string in the class changes the element name, especially since it only works if the string variable is first.)