- Home /
Custom names for variables (array-elements!!) in the inspector??
I've been looking and googling for a way to do that for hours now... is there really no attribute or something to do this simple thing - provide a string to be used in place of the variable name (or Element 0, 1...)?
Do I really need to write some custom attribute and drawer-thingy for this?
Run it through an enum somehow.
I do think youll need to write some editor code...
Why an enum? Will the enum-values be displayed as text? The problem is, I need something that isn't constant. Basically, I have an array/list of structures with a string as description/name and a float (with slider) as value. The rest is private data. I only want the description-string to be shown per class as the name (left) for the float-slider (right).
Yeah an enum is just a simple way of giving strings an index. I cant say Ive ever used them though. You may be facing the need to create a custom inspector, as you fear.
There are some hits on google.
http://answers.unity3d.com/questions/145262/na$$anonymous$$g-array-elements-in-editor.html
Answer by tanoshimi · Oct 04, 2015 at 06:42 PM
Forget drawers and attributes - you just need to write a custom inspector class for your script - it only takes a couple of lines, and you get a lot of flexibility as a result:
https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
Thanks. Are you sure it's just a few lines?...
I figured out how to create a custom label text and a slider that represents a property for a simple class.
What I have yet to figure out is
1) how to actually CHANGE the associated property from the inspector.
2) how to deal with nested classes or arrays/lists of their instances.
when I use the editor/inspector-override on the "base"-class (composition-wise), I don't know how to access properties of nested classes, especially when there is a whole list of them
when I try to create an override for the nested classes themselves, the override never gets called at all
To change the associated property, you call serializedObject.Apply$$anonymous$$odifiedProperties()
.
For styling "nested" classes/arrays in a custom inspector, you can use GetArrayElementAtIndex(i)
and FindPropertyRelative
.
Here's another more detailed tutorial that covers these and other issues: http://catlikecoding.com/unity/tutorials/editor/custom-list/
Answer by idbrii · Feb 05, 2018 at 08:10 PM
If the first variable in the class is a string, then Unity will use that variable as the array item label.
I've found other sources that say the variable needs to be named title, name, or key, but it doesn't seem to matter anymore:
// Here, Funky is used as the array item label. Title and ToString are ignored.
[Serializable]
public class Pairs {
public string Funky;
public string Title;
public bool IsMeshVisible;
public string ToString()
{
return string.Format("{0} {1}", Funky, IsMeshVisible);
}
}
I totally thought it had to be called "name" and modified your test to confirm it. Thanks!
Please tell us what to do if the first is not a string. I Just want all elements named after the class/struct name it represents. (In your example "Pairs")
Answer by joaoborks · Mar 14, 2020 at 05:32 PM
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, true);
}
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;
}
Your answer
Follow this Question
Related Questions
If I have a bool method, how can I see in the inspector whether it returns true or false? 2 Answers
Custom Inspector. Aligning properties? 1 Answer
Showing properties from a base class in inspector? 0 Answers
Hide/Show properties dynamically in inspector. 6 Answers
What should be handled in the custom inspector version of a script? 1 Answer