美文网首页
2020-04-08 Unity是否点击在UI层

2020-04-08 Unity是否点击在UI层

作者: VECTOR_Y | 来源:发表于2020-04-08 15:07 被阅读0次
        public static bool IsPointerOverUIObject()
        {
            PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    #if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IPHONE)
            eventDataCurrentPosition.position = Input.GetTouch(0).position;
    #else
            eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    #endif
    
            List<RaycastResult> results = new List<RaycastResult>();
            if (EventSystem.current)
            {
                EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
            }
    
            for (int i = 0; i < results.Count; i++)
            {
                if (results[i].gameObject.layer == LayerMask.NameToLayer("UI"))
                    return true;
            }
    
            return false;
        }
    

    相关文章

      网友评论

          本文标题:2020-04-08 Unity是否点击在UI层

          本文链接:https://www.haomeiwen.com/subject/klytmhtx.html