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 crushy · Jul 10, 2012 at 04:33 AM · guieditorwindowwindowclose

Close()ing an EditorWindow

While trying to make a (rather complex) editor for a component, I decided that, instead of trying to cram everything into a custom inspector, to just make a simple one that creates a custom EditorWindow, like this:

 function OnInspectorGUI () {
  if (GUILayout.Button("Open Dialogue Editor")) {
    var window:DialogueEditor = EditorWindow.GetWindow(DialogueEditor);
       window.SetTarget(target);
       window.Init();
  }
 }

And use this window to edit the target. However, if the target is deleted and the window is still open, it should close itself. For that purpose I wrote the following at the start of the window's OnGUI():

 if (!target) {
   this.Close(); 
   return;
 }

However, if I test it out by simply not setting the target, I get the following errors:

 NullReferenceException: CalculateNextFromHintList accessing GUI state from destroyed monobehaviour
 UnityEngine.IDList.GetNext (Int32 hint, FocusType focusType, Rect position) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:66)
 UnityEngine.IDList.GetNext (Int32 hint, FocusType focusType) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:59)
 UnityEngine.GUIUtility.GetControlID (Int32 hint, FocusType focus) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:254)
 UnityEngine.GUI.Box (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:215)
 UnityEditor.DockArea.OnGUI () (at C:/BuildAgent/work/14194e8ce88cdf47/Editor/Mono/GUI/DockArea.cs:664)

And the slightly more intuitive

 GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly.

Am I doing something wrong? This seems to be all fine according to the Reference and the examples they've given.

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
2
Best Answer

Answer by delstrega · Jul 10, 2012 at 06:31 PM

Move that piece of code to the window's update function and you should be fine.

Comment
Add comment · Show 2 · 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 crushy · Jul 12, 2012 at 10:42 PM 0
Share

Sorry for the late reply but won't this have a severe impact on performance? Not that it doesn't work...

avatar image Bunny83 · Jul 12, 2012 at 11:10 PM 0
Share

No, the Update is only called when something changes. The Editor doesn't redraw itself 60 times per sec ;) only when it's necessary.

An alternative could be to call GUIUtility.ExitGUI(); Which should ter$$anonymous$$ate the current GUI redraw cycle. This should also be used in the inspector to avoid warnings. See

this post

But generally it's better to execute it from outside of OnGUI, or atleast in the repaint step. The same rule applies to changes to the GUI elements (adding / removing elements). Never change such a condition in the layout step. The layout and the repaint step have to be exactly the same.

avatar image
2

Answer by The-Arc-Games · Jul 10, 2012 at 07:32 AM

What happens is that when target is not set, the first OnGUI call is used to close the window, which was generated in the same iteration.

You basically need to change your window condition into something like:

  if (GUILayout.Button("Open Dialogue Editor") && target != null) 
Comment
Add comment · Show 1 · 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 crushy · Jul 10, 2012 at 06:20 PM 0
Share

Even if I use that code or make sure it never closes the window on the first OnGUI call, I still get the same errors.

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

7 People are following this question.

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

Related Questions

Converting from EditorWindow to runnable window 1 Answer

Close (X) button 1 Answer

Unity editor creates a lot of default objects. Why? 0 Answers

GUI Window problem 2 Answers

GUI Window on GameObject location? 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