Unity 和 C# click/touch 超过特定 UI 元素检查
Unity and C# click/touch over specific UI element check
我有一个代码,如果我开始单击或触摸屏幕并拖动特定距离,就会执行某些操作。我想检查它何时超过特定区域(可以说超过 UI,比如 canvas)。 主要目标是让屏幕的上半部分对点击和触摸做出反应。
我试图通过创建新的 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();
}
欢迎任何想法,谢谢...
您在屏幕底部启动 Rect,因此它位于下半部分。
Parameters
x The X value the rect is measured from.
y The Y value the rect is measured from.
width The width of the rectangle.
height The height of the rectangle.
以下应该适合您:
Rect bounds = new Rect(0, Screen.height, Screen.height / 2, Screen.width);
我有一个代码,如果我开始单击或触摸屏幕并拖动特定距离,就会执行某些操作。我想检查它何时超过特定区域(可以说超过 UI,比如 canvas)。 主要目标是让屏幕的上半部分对点击和触摸做出反应。
我试图通过创建新的 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();
}
欢迎任何想法,谢谢...
您在屏幕底部启动 Rect,因此它位于下半部分。
Parameters
x The X value the rect is measured from.
y The Y value the rect is measured from.
width The width of the rectangle.
height The height of the rectangle.
以下应该适合您:
Rect bounds = new Rect(0, Screen.height, Screen.height / 2, Screen.width);