- Home /
How to manage toggle (and other controls) state in a custom inspector?
I have a custom inspector with toggles for a number of items to make it more manageable:
Each "Item #" can be toggled (simulating the Unity array inspector). The toggles are only part of the editor itself and exist only to make the editor more easy to digest. Unfortunately since there is no boolean in my object representing the toggle state it means that all the toggles reset as I switch between objects in Unity.
I could solve this issue by putting a variable for the toggle state into my object but this seems wasteful and unnecessary.
I know that Unity has some method of handling this without intruding into the object itself because the default inspector toggles work correctly with no "extra" state variables in the object.
How can I get Unity to remember the toggle state?
Answer by Bunny83 · May 23, 2011 at 01:24 AM
Just add a boolean array to your inspector. When you size your array you have to size this array as well. You could make the array a static variable, so the toggled items keep their state when selecting different objects.
OnEnable is fired when you select one of your objects. You should check and update the array here. I would use a List instead of an array ;) just because it's easier to resize.
I was basically doing that but I was using start. It seems that the entire inspector is re-created each time you select an object. So this won't actually work. I have almost worked around that by storing my inspector state variables in a static dictionary keyed on the object I have selected. Unfortunately this works until I hit "Play" then it loses all state again (I guess the objects get new identities).
Why do you want to store the states per object? wouldn't it be enough to create one static list? Of course if you have toggled an item and you select another object the same items(index-based) will be toggled. If you really want the toggle state per object, add the state variable to your object. You can wrap the variable in #if UNITY_EDITOR .... #endif
so it won't go into your build.
Answer by Adam-Buckner · Aug 26, 2011 at 08:11 PM
My solution was similar to Bunny83's. I have an array for the boolean flags for each element, but I save the flags to editor prefs. This code is first pass, and needs some polish, but goes something like this:
public MyType thisTarget;
public bool[] displaysExpanded = new bool[0];
void OnEnable () {
thisTarget = target as MyType;
displaysExpanded = new bool[thisTarget.myArrayOrList.Length];
for (int i = 0; i < displaysExpanded.Length; i++) {
displaysExpanded[i] = EditorPrefs.GetBool ("FoldFlag"+i);
}
}
void OnDisable () {
for (int i = 0; i < displaysExpanded.Length; i++) {
EditorPrefs.SetBool ("FoldFlag"+i, displaysExpanded[i]);
}
}
If you wanted to save this per game object, you could have the editor prefs key be something like "FoldFlag"+target.gameobject.name+i (or whatever is legal).
Your answer
Follow this Question
Related Questions
Custom Editor for GameObject Class look like Unity's Custom Inspector for GameObject? 1 Answer
Editor randomly unparents game object after InstantiatePrefab() & SetParent() in OnInspectorGUI() 0 Answers
Change gameview ratio / resolution using a custom inspector? 1 Answer