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
0
Question by Bovine · Oct 28, 2011 at 04:11 PM · editorwindowactivefocusselectedguilayout.window

How can I determine which GUILayout.Window was last clicked?

Hi All

I have an EditorWindow for my behaviour tree implementation that draws Window objects for each node. I need to know which node was selected/clicked last so I can create the necessary controls for the details of that control. I've seen that you can store the ID in your DoWindow method, but this doesn't seem to always be the last window drawn - which is a shame.

Any suggestions?

Thanks Bovine

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 Bovine · Oct 28, 2011 at 07:02 PM

Okay,

I came across this thread here:

http://forum.unity3d.com/threads/18109-how-to-get-GUI.Window-focus

It seems that in the editor, the Event.current property only returns Repaint and Layout for me. However the black magic suggested by PaulAsh does work and I've refactored this below:

    static System.Reflection.FieldInfo s_focusedWindow = null;
 
    static public int FocusedWindow
    {
       get
       {
          if(s_focusedWindow == null)
          {
             s_focusedWindow = typeof(GUI).GetField("focusedWindow", 
                                     System.Reflection.BindingFlags.NonPublic | 
                                     System.Reflection.BindingFlags.Static);
          }
    
          return (int) s_focusedWindow.GetValue(null);
       }
    }

It returns -1 if there's no focus and the window ID for the item with focus if one has been clicked. It also reverts to -1 if you click away from a window.

I'm using this in the editor and I have to say I don't much like it, but.... it does work and work well. However, I have to wonder why on earth it is not public API - it's such a useful property!

I also looked at Input.GetMouseButtonUp() but wasn't terribly surprised to see this not work either.

If anyone else has a more acceptable solution, then please post it and I'll accept that as the answer instead, but in the short term it works so well and I don't have another two hours to spend googling the subject.

Let's hope they don't break it! :)

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
avatar image
1

Answer by Jerdak · Oct 28, 2011 at 04:52 PM

Not sure if this is related but I manage all windows in a WindowManager class. Whenever a script instantiates a window it first registers that Window's rect with the WindowManager and the manager sends back the unique ID to use when creating the actual Window.

After that I do a check every few Update()s to see if the mouse cursor is inside any of the Window rects. WindowManager also does a test for leftmouseclick(), if detected and the mouse is over a window, it logs the ID of that window as lastClicked.

It's a touch overkill and probably not efficient but my use case was limited to 1 <= N <= 10 windows so the cursor-in-window test was quick.

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 Bovine · Oct 28, 2011 at 06:17 PM 1
Share

So I know the rect of each node and i was thinking of doing this but this is an EditorWindow and it isn't clear to me how to detect a mouse click. I tried the Event class but it wasnt firing for mouse down - ill try logging to see if any events fire. Of course z-order is another potential problem for overlapping windows.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

EditorWindow.Update without Unity having focus 1 Answer

BUG REPORT: Editor Docked windows do not change focus properly. 0 Answers

EditorWindow.ShowPopup is not focused 0 Answers

Immediate editor inside unity 2 Answers

Creating Editor 2D Animation Preview 2 Answers


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