ugui点击穿透判断

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ClickTest : MonoBehaviour {

    // Use this for initialization
    void Start () {
        
    }

    // Update is called once per frame
    void Update()
    {

        //Debug.Log(EventSystem.current.gameObject.name);
        if (Input.GetMouseButtonDown(0))
        {
            if (CheckGuiRaycastObjects()) return;

            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit.collider != null)
            {
                Debug.Log("点击");
            }
        }
    }

    /// <summary>
    /// 基于ugui的eventsystem判断是否有ui遮挡
    /// </summary>
    /// <returns></returns>
    bool CheckGuiRaycastObjects()
    {
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;

        List<RaycastResult> list = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, list);
        //Debug.Log(list.Count);
        return list.Count > 0;
    }
}

 

posted @ 2019-08-19 13:19  三页菌  阅读(315)  评论(0编辑  收藏  举报