- Home /
Using EditorGUILayout controls in OnSceneGUI()
I'm implementing a simple level editor that functions in the scene view. For the most part it's working fine, but there seem to be some controls that, while they work correctly in OnInspectorGUI(), don't work correctly in OnSceneGUI().
The two controls I'm having trouble with are Popup() and ColorField(). It seems that with these controls, the selected value is not returned to the caller when the control is used in OnSceneGUI().
Here's the code I'm using to test this:
using UnityEditor; using UnityEngine;
[CustomEditor(typeof(MyScript))] public class MyScriptEditor : Editor { int selected = 0; Color color = Color.white;
void OnSceneGUI()
{
if (Event.current.type == EventType.Layout) {
HandleUtility.AddDefaultControl(
GUIUtility.GetControlID(FocusType.Passive));
}
Handles.BeginGUI();
selected = EditorGUILayout.Popup(
selected, new string[] { "0", "1", "2" });
color = EditorGUILayout.ColorField(color);
Handles.EndGUI();
}
}
The value returned is always the initial value (0 and Color.white, respectively), regardless of what's selected in the control.
Should these functions be expected to work in this context? Can anyone see any obvious problems with the example code above?
Answer by qJake · Sep 05, 2010 at 08:56 PM
I could be wrong, but I believe the only reason you can do OnSceneGUI()
in the editor is to catch the Gui Events which are accessible through the Event
class (i.e. Event.current.use
or something like that).
Additionally, the GUI functions in EditorGUI are primarily used for wizards and custom inspectors, and are not to be used to paint directly onto the scene view in Unity. Also, there is no identical "Popup" or "ColorField" component in the standard game GUI class, so I'm pretty sure Popup and ColorField are only supposed to be used within custom inspectors, editor windows, and wizards, and not in scene GUIs.
If you're trying to create a custom editor within Unity, it is highly recommended that you use a custom EditorWindow, and not paint directly onto the scene view, because, as you just found out, not everything is supported, and it's not really a "best practice" for Unity.
Thanks for your reply. In the documentation, at the very bottom of the page titled 'Extending the Editor', it states that if you want to use GUI controls in OnSceneGUI(), they should be wrapped in calls to Handles.BeginGUI()/EndGUI(). This would seem to suggest that GUI controls can in fact be used in the scene view. I certainly believe you about it not being best practice and/or being unsupported, but I haven't really seen that in the documentation. $$anonymous$$ay I ask where you're finding that information? ($$anonymous$$aybe I'm just looking in the wrong places.)
It's my opinion. Rarely do I see anyone using GUI controls on the Scene view, most everyone just creates a custom editor window. Not only is it a lot easier, but it supports ColorField and Popup.
Fair enough. Some of what you're saying seems to contradict what I've read in the documentation and on the forums, but I suppose in this case it may just come down to the fact that, empirically, the controls in question don't work correctly in the scene view. It'd be nice to know for sure whether you are or are not supposed to do this, and whether it is or is not supposed to work, but I guess I'll just have to settle for 'empirical' this time. Anyway, thanks for your replies - I appreciate it.
Imho its not always useful adding a new EditorWindow
for each feature. Isn't there a benefit having a GameObject inside the scene which gives you the ability to do Editor
operations through the Inspector
while also holding generated GameObjects
. Also the Inspector
view is opened most of the time so displaying additional information there makes sense! Also working with the SceneView
in the way that you can retrieve positions clicked on the view works only inside a CustomEditor
because OnSceneGUI
and why should it be implemented if it shouldn't be used. I already work with it and while you have to work a little bit different than in play mode its quite useful!
Answer by yoyo · Jan 05, 2011 at 05:28 PM
(Should be a comment, but I need code formatting.)
For a target component that adds GUI overlays, it's tempting to do this in your editor to make your scene view emulate your game view ...
public void OnSceneGUI()
{
Handles.BeginGUI();
target.OnGUI();
Handles.EndGUI();
}
This may work, but it's easy for your OnGUI method to start making assumptions and calls that are not supported in edit mode, so be careful.
Your answer
Follow this Question
Related Questions
Customizing the item rendering in a EditorGUILayout.Popup 0 Answers
How can I put a list of filenames into an editor menu? 2 Answers
EditorGUILayout.Popup() ignores repetitive Strings in the Array 1 Answer
Text box pop up on mouseover 1 Answer
Destroy Object then pop-up other small window question. 1 Answer