当前位置:网站首页>How unity makes the UI intercept click events

How unity makes the UI intercept click events

2022-06-25 13:17:00 acgCode

stay Unity It is often necessary to make pop-up windows during game development . Pop up creation in Canvas in , Have a lot of UI Components can be used .

In the actual use of the process found , The pop-up window cannot intercept the mouse click event .Canvas hinder GameObject If there are bound collider components and triggers , Click on the event to penetrate UI Behind the trigger GameObject Click events for .

The official has provided testing methods , I will explain it in detail below :

First, you need to introduce namespaces before the code .

using UnityEngine.EventSystems;

After the GameObject Add a judgment to the mouse click event :

private void OnMouseUpAsButton()
  {
    
    if (EventSystem.current.IsPointerOverGameObject()) {
    
      return;
    }
    // TODO  The logic to be executed later 
  }

It's on it PC End of the judgment , The mobile terminal should write this :

private void OnMouseUpAsButton()
  {
    
    //  Disable multi touch 
    if (Input.touchCount == 1 && EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) {
    
      return;
    }
    // TODO  The logic to be executed later 
  }

such GameObject There is one ahead UI When , The subsequent logic will not be executed , Thus, a mouse click event is blocked .

Of course, if game logic and interactive judgment are written in Update Words in Li , This judgment can also be added to Update in , So as to play the role of interception . Introduce as needed .

原网站

版权声明
本文为[acgCode]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200525483837.html