- Home /
Custom inspector resets values when refreshing
I have this code
using UnityEngine;
using UnityEditor;
[CustomEditor( typeof(Preview) )]
public class PreviewEditor : Editor
{
float PreviewAngleAlpha = 0.25f;
public override void OnInspectorGUI() {
PreviewAngleAlpha = EditorGUILayout.Slider("PreviewAngle Alpha: ", PreviewAngleAlpha, 0.0f, 1.0f, GUILayout.Width(350));
}
}
so I have a value PreviewAngleAlpha that varies between 0 and 1 that I can adjust with a slider in the inspector, but everytime when I click on an a different object and then click back on the first object with this custom inspector on it, the value resets to 0.25f... How can I make it so that the inspector keeps using the same values?
Editor are used to edit other object data, if you want your editor save data that is not from the edited object you need use a ScriptableObject or a EditorPrefs to hold the data.
Yep, this is a good example for EditorPrefs as this seems to be a preference of this editor class.
Answer by Glurth · Nov 11, 2016 at 03:48 AM
Another option is to make the float variable static
static float PreviewAngleAlpha = 0.25f;
This will make it such that all PreviewEditor's will use the SAME float value: change it in one, you change it in all.
*This will NOT save the value across sessions; see Sergio7888's answer for that.
"statics" will also be lost on an assembly reload which happens every time you edit or import any script in your project.