In the past I’ve used [ExposeProperty] to expose getter/setters in the Unity Editor.
Unfortunately, every time I press play, the values that the properties wrap get reset back to their default values. I know this has worked in the past, but for some reason, I can’t seem reproduce it working at all, even trying to use older versions of Unity.
I’ve included the necessary scripts to test this in a unitypackage. Just attach MyType.cs to a game object, change the values in the editor, and hit Play to see them get reset.
Any ideas on how to get this working again?
Got it finally.
Long story short: Thats just the way it works- when you hit play, the private/protected variable behind the property will be reset. You have to add [SerializeProperty] to make it save its value into the run. However, this creates the negative side effect of exposing the private / protected field in the editor. Undo that by adding [HideInInspector].
So, the resulting MonoBehavior component, after implementing [ExposeProperty] and component’s associated custom editor, will look something like: (note untested code)
using UnityEngine;
using System.Collections;
public class MyType : MonoBehaviour
{
[HideInInspector] [SerializeField] int m_SomeInt;
[ExposeProperty]
public int SomeInt
{
get { return m_SomeInt; }
set { m_SomeInt = value; }
}
}
Here are some related links on the topic…
http://answers.unity3d.com/questions/22198/monobehaviour-properties-not-saving-their-values-from-editor
http://answers.unity3d.com/questions/17986/accessing-class-properties-through-getters-setters-in-editor-mode
http://answers.unity3d.com/questions/11775/expose-public-property-not-variable-in-inspector
http://answers.unity3d.com/questions/7673/how-do-i-expose-class-properties-not-fields-in-c
http://feedback.unity3d.com/forums/15792-unity/suggestions/273288-scripting-c-expose-properties-to-editor
2 Likes