纳金网

标题: UGUI判断鼠标或手指(移动平台)是否点击在UGUI上 [打印本页]

作者: 烟雨    时间: 2015-5-30 06:34
标题: UGUI判断鼠标或手指(移动平台)是否点击在UGUI上

很多的时候我们要判断时候都点中UGUI

此时要加命名空间 using UnityEngine.EventSystem;

有的时候新建脚本并不能直接用 UGUI  此时也要加命名空间  using UnityEngine.UI;

接下来就是判断是否点击在UGUI 上了 ,上代码


//Windows 上

if (Input.GetMouseButtonDown(0))
            {
                Debug.Log(EventSystem.current.gameObject.name);
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Debug.Log("当前触摸在UI上");
            
                }
                else
                {
                  
                    Debug.Log("当前没有触摸在UI上");
               
                }
            }


//安卓上

  if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                {
                    Debug.Log("Hit UI, Ignore Touch");
               
                }
                else
                {
               
                    Debug.Log("Handle Touch");
                }
            }


   bool IsPointerOverGameObject(int fingerId)
    {
        EventSystem eventSystem = EventSystem.current;
        return (eventSystem.IsPointerOverGameObject(fingerId)
            && eventSystem.currentSelectedGameObject != null);
    }









欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5