- Home /
change the name of List elements in the inspector
I tried to do it here
but not work, i make a script : NamedArrayAttribute:
using UnityEngine;
public class NamedArrayAttribute : PropertyAttribute
{
public readonly string[] names;
public NamedArrayAttribute(string[] names) { this.names = names; }
}
PropertyDrawer:
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer (typeof(NamedArrayAttribute))]public class NamedArrayDrawer : PropertyDrawer
{
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
try {
int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
EditorGUI.ObjectField(rect, property, new GUIContent(((NamedArrayAttribute)attribute).names[pos]));
} catch {
EditorGUI.ObjectField(rect, property, label);
}
}
}
but namespace name "NamedArrayAttribute" could not be found
Do you have multiple assemblies in your project? Have you tried to use [NamedArray]
attribute ins$$anonymous$$d of [NamedArrayAttribute]
?
Answer by Bunny83 · Apr 29, 2019 at 12:17 PM
Make sure you declared your "NamedArrayAttribute" inside a runtime class, not inside an editor file. The attribute need to be part of the runtime. If you declare it inside an editor script it is not available for your runtime scripts. So you can not declare the attribute definition inside the same file as your propertydrawer.
It's best practise to place the attribute definition in a seperate file that has the filename matching with the defined type. So create a "NamedArrayAttribute.cs" file somewhere inside your assets folder but not inside an editor folder.
ps: you may want to change the attribute constructor to take a params array so the initialization is simpler. Without the params keyword you have to do
[NamedArray(new string[] {"str1", "str2", "str3", "str4"})]
with the params keyword you can simply do
[NamedArray("str1", "str2", "str3", "str4")]
Even the the effect is the same it's simpler to use. The params keyword is essentially syntactic sugar for the first variant. So the array creation is done by the compiler for you.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Explanation on how to use LIST<> in UnityScript 2 Answers
How to see what element is the current one 2 Answers
how can i check if there is a blanc spot in my list 3 Answers