Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Jesse Anders · Sep 05, 2010 at 07:27 PM · editorguilayoutpopuponscenegui

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jesse Anders · Sep 05, 2010 at 10:20 PM 0
Share

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.)

avatar image qJake · Sep 06, 2010 at 01:47 AM 0
Share

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.

avatar image Jesse Anders · Sep 06, 2010 at 05:12 AM 0
Share

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.

avatar image ocimum · Nov 05, 2015 at 03:15 PM 0
Share

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!

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges