- Home /
breaking out of OnGUI() ticks?
I'm not sure my title makes much sense but I couldn't find a better one...
My problem: I have code which runs constantly in OnGUI() for an editor window:
GameObject fbx, model, prefab;
bool childUpdateOnly = false;
bool dontCheck = false;
void OnGUI()
{
// Create selection field to allow the selection of the object to process
model = (GameObject)EditorGUILayout.ObjectField( "Select FBX Object", fbx, typeof( GameObject ), true );
oldPrefab = (GameObject)EditorGUILayout.ObjectField( "Select Prefab To Update", oldPrefab, typeof( GameObject ), false );
childUpdateOnly = (bool)EditorGUILayout.BeginToggleGroup( "Child Update Only", childUpdateOnly );
if( model && prefab )
{
if( model.name == prefab.name && dontCheck == false )
{
childUpdateOnly == false;
}
}
}
As you can see in the code above, 'childUpdateOnly' gets set on every update tick so that if the user removes a GameObject from one of the ObjectFields, it automatically gets set to the correct state. But I also want to be able to check/un-check it manually by clicking the toggle field in the EditorWindow, problem is, since the code checks for the ObjectFields state on every tick, 'childUpdateOnly' gets set back automatically so I can't manually check/un-check it.
How can I get around this problem? Is there a way to maybe use the Event system, with something like this:
if( Event.current.type == EventType.MouseClickUp )
{
childUpdateOnly = !childUpdateOnly;
dontCheck = true;
}
But then how do I set 'dontCheck = true' if the user removes or adds a GameObject from one of the ObjectFields?
I hope I'm explaining my problem clearly enough, if not I'm sorry :) Any help will be greatly appreciated as always, thanks for your time guys.
Stephane
http://docs.unity3d.com/Documentation/ScriptReference/EditorGUILayout.Toggle.html
may be you should use this ins$$anonymous$$d of BeginToggleGroup?
Hey Scroodge, thanks for the quick answer. I don't want to use a regular Toggle, because I need everything within the ToggleGroup to be disabled when the user un-checks the check box.
then... don't you forget about...
EditorGUILayout.EndToggleGroup();
???
I do have EditorGUILayout.EndToggleGroup(); at the end of my code (which I didn't include in the example above, sorry). That part is all working fine. What I need is a way to 'override' the if() statement which happens every tick by detecting a Event, like a dragging or mouse-click Event, so that I can toggle 'childUpdateOnly' manually even if the if() statement returns true...I realize my question might be confusing, it's really clear in my head but maybe I am not explaining what I want to do correctly?
ouch... i understood now 8) i'd solve this issue once in one of my projects...
what do you think about:
childUpdateOnly = EditorGUILayout.Toggle(...);//this can be easy styled like drop-down switcher if (childUpdateOnly) { ...draw other controls }
Answer by ScroodgeM · Aug 08, 2012 at 07:15 PM
unity serializes all public properties and stores it internally.
editor doesn't store any values at all. Editor class initializes every time it needed, so it can't be used to store any data at all. Editor is just a provider between you and component it created for.
'childUpdateOnly' in your case is a private variable so it will not be stored.
declare this property as public in class that this editor created for and change it in this editor on user's clicks
don't leave any needed data in editor.
Got it, I didn't know that! Thanks for the explanation, I'll give that a try.