I have a Texture that simulates a button so I use the Rect.Contains function to perform the onMouseOver and so on. The thing is that when I draw the buttons I use the call GUI.Matrix to make all the buttons relative to the screen resolution. Since here everything well but when I call the Rect.Contains it seems like the size and position of the button keeps being the absolute and not the relative. What could I do to fix this?
GUI.matrix applies to all the internal rectangles used within GUI functions (including mouse overs, etc.). However, it does not affect the Rect you use in your script. So, if you do any calculations with the Rect in your script, you must make sure to apply the matrix to it first.
First try and found out if the internal functionality of transforming rectangles is exposed in the Unity API. It doesn’t appear to be (at least not in the logical places), but maybe you get lucky.
If it doesn’t exist, implement it yourself:
Applying a matrix to a Rect means getting the four corner points of your Rect and multiplying them with the GUI matrix. Then:
Either construct a new Rect from the min and max values of your transformed points (if you don’t particularly care about rotations)
Or create a new Contains function for a non-axis-aligned rectangle (if you do care about correct behaviour under rotation).
Thakns for the reply
I did what you said and seems to work. But instead of that I was calculating the screen factor:
float fScreenFactor = Screen.height / 768.0f;
and multiplying it to the x and y of my rect and I get the same result as multiplying with the GUI.matrix. I guess is the same idea but not having into account the rotation. However if I had rotation on my GUI I could apply to my x and y a sine and cosine operation to rotate their positions… Am I correct?
Yes, you are correct. Although I’d personally find the matrix version a lot easier than having to think about sin and cos, but that’s probably just me :P.