- Home /
Changing Inspector's Serialized Property Label (With Code Sample)
I've been making progress with one of my first editor extensions / tools for my own personal use and perhaps asset store uploading. It works decently well, but I have a simple question... First, the code.
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(StandardBrush))]
public class StandardBrushINS : Editor
{
private SerializedObject standardBrush;
private SerializedProperty width;
private SerializedProperty length;
private SerializedProperty height;
void OnEnable()
{
StandardBrush tempTarget = (StandardBrush)target;
tempTarget.GetComponent<MeshFilter>().hideFlags = HideFlags.HideInInspector;
tempTarget.GetComponent<MeshRenderer> ().hideFlags = HideFlags.HideInInspector;
standardBrush = new SerializedObject (target);
width = standardBrush.FindProperty ("userDesiredWidth");
length = standardBrush.FindProperty ("userDesiredLength");
height = standardBrush.FindProperty ("userDesiredHeight");
}
public override void OnInspectorGUI ()
{
EditorGUIUtility.LookLikeControls();
standardBrush.Update ();
EditorGUILayout.PropertyField (width);
EditorGUILayout.PropertyField (length);
EditorGUILayout.PropertyField (height);
standardBrush.ApplyModifiedProperties();
}
}
So this code works, it updates the properties of the target in real time. Perfect. However, I have an issue with the SerializedProperty's labels. They inherit a label next to the field that is essentially the name of the variable. In this case, "User Desired Width/Length/Height" appear next to each field. This is redundant for the client as the "User Desired" naming convention is used mostly to make code more legible (as I do various checks between the User Desired value and the Current value.)
If this were a EditorGUILayout.FloatField, I would be able to put in my desired "label" as a string before the target float variable (i.e. EditorGuiLayout.FloatField("width", target.userDesiredWidth) .) Is there an equivalent for SerializedProperty?
Yes, I was thinking GUIContent would provide a solution, but unfortunately I kept getting an error trying to create a new GUIContent object.
Answer by Jamora · Jan 30, 2014 at 09:10 PM
You can create a new GUIContent to show a different label as follows:
EditorGUILayout.PropertyField(serializedObject.FindProperty("myVariable"), new GUIContent("aDifferentLabel"));
That's strange. I tried that before and the Unity IDE was yelling at me. I ignored it and it still compiled correctly. I thought perhaps I was missing arguments. This is the answer, just thought I'd come back and let anyone else looking this up know.
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
EditorApplication.projectWindowItemOnGUI and sub assets 1 Answer
Call a function from CustomPropertyDrawer of Arbitrary class 1 Answer
Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor 0 Answers
CustomEditor & Serialization not keeping Overrides & not working with Animation 1 Answer