[英]How to detect a mouse clicked on a UI element
我正在嘗試在我的游戲中制作一個工具欄系統,但我無法找到一種方法來僅檢測 UI 元素上的鼠標單擊或檢測它是否超過它,常規檢測系統無法正常工作。
我試過:
void OnMouseDown()
{
Debug.Log(“yay”);
}
但該消息從未被記錄。
您可以在 UI 元素上使用 EventSystems 接口:
例如在你的情況下:
public class test : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Click");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Enter");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Exit");
}
}
確保你的場景有一個 EventSystem 來讓它工作。
UI可以通過上面鏈接中的方法來區分。 您可以像下面這樣使用它。
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked UI");
}
else
{
Debug.Log("Clicked Not UI");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.