【问题标题】:Unity touch and ui buttonunity touch 和 ui 按钮
【发布时间】:2017-01-10 18:57:09
【问题描述】:

就像上图一样,我们前面有一个模型和一个画布。它在脚上有对撞机,我还需要按钮中的 onclick 事件。我想要的是当用户单击按钮时游戏对象没有动作,但现在很难单击按钮。

下面是我的代码:

if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        EventSystem es = EventSystem.current;
        Debug.Log("IsPointerOverGameObject === " + es.IsPointerOverGameObject(Input.GetTouch(0).fingerId));
        Debug.Log("currentSelectedGameObject === " + es.currentSelectedGameObject);
        if (!(es.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && es.currentSelectedGameObject != null))
        {
            Debug.Log("Handle touch === ");
            HandleTouchEvent();
        }
    }

有人有什么想法吗?

【问题讨论】:

  • 它们只是按钮吗?
  • @TimCooley 是的,ScrollRect 中的按钮

标签: unity3d unity5


【解决方案1】:

我认为当您触摸gameobject 时,您的 UI 和其他游戏对象都会受到影响,因此您应该排除用户使用此类 UI 元素的时间,

 public class TouchExample : MonoBehaviour {
  void Update () 
    {
    // Check if there is a touch
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
      {
       // Check if finger is over a UI element 
        if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
       {
    Debug.Log("UI is touched");
    //so when the user touched the UI(buttons) call your UI methods 
       }
    else
       {
    Debug.Log("UI is not touched");
   //so here call the methods you call when your other in-game objects are touched 
       }
      } 
     }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多