[英]Input.GetPointerDown instead of Input.GetMouseButtonDown?
[英]Unity - Input.GetMouseButtonDown
我創建了一個腳本,通過點擊智能手機顯示屏,一個對象被激活。 我使用Input.GetMouseButtonDown(0)
來檢查是否在顯示屏上進行了點擊。 該腳本工作正常,但我有一個問題:如果我在場景中插入一個按鈕,單擊該按鈕我會同時收到該按鈕的單擊和Input.GetMouseButtonDown
函數的單擊。 我怎么能“分開”兩次觸摸?
// Example
void Update()
{
if (Input.GetMouseButtonDown(0))
myObject.SetActive(true);
}
public void PauseButton()
{
// Open Pause Panel
}
我正在考慮刪除“Input.GetMouseButtonDown (0)”並使用一個不可見的按鈕並在我需要的顯示部分上調整它的大小。
將標簽添加到您的 UI 按鈕,例如: "UI"
下面的函數告訴點擊是否在一個用 UI 標簽標記的 UI 對象上,所以你可以檢查你的屏幕點擊函數是否應該觸發你的事件
public static class InputHandler
{
public const string CompareTagForUIElements = "UI";
public static bool IsPointerOverUIObject(int touchId)
{
var eventDataCurrentPosition = new PointerEventData(EventSystem.current);
// I'm using touch since you are talking about smartphones,
// if you still need click use Input.mousePosition
eventDataCurrentPosition.position = new Vector2(GetTouch(touchId).position.x, GetTouch(touchId).position.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
for (int i = 0; i < results.Count; i++)
{
if (results[i].gameObject.CompareTag(CompareTagForUIElements))
return true;
}
return false;
}
}
所以而不是
public void ITappedOnScreen()
{
if(Input.GetMouseButtonDown(0))
Debug.Log("Screen tapped");
}
用
public void ITappedOnScreen()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Begin &&
!InputHandler.IsPointerOverUIObject(0))
Debug.Log("Screen tapped");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.