Custom Editor Script resets values on Play

Alright, this has been driving me nuts.

I’ve worked with Unity for years, but this is the first time I’ve ever really made an Editor script. After scrounging around the internet, trying to sift out outdated code and whatnot, I made a script that I can use to specify Ranges (I know a Vector2 would work, but I have plans for this script). However, when I hit play, any values entered in the Inspector reset to 0 (or a default value). What am I doing wrong?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityObject = UnityEngine.Object;
#endif

[System.Serializable]
public class Range<T> where T : IComparable
{
    T _min;
    T _max;

    public T Min
    {
        get { return _min; }
        set { _min = value;  }
    }
    public T Max
    {
        get { return _max; }
        set { _max = value; }
    }

    public Range() {}

    public Range(T min, T max)
    {
        if(min.CompareTo(max) > 0)
        {
            Debug.LogError("Error: Range " + min + " is greater than " + max);
        }
        this._min = min;
        this._max = max;
    }

    public bool Contains(T value)
    {
        return value.CompareTo(this._min) >= 0 && value.CompareTo(this._max) <= 0;
    }
}
#if UNITY_EDITOR
public abstract class RangeDrawer<T> : PropertyDrawer where T : IComparable
{
    [SerializeField]
    private Range<T> _range;
    private bool _Foldout;
    private const float kButtonWidth = 18f;

    private static readonly Dictionary<Type, Func<Rect, object, object>> _Fields =
        new Dictionary<Type, Func<Rect, object, object>>()
        {
            { typeof(int), (rect, value) => EditorGUI.IntField(rect, (int)value) },
            { typeof(float), (rect, value) => EditorGUI.FloatField(rect, (float)value) },
            { typeof(string), (rect, value) => EditorGUI.TextField(rect, (string)value) },
            { typeof(bool), (rect, value) => EditorGUI.Toggle(rect, (bool)value) },
            { typeof(Vector2), (rect, value) => EditorGUI.Vector2Field(rect, GUIContent.none, (Vector2)value) },
            { typeof(Vector3), (rect, value) => EditorGUI.Vector3Field(rect, GUIContent.none, (Vector3)value) },
            { typeof(Bounds), (rect, value) => EditorGUI.BoundsField(rect, (Bounds)value) },
            { typeof(Rect), (rect, value) => EditorGUI.RectField(rect, (Rect)value) },
        };

    private static V DoField<V>(Rect rect, Type type, V value)
    {
        Func<Rect, object, object> field;
        if (_Fields.TryGetValue(type, out field))
            return (V)field(rect, value);

        if (type.IsEnum)
            return (V)(object)EditorGUI.EnumPopup(rect, (Enum)(object)value);

        if (typeof(UnityObject).IsAssignableFrom(type))
            return (V)(object)EditorGUI.ObjectField(rect, (UnityObject)(object)value, type, true);

        Debug.Log("Type is not supported: " + type);
        return value;
    }

    private void CheckInitialize(SerializedProperty property, GUIContent label)
    {
        if (_range == null)
        {
            var target = property.serializedObject.targetObject;
            _range = fieldInfo.GetValue(target) as Range<T>;
            if (_range == null)
            {
                _range = new Range<T>();
                fieldInfo.SetValue(target, _range);
            }

            _Foldout = EditorPrefs.GetBool(label.text);
        }
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        CheckInitialize(property, label);
        if (_Foldout)
            return 51f;
        return 17f;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        CheckInitialize(property, label);
        position.height = 17f;

        var foldoutRect = position;
        foldoutRect.width -= 2 * kButtonWidth;
        EditorGUI.BeginChangeCheck();
        _Foldout = EditorGUI.Foldout(foldoutRect, _Foldout, label, true);
        if (EditorGUI.EndChangeCheck())
            EditorPrefs.SetBool(label.text, _Foldout);
        if (!_Foldout)
            return;
        position.y += 17f;
        T value;
        EditorGUI.BeginChangeCheck();
        value = DoField(position, typeof(T), _range.Min);
        if(EditorGUI.EndChangeCheck())
        {
            _range.Min = value;
        }
        position.y += 17f;
        EditorGUI.BeginChangeCheck();
        value = DoField(position, typeof(T), _range.Max);
        if(EditorGUI.EndChangeCheck())
        {
            _range.Max = value;
        }
        property.serializedObject.ApplyModifiedProperties();
    }
}
#endif

And, for the curious, here’s how it’s used:

public class SomeClass : MonoBehaviour {
    [System.Serializable] public class floatRange: Range<float> { }

    [SerializeField]
    public floatRange range;
}

There are a couple of problems here at a glance.

  1. Your _min and _max fields are not marked with the SerializeField attribute, so there is no way to save changes to them between domain reloads (e.g., playing/stopping the game)
  2. You are editing values on your target (inspected) object directly instead of its serialized data. This by itself is not a problem necessarily, but puts the burden of a lot more work on you to make sure your changes are saved, that they are undoable, and that they work with multiple object selections. Instead of getting the target object and reading/writing to its fields, you should instead use something like property.FindRelative("_min").floatValue if e.g., property.propertyType == SerializedPropertyType.Float. You may find my answer on this question helpful.