- Home /
Incorrect mouse-over behavior using GUI.Window, after scaling UI by changing GUI.matrix
Hello,
When a user switches to a screen resolution, lower than a predefined value, we scale down the whole UI using GUI.Matrix. Everything shows up fine, but when GUI.Window is used, gui elements with Hover style detect mouse incorrectly. Also Rect.Contains(...) starts working incorrectly.
Setting the matrix:
Vector3 scale = Vector3.zero;
scale.z = 1;
scale.y = Screen.height < 1024 ? Screen.height/1024 : 1;
scale.x = scale.y;
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
This started happening after upgrading from Unity 4.1.0 to 4.2.0. I noticed there is a new GUI.ModalWindow from 4.1.2, which when used, does not produce the above problems as GUI.Window. However using GUI.ModalWindow is not always possible.
This seems like a big problem, especially for Unity games run on game portals, where the resolution is really low by default.
Anyone bumbed into this? Thanks in advance
The behaviour you describe is the one I am used to. I've no experience of this working in 4.1.0. You're changing a matrix used to render the GUI, so it's expected that the mouse events cease being aligned. If it was my code I'd just apply the scale in the calls into the GUI functions, so your rects etc get computed using the scale factor.
I just dont understand what you mean grahamdunnett.
I have the same problem, as many users. I still dont know which version of unity has the solution for this, but i'm not wishing to update because last time i did i had a problem with versions everytime i compiled (i'm using unity free).
Anyway, what i'm doing is scaling the matrix before any GUI element is drawn, so every rect is changed to fit the new screen resolution, the call to GUI.matrix is done in the first line of OnGUI function. So your last sentence does not make sense to me, because GUI rects are already computed with the scale factor. I mean, it works great. However when you pass your mouse over the buttons you notice the area where the button state changes to "On Hover" is displaced.
Am i getting you wrong?
Answer by boudinov · Feb 03, 2014 at 01:15 PM
The problem is the inconsistent behaviour of different API functions. For example, as Grahan mentioned, you can apply the scale in calls to Rect.Contains functions, alright. On the other hand, GUI.Button works fine without modifying anything - clicks on the button are captured correctly. However, using GUIStyle with normal/hover texture backgrounds, does not work correctly, and you have none control over this.
And it seems that in version 4.3, this is all fixed and everything works as expected. Have to double check that though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130516im_/https://answers.unity.com/themes/thub/images/avi.jpg)