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
1
Question by vexe · Feb 01, 2014 at 03:56 AM · guieditorwindowcompilingcompilation

"You can only call GUI functions from inside OnGUI." Each time I try to open a window after a recompilation when the window was opened?

This is really annoying - Say I have a EditorWindow opened - and then make something that triggers a recompilation - I start to get "You can only call GUI functions from inside OnGUI." about my window (also for each time I try to open it from this moment on). The only way to fix this is to close the window and then trigger a recompilation again.

Why does this happen? -__-

Thanks!

EDIT: Code

 using UnityEngine;
 using System.Collections.Generic;
 using UnityEditor;
 using System.Linq;
 using System.Text.RegularExpressions;

 public class SelectSceneWindow : EditorWindow
 {
     /* a bunch of GUIStyles... */

     private SceneTransition transition;
     private Object[] scenes;
     private Object[] filteredResult;

     public static void Show(SceneTransition transition)
     {
         var window = CreateInstance<SelectSceneWindow>();
         window.Init(transition);
         window.ShowUtility();
     }

     public void Init(SceneTransition transition)
     {
         this.transition = transition;
     }

     Vector2 scrollPosition;
     string search = "";
     private const float INDENT_WIDTH = 20f;

     void OnGUI()
     {
         GUIHelper.HorizontalBlock(() =>
         {
             GUILayout.Label("Scenes", ScenesLabel);
             search = EditorGUILayout.TextField("", search);
             filteredResult = scenes.Where(s => Regex.IsMatch(s.name, search, RegexOptions.IgnoreCase)).ToArray();
             if (GUILayout.Button(new GUIContent("↶", "Reload scenes"), RefreshButton, GUILayout.Width(20), GUILayout.Height(20)))
                 ReloadScenes();
         });

         scrollPosition = IUnifiedGUIHelper.ScrollViewBlock(scrollPosition, false, false, () =>
         {
             foreach (var scene in filteredResult) {
                 bool selected = scene.name == transition.scene;
                 var nextStyle = GetNextStyle();
                 IUnifiedGUIHelper.HorizontalBlock(selected ? SelectedStyle : nextStyle, () =>
                 {
                     GUILayout.Space(INDENT_WIDTH);
                     var spaceRect = GUILayoutUtility.GetLastRect();
                     GUILayout.Label(scene.name, selected ? SelectedLabel : UnselectedLabel);
                     var labelRect = GUILayoutUtility.GetLastRect();
                     var buttonRect = IUnifiedGUIHelper.CombineRects(spaceRect, labelRect);
                     if (!selected) {
                         EditorGUIUtility.AddCursorRect(buttonRect, MouseCursor.Link);
                         if (GUI.Button(buttonRect, "", GUIStyle.none)) {
                             transition.scene = scene.name;
                         }
                     }
                 });
             }
         });
     }
     void ReloadScenes()
     {
         scenes = Utils.GetAssets<Object>("Assets", "*.unity", SearchOption.AllDirectories);
         filteredResult = scenes;
     }

     void OnEnable()
     {
         ReloadScenes();
     }
 }

What it looks like

alt text

fsm.png (36.4 kB)
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

1 Reply

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

Answer by Bunny83 · Feb 01, 2014 at 04:05 AM

You probably using some GUI stuff in OnEnable or some other callback. However, like the error tells you, you can only use GUI stuff inside OnGUI. Without seeing your code nobody can tell you what you've done wrong since all we know is what you've written here.

Feel free to **edit** your question and include more information.

Comment
Add comment · Show 3 · 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 vexe · Feb 01, 2014 at 04:21 AM 0
Share

Added details.

avatar image Bunny83 · Feb 01, 2014 at 04:30 AM 0
Share

Do you create your "bunch of GUIStyles" in the field initializers of your class? That's most likely the cause of your errors. You should initialize the GUIStyles inside OnGUI like this.

Can you add one or two example GUIStyles? Thanks

avatar image vexe · Feb 01, 2014 at 04:33 AM 0
Share

Oh so it must be the same problem again - I see. Thanks.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get Mouse Events with EditorWindow 3 Answers

Can not close editor window not in GUILayout.somemethod in OnGUI method 0 Answers

How to trigger button press on a button in an editor window by pressing the return key ? 0 Answers

Using Handles in EditorWindow 0 Answers

EditorWindow: Scrollview 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