Unity UI丨判断当前点击的是不是UI、获得当前点击的UI物体

 获取当前点击的是不是UI

    void Update()
    {
        //场景中必须有EventSystem
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject() || GUIUtility.hotControl != 0)
            {
              Debug.Log("单击到了UI");
            }
            else
            {
              Debug.Log("没有单击到UI");
            }
        }
    }

 

 

获得当前点击的UI物体

    /// 
    /// 获得当前点击到的UI物体
    /// 
    public GameObject Skode_GetCurrentSelect()
    {
        GameObject obj=null;

        GraphicRaycaster[] graphicRaycasters = FindObjectsOfType();

        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;
        List list = new List();

        foreach (var item in graphicRaycasters)
        {
            item.Raycast(eventData, list);
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    obj = list[i].gameObject;
                }
            }
        }

        return obj;
    }

 

你可能感兴趣的:(#,UGUI)