您当前的位置: 首页 >  unity

Jave.Lin

暂无认证

  • 3浏览

    0关注

    704博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity - UGUI 判断鼠标、触点的点击是否在UI上

Jave.Lin 发布时间:2020-02-15 22:43:32 ,浏览量:3

Code
    // 当前输入是否在UI上
    public static bool InputOnUI()
    {
        if (Input.touchCount == 0)
        {
            return (EventSystem.current.IsPointerOverGameObject(-1));
        }
        else
        {
            foreach (var t in Input.touches)
            {
                if (EventSystem.current.IsPointerOverGameObject(t.fingerId))
                {
                    return true;
                }
            }
        }
        return false;
    }
	// 是否有任意的点击输入
    public static bool AnyPressInput()
    {
        if ((Input.touchSupported && Input.touches.Length > 0))
        {
            foreach (var t in Input.touches)
            {
                if (t.phase == TouchPhase.Began) return true;
            }
        }
        else
        {
            return Input.anyKeyDown;
        }
        return false;
    }
    //private static List raycastListHelper = new List();
    // 是否输入都在UI的位置
    public static bool AnyInputOnUI()
    {
        if (Input.touchSupported)
        {
            if (Input.touches.Length > 0)
            {
                foreach (var t in Input.touches)
                {
                    return EventSystem.current.IsPointerOverGameObject(t.fingerId);
                    // raycast方式会不管该UI能否接受触点,只要有RectTransform有交集就中,但可以使用CanvasGroup的Block Raycast去掉,就不会挡射线了
                    //raycastListHelper.Clear();
                    //EventSystem.current.RaycastAll(new PointerEventData(EventSystem.current)  // 但是Raycast也有弊端,要一直new PointerEventData会影响GC的
                    //{
                    //    position = t.position,
                    //}, raycastListHelper);
                    //return raycastListHelper.Count > 0;
                }
            }
        }
        else
        {
            if (Input.anyKeyDown)
            {
                return EventSystem.current.IsPointerOverGameObject(-1);
            }
        }
        return false;
    }
    // 有任意的鼠标或是触点输入,且在UI上
    public static bool AnyPressInputOnUI()
    {
        return AnyPressInput() && !AnyInputOnUI();
    }

先用简单的UGUI来制作测试(其实之前有用第三方UI解决方案:FairyGUI,但目前测试项目用不上)。

关注
打赏
1664331872
查看更多评论
立即登录/注册

微信扫码登录

0.0406s