检测一定范围内的光标
Detecting cursor in a certain range
我正在尝试找出一种方法来检测光标在特定范围内的位置。这就是我正在寻找的东西:
if ('bla bla bla' && Input.mousePosition == (in between x1 and x2, in between y1. and y2))
这在unity中可行吗,因为我想不通:(
感谢您的帮助!
Vector2 mousePos = Input.mousePosition;
这个returns一个Vector2
鼠标位置坐标x
和y
。检查坐标为mousePos.x
和mousePos.y
的点是否在x1
和x2
范围内; y1
和y2
,我们可以写成
if((mousePos.x >= x1 && mousePos.x <= x2) &&
(mousePos.y >= y1 && mousePos.y <= y2))
{
// do something
}
或者,
if(mousePos >= new Vector2(x1, y1) &&
mousePos <= new Vector2(x2, y2))
{
// do something
}
您也可以使用 Rect.Contains
来防止重复:
var InRect = new Rect(0, 0, Screen.width/2, Screen.height).Contains(Input.mousePosition);
UnityEngine.Debug.Log(InRect);
我正在尝试找出一种方法来检测光标在特定范围内的位置。这就是我正在寻找的东西:
if ('bla bla bla' && Input.mousePosition == (in between x1 and x2, in between y1. and y2))
这在unity中可行吗,因为我想不通:( 感谢您的帮助!
Vector2 mousePos = Input.mousePosition;
这个returns一个Vector2
鼠标位置坐标x
和y
。检查坐标为mousePos.x
和mousePos.y
的点是否在x1
和x2
范围内; y1
和y2
,我们可以写成
if((mousePos.x >= x1 && mousePos.x <= x2) &&
(mousePos.y >= y1 && mousePos.y <= y2))
{
// do something
}
或者,
if(mousePos >= new Vector2(x1, y1) &&
mousePos <= new Vector2(x2, y2))
{
// do something
}
您也可以使用 Rect.Contains
来防止重复:
var InRect = new Rect(0, 0, Screen.width/2, Screen.height).Contains(Input.mousePosition);
UnityEngine.Debug.Log(InRect);