Unity判断点击的UI对象

Posted DaLiangChen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity判断点击的UI对象相关的知识,希望对你有一定的参考价值。

 /// <summary>判断点击的UI对象</summary>
    public GameObject getOverUI()
    
        PointerEventData pointerEventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        gr.Raycast(pointerEventData, results);
        if (results.Count != 0)
        
            return results[0].gameObject;
        
        return null;
    

以上是关于Unity判断点击的UI对象的主要内容,如果未能解决你的问题,请参考以下文章