Why can't FindProperty find properties?
I'm trying to update my Editor code to use serializedObject and [CanEditMultipleObjects], but I'm hitting a few problems here. My main problem seems to be the fact that FindProperty can't find properties, but for some reason it can find public fields. This doesn't really make much sense in my world
Here's my code:
[Serializable]
public class MushroomPosition: MonoBehaviour
{
[SerializeField]
private int row = 0;
public int Row
{
get
{
return row;
}
set
{
if (row == value)
return;
row = value;
DoSomeWork();
}
}
private void DoSomeWork()
{
// Do some work!!!
}
}
[CustomEditor(typeof(MushroomPosition), true)]
[CanEditMultipleObjects]
public class ItemPositionEditor : Editor
{
SerializedProperty rowProperty;
SerializedProperty rowVariable;
private void OnEnable()
{
// There is a property called Row in my MushroomPosition code, but it can't find it. I would assume a method called FindProperty would find the property
rowProperty = serializedObject.FindProperty("Row");
// There is a public field called row on the object. For some weird reason this actually finds it. I would assume a method called FindProperty would NOT be able to find fields... but it does... ehhhh?
rowVariable= serializedObject.FindProperty("row");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.IntSlider(rowProperty, -15, 15, new GUIContent("Row"));
serializedObject.ApplyModifiedProperties();
}
}
Googling for answers I have only been able to find threads where people using FindProperty are searching for public fields on an object, but none where they are actually using FindProperty to... well... you know... find a property..?
I need Editor to use the property to set the value as I need it to execute some code when the value is changed. How can this be done?
Answer by Hellium · Nov 11, 2018 at 04:28 PM
As indicated by the documentation, FindProperty
finds a serialized property. In .NET term, it's a field. Unity deliberately does not serialize getters and setters to prevent possible side effects.
Because row
is marked as SerializeField
, FindProperty
is able to retrieve it.
If you want to use the getter and setter in your custom inspector, I advise you to do as follow:
public class ItemPositionEditor : Editor
{
MushroomPosition mushroomPosition;
SerializedProperty rowProperty;
SerializedProperty rowVariable;
private void OnEnable()
{
mushroomPosition = (MushroomPosition ) target;
}
public override void OnInspectorGUI()
{
// Not sure if the following line is needed anymore
// Unless you need to edit other SerializedProperty
serializedObject.Update();
mushroomPosition.Row = EditorGUILayout.IntSlider(mushroomPosition.Row, -15, 15, new GUIContent("Row"));
// Not sure if the following line is needed anymore
// Unless you need to edit other SerializedProperty
serializedObject.ApplyModifiedProperties();
}
}
Answer by Lesh48 · May 21 at 02:53 PM
You can simply add [SerializeField] [HideInInspector] to private field and get it from the editor.