I cannot rename list element names in the inspector

Hiya.
I was trying to clean up my inspector view a bit and wanted to spice up my list/arrays names in the inspector.
I knew that i should’ve made a public string called “name” and insert it in the base class for my list.
Then they changed the list/array ui a bit in the 2020.2 and this doesnt work anymore is there any way that i can still rename list/array names in any way without writing a custom inspector for it?

I’m sorry I don’t quite understand what you’re doing.

If you have an array/list called “Animals” and want to instead call it “Vegetables,” by default Unity will not be able to reconnect that, but there are helper decorators:

You can also go in and hand-edit the scenes and/or prefabs (they are just text files) and rename the actual fields, which is what I prefer to do.

Maybe i should’ve explained it better
6884873--804527--upload_2021-2-27_21-15-12.png
I meant chaning the text from “Element 0” to the some string varriable which is name in this case.

AFAIK no, but if there is, I’d like to hear about it. “Element N” is just Unity giving your indices generic names. If you really want new names, don’t use lists, use individual objects. (unless there is a way to rename indices!)

1 Like

In your case, just move the Name variable above the rest in the code.

        public string name;
        public int texturesID;
        public float rarity;

Instead of this:

        public int texturesID;
        public float rarity;
        public string name;
2 Likes

thank you it works now

There are at least two ways:

  1. Add field “public string name” to item class
[Serializable]
public class ListElement
{
    [SerializeField]
    public string name; //<--

    [SerializeField]
    public string Text;
}
  1. Using CustomPropertyDrawer

For example, I has List container class:

[Serializable]
public class UnityLocalization : ILocalization
{
    [SerializeField]
    public List<UnityLocalizationString> StringContainer;
}

Some custom class for element

public enum LocalizationStringId{
 Label1, Label2
}

[Serializable]
public class UnityLocalizationString
{
    [SerializeField]
    public LocalizationStringId StringId;

    [SerializeField]
    public string Text;
}

I want custom label from enum LocalizationStringId. Using CustomPropertyDrawer:

[CustomPropertyDrawer(typeof(UnityLocalizationString))]
public class IladrinDrawerUIE : PropertyDrawer
{
    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        var container = new VisualElement();
        var intValue = property.FindPropertyRelative(nameof(UnityLocalizationString.StringId)).intValue;
        var label = Enum.GetName(typeof (LocalizationStringId), intValue);
        var unitField = new PropertyField(property, label);
        container.Add(unitField);
        return container;
    }
}

//property.FindPropertyRelative(nameof(UnityLocalizationString.StringId)).intValue;
If your property for label has another type, u can use “.stringValue” or etc.

But labels changes only on editor redraw. With OnGUI this can be change label dynamically someway. If somebody find a solution - reply.

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
    {
        var intValue = property.FindPropertyRelative(nameof(UnityLocalizationString.StringId)).intValue;
        var labelString = Enum.GetName(typeof(LocalizationStringId), intValue);
        content.text = labelString;
        content.tooltip = property.FindPropertyRelative(nameof(UnityLocalizationString.Text)).stringValue;
        EditorGUILayout.PropertyField(property, content, GUILayout.ExpandHeight(true));
    }

This code will update label onfly, but this will draw empty spaces.

1 Like