[英]Unity and C# click/touch over specific UI element check
I have a code where if I start click or touch the screen and drag for specific distance does something. 我有一个代码,如果我开始单击或触摸屏幕并拖动特定距离,它会执行某些操作。 I would like to check whenever it is over a specific area (lets say over UI like canvas).
我想检查它何时位于特定区域上(让我们说说UI,例如canvas)。 The main goal is to have the upper half of the screen to react on click and touch.
主要目标是让屏幕的上半部分对单击和触摸做出反应。
I tried to do so by creating new Rect. 我试图通过创建新的Rect来做到这一点。 This works, but I can`t make the rect on upper part of the screen (right now it is on the lower part of the screen).
这可以,但是我不能在屏幕的上部制作矩形(现在它在屏幕的下部)。 I might be missing something, but the following code should create rect in the upper part of the screen, not ?
我可能会丢失一些东西,但是下面的代码应该在屏幕的上部创建rect,不是吗?
if (Input.GetMouseButtonDown(0))
{
Rect bounds = new Rect(0, 0, Screen.height / 2, Screen.width);
if (Input.GetMouseButtonDown(0) && bounds.Contains(Input.mousePosition))
{
Debug.Log("Touchableee!");
TouchableArea = true;
}
if (TouchableArea == true)
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
TouchableArea = false;
}
}
else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
Any ideas are welcome, thank you... 任何想法都欢迎,谢谢...
You're starting the Rect at the bottom of the screen, thus why it's on the bottom half. 您将在屏幕的底部启动Rect,因此为什么它在屏幕的下半部分。
Parameters
参数
x The X value the rect is measured from.
x测量矩形的X值。
y The Y value the rect is measured from.
y测量rect的Y值。
width The width of the rectangle.
width矩形的宽度。
height The height of the rectangle.
height矩形的高度。
The following should work for you: 以下应该为您工作:
Rect bounds = new Rect(0, Screen.height, Screen.height / 2, Screen.width);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.