- Home /
Property Drawer To Rename Variable and Make Serializable
When naming private variables, I like to use the convention of the m prefix (e.g. mVar). However, when made serializable, Unity displays this in a funny way in the editor (M Var), so I wanted to write a property drawer to rename this (so Var is the name displayed in the editor).
The code I wrote consisted of empty class
public class m : PropertyAttribute
{
}
And the property drawer code
[CustomPropertyDrawer(typeof(m))]
public class RenameEditor : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
label.text = label.text.Substring(2);
EditorGUI.PropertyField(position, property, label);
}
}
This works successfully by removing the M and space from the variable label, but am I struggling to find a way to also ensure that this new property drawer will Serialize private variables. As such, I currently have to also tag the SerializeField attribute, like so:
[m,SerializeField]
private float mMovementSpeed = 5f;
Does anyone know how I could ensure that my new property drawer also contains the SerializeField properties by default? (Also I am aware that using the m prefix is no longer advised, and that using m_ will display better, but for consistency I would like to continue with this for the time being)
I don't think you can do that. The PropertyDrawer won't even come into play if the field isn't being serialized, so how can it force that to happen?
I would also question whether this is a good idea. It sounds like you want all your private variables to be serialized. Is there a reason for that? Seems better to me for you to have the power to decide on a case-by-case basis whether that's what you want (and note that you can always see them in the editor when needed, by choosing the Debug view).
If you can explain what you're trying to do and why, it might help people to come up with potential solutions for you.
It's not that I want all private variables to be serialized, it's that I want all serialized private variables to have the $$anonymous$$ in the label removed. So my plan is to create a new attribute that both makes a private variable serialized AND removes the m. And then, I would use this attribute any time I want a private variable serialized, and unlike the default SerializeField attribute, it would display nicely in the inspector.
I hope that explains it better.
Yes I think that's clear. What you'd need to do is somehow give your attribute the same functionality as the System.Serializable attribute. But you can't inherit that functionality because it's sealed.
I think probably the best you could do is to combine the attributes, so that ins$$anonymous$$d of needing to write [Remove$$anonymous$$,SerializeField]
you could write [Remove$$anonymous$$AndSerializeField]
. But this would I$$anonymous$$HO be insane; far more trouble than it's worth for the sake of not having to make a few keystrokes. Especially since, logically, these are quite unconnected attributes.
If you decide that you are that insane :) you could check out eg the various solutions given here : https://stackoverflow.com/questions/38503146/combining-multiple-attributes-to-a-single-attribute
Property drawer is about to control how to display your custom types in the inspector, I don't think you can apply it to whatever type you want and even if you can, this could have many undesired consequences: what if you then want to make a custom drawer for some of your types as it is intended? Unity will display automatically serialize fields without "m_" prefix, so I'd suggest you to stick with it.
Your answer
Follow this Question
Related Questions
Customizing Inspector variables. 2 Answers
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer
Reset a SerializedProperty to it's default value. 2 Answers
Loading sprite by script shows weird icon 0 Answers
Serialize Custom Abstract Class (No-MonoBehaviour / No-ScriptableObject) 1 Answer