- Home /
Problem detecting Mouse Over Gui when scaling it with Matrix4x4
Hi, im having an issue detecting the Mouse pointer over a specific rect when scaling it on GUI. This is what im doing On GUI, and im trying to detect the mouse over gui on the TutoWindowsRect.
void OnGUI(){
GUI.skin = GUI_Skin;
Matrix4x4 oldMatrix;
Matrix4x4 tMatrix;
static float width = 2048; //Reference resolution
static float height = 1536; //Reference resolutio
oldMatrix = GUI.matrix; //Store current matrix
tMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1.0f*Screen.width/width, 1.0f*Screen.height/height, 1.0f)); //Construct matrix to scale to actual view size
GUI.matrix = tMatrix; //Set the GUI matrix to the scaling matrix
if (GUI.Button(new Rect(width - 96, 20,64,64), "", "TutoButton")){
if(tutoWindow == false){
tutoWindow = true;
}else{
tutoWindow = false;
}
}
if (tutoWindow == true){
TutoWindowRect = GUI.Window(1, TutoWindowRect, TutoWindow, "", "TutoWindow");
}
GUI.matrix = oldMatrix; //Set the original matrix back
}
void TutoWindow(int WindowID){
if (Time.time - _StartTimeTip > 13){
_StartTimeTip = Time.time;
int dado = Random.Range(0,7);
while (dado == _lastDado){
dado = Random.Range(0,7);
}
_lastDado = dado;
}
GUI.Label(new Rect(50,672,1500, 128),_intro[_lastDado], "Tips");
}
and here is what im using to detect the mouse over gui.
void Update(){
float multipX = (float)Screen.width/width;
float multipY = (float)Screen.height/height;
bool a = MouseOver2DGUI(new Rect(TutoWindowRect.x*multipX, TutoWindowRect.y*multipY, TutoWindowRect.width*multipX, TutoWindowRect.height*multipY));
Debug.Log(a);
}
bool MouseOver2DGUI(Rect rect){
Vector2 realMousePos = new Vector2 (Input.mousePosition.x, height - Input.mousePosition.y);
bool mouseOver = (rect.Contains(realMousePos));
return mouseOver;
}
The thing is that the boolean is giving me perfect results on x, width and height but not in the Y position of the rect. Maybe the logic im using for this is wrong somewhere i cannot find, i hope someone can help me, thank you very much.
Comment