- Home /
Editor values not updating in OnSceneGUI
So, here's my basic problem. My custom editor is using both OnInspectorGUI and OnSceneGUI.
The editor itself has several values - mostly integers - for holding editor state information. Things like which tab is selected, or which options in a list, things like that. When those values are updated, OnSceneGUI reports no change in them. Here is a simplified version:
public class ProblemFinder : MonoBehaviour {
// Tried containing variables on editor target, instead of editor, but got the same result.
// public int testInt = -1;
// public string[] testButtons = new string[] { "Test 1", "Bob", "Blueberry" };
}
[CustomEditor (typeof(ProblemFinder))]
public class ProblemFinderEditor : Editor {
int testInt = -1, currentInt = -1;
string[] testButtons = new string[] { "Test 1", "Bob", "Blueberry" };
public override void OnInspectorGUI()
{
Debug.Log("Test Int Value: " + testInt.ToString());
for (int i = 0; i < testButtons.Length; i++)
{
if (currentInt != i && GUILayout.Button(testButtons[i]))
{
testInt = i;
Debug.Log("Clicked " + testInt.ToString() + ". " + testButtons[i]);
SceneView.RepaintAll();
}
else if(currentInt ==i)
{
GUILayout.Label(testButtons[i]);
}
}
}
public void OnSceneGUI()
{
Debug.Log("Scene View Updated: " + testInt.ToString());
if (testInt != -1)
{
// I never see this message. testInt ALWAYS == -1 in this function
Debug.Log("Test Int = " + testInt.ToString());
currentInt = testInt;
testInt = -1;
}
}
}
I see the three buttons I would expect, in the Inspector: Test 1, Bob, and Blueberry.
Clicking Blueberry returns the following:
Clicked 2. Blueberry
Scene View Updated: -1
My initial debug comment, in OnInspectorGUI confirms that testInt HOLDS its value of 2.
Even so, OnSceneGUI ALWAYS returns it with a value of -1. Anyone have any idea why?
I'm using Unity 5.5. I was not having this problem prior to updating, and some, but not all, other teammates, also now updated to 5.5 are NOT having the same issues.
I have tried reimporting the scripts, I've tried creating the test scripts above (which DO exhibit the exact issues I describe here), I've tried reinstalling Unity - and none of that has made an impact so far.
Answer by Bunny83 · Dec 11, 2016 at 11:54 AM
Well, i don't have the latest Unity version but i would simply guess that Unity creates a seperate editor instance for the sceneview callback. An Editor is not ment to hold data as it's just an editor for the inspected class.
Try printing the instance ID of each editor instance.
Debug.Log("OnInspectorGUI: " + GetInstanceID());
Debug.Log("OnSceneGUI: " + GetInstanceID());
Most likely they are not the same. They might have completely seperated the drawing of the scene view from the drawing of the inspector.
One solution could be to use static variables inside your editor. Or store the things you need in both editor instances in your actual MonoBehaviour. If it's more like a global editor setting you could also use EditorPrefs.