unity-game-engine - Unity触摸和ui按钮

标签 unity-game-engine

Image

就像上图一样,我们有一个模型和它前面的 Canvas 。它的 footer 有碰撞器,我还需要按钮中的 onclick 事件。我想要的是当用户单击按钮时游戏对象没有任何 Action ,但现在很难单击按钮。

下面是我的代码:

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();
        }
    }

有人有什么想法吗?

最佳答案

我认为当您触摸 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 
       }
      } 
     }
    }

关于unity-game-engine - Unity触摸和ui按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41560432/

相关文章:

c# - Unity 检查器不将 XML 文档 (<summary>) 显示为工具提示

c# - 不知道如何获得敌人的生命值

c# - Unity - 从资源文件夹中读取文本文件

c# - 当所有其他对象暂停时移动角色

c# - MongoDB 的单例

firebase - Unity 的本地通知(Firebase?)

c# - 各地区的数制问题

c# - 将 nhibernate 与 Unity3D 集成

c# - 在 Unity 中通过代码创建枢轴点

android - 获取android : Unity3d的音量大小