Hi,
I have two GUI.Window.WindowID are 1, 2.
How do I know which Window is focusing?
Thanks.
Hi,
I have two GUI.Window.WindowID are 1, 2.
How do I know which Window is focusing?
Thanks.
Anybody please to help me this!
By either the window id or by which callback was executed. Since you assign both, itâs quite easy to determine which window was last clicked in.
This is covered in the documentation with examples to boot.
Hi Quietus,
When I draw two Windows then both callback functions are called, but the focusing is the last called.
My situation: I have two GameObjects, each will draw one GUI.Window with different WindowID. When I click Delete Key on keyboard, I want to know which Window is focusing in order to process with different task.
At your link, I donât see it can help me with this stuff. It is different from my situation.
Thanks.
There was a recent thread on how to trick Unity to get some kind of mousehover
you could attempt the same solution for this.
But above that, there is no functionality like that.
At simplest you just do it differently and much simpler:
Ensure each window is in its own script / class.
Put a bool variable in there.
Draw the window only if that variable is true
and then add close functionality where it is meant to be: into the window code
and close functionality just sets the bool to false
or another alternative
A global variable in some globally accessable context that holds a âlast windowâ reference. whenever a gui element is used in some way you update that reference to hold the current window handle
Ok so youâre not actually driving focus by clicking on an element within the window.
If you look at the bottom of the documentation it states that one cannot count on the window functions to be called in any particular order. If they overlap, the front most window will be called last and that callback should be the one that has focus.
So if youâre not clicking in the window and they donât overlap Iâm not sure how you would go about determining focus, or that it can be done at all.
Thank Quietus and dreamora.
I found the way can resolve this.
As dreamora wrote, I use a global variable as focusing WindowID (for example: m_focusingID). Then each window function callback, I just only write:
if ((Event.current.button == 0) (Event.current.type == EventType.MouseDown)) {
m_focusingID = windowID;
}
And it works!!!. At first, I think this code ALWAYS set m_focusingID = windowID. But I am suprised, it only run when you click on Window zone(window area), and position that Mouse returns is position in Window zone.
Thanks!
Thanks guys for sharing your solution!
There is another (simpler) way. You will find that if you use âMouseUpâ unity will automatically know which Window you are inâŚ
For example, if you have:
void SomeWindow(int id){
...
if ((Event.current.button == 0) (Event.current.type == EventType.MouseUP)) {
//some stuff
}
}
inside your SomeWindow(int id) function you can handle coding specific to that window. You could also set a flag to remember the last window to be accessed. The key here is the MouseUp as MouseDown will not work (you canât have it all)âŚ
But how can you know if no window is selected? Cause now the last window will stay registered as active. And will not be setted to null.
any ideas?
Heres out I determine the focused window, it is dark magic of reflection because GUI does have an internal static member for the current focusedWindow.
Be careful with this code if you use it if they change the static fields name it will break;
Wow PaulAsh tanks soo much! This works perfectly and saves me so much time! I like the black voodoo, me want more!
Awesome work
Wrote same hack-fu reflection code day ago. And do u know what? It does not works in webplayer, due security restrictions of private and internal variables acces.
Why are âfocusedWindowâ not public? I see absolutely no particular reason fo that. IMGUI sucks all way ahead, but Unity realization of it sucks even more. I hope NEWGUI⢠will be OOP, and done completely not in Unityâs best traditions.
I wrote a ton of reflections myself and it all works in the browser.
Iâm not sure about this particular example, since it tries to reflect something from âUnity classâ.
btw Iâm interested if the reflection above works on iOS?
ThanksâŚ
Very Awsome Indeed!!! Works Great!!!
Sadly this is now broken in 4.x - anyone resolved this for 4.x?
Bump? Any solutions for Unity 4?
also interested ff anyone resolved rhis for 4.x
I donât have Unity 4 installed, but if you open your Unity 4 project in MonoDevelop you can right click on the GUI class (anywhere you use it, for example the GUI.Window(âŚ) call) and âGo to declarationâ to open the GUI class in the Assembly Browser. This will show you all the public and private fields, properties and methods, so you can see if there is a new field along the lines of the old âfocusedWindowâ.
Note that for the Assembly Browser to find the Unity DLLâs you need to add the right path (e.g. âC:/Program Files(x86)/Unity/Editor/Data/Managedâ) to your MonoDevelop > Options > Preferences > Build >Assembly Folders setting.