Unity IsPointerOverGameObject的一個(gè)失靈問題

需求

  • 如果希望點(diǎn)擊一個(gè)物體但是希望隔離UI,當(dāng)點(diǎn)擊到UI時(shí)不響應(yīng)物體的事件,那么一般的做法都是使用Unity自帶的api IsPointerOverGameObject來判斷是否點(diǎn)擊到UI,代碼如下
        if (Input.GetMouseButtonDown(0))
        {
            IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
            if (IsOverGameObject)
            {
                Debug.Log("Click the UI");
                return;
            }

            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
            {
                if (null != hit.collider)
                {
                    Debug.LogError(hit.collider.name);
                }
            }
        }

解析

  • 但是這個(gè)代碼實(shí)際上是有問題的,比如當(dāng)離開unity編輯器窗口,然后再次點(diǎn)擊游戲內(nèi)對應(yīng)物體,會發(fā)現(xiàn)可以打印出點(diǎn)擊物體的名字,之后再點(diǎn)擊則是被IsPointerOverGameObject隔離了。但是我們需要的是無論什么時(shí)候都要被IsPointerOverGameObject隔離,順著IsPointerOverGameObject查看源碼,會發(fā)現(xiàn)如下代碼
        public override bool IsPointerOverGameObject(int pointerId)
        {
            var lastPointer = GetLastPointerEventData(pointerId);
            if (lastPointer != null)
                return lastPointer.pointerEnter != null;
            return false;
        }

關(guān)鍵在于lastPointer.pointerEnter != null這句代碼,如果在自己的UI上掛腳本并實(shí)現(xiàn)以下代碼

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("OnPointerEnter");
    }

會發(fā)現(xiàn)在上面有問題的代碼下,從其他地方重新點(diǎn)擊到unity游戲窗口,并點(diǎn)擊物體,物體名字的log在OnPointerEnter log的前面,所以驗(yàn)證了unity內(nèi)部判斷是否點(diǎn)到ui實(shí)際上是判斷是否OnPointerEnter了UI,但是遺憾的是這個(gè)順序是不固定的。所以一旦是不固定的順序,那么IsPointerOverGameObject在某些情況下會失靈。

解決

  • 一個(gè)簡單的解決方案是在GetMouseButtonUp時(shí)再次檢測一遍,這次確保unity檢測到了進(jìn)入U(xiǎn)I,代碼如下
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
            if (IsOverGameObject)
            {
                Debug.Log("Click the UI GetMouseButtonDown");
                return;
            }
        }

        if (Input.GetMouseButtonUp(0))
        {
            IsOverGameObject = EventSystem.current.IsPointerOverGameObject() || IsOverGameObject;
            if (IsOverGameObject)
            {
                Debug.Log("Click the UI GetMouseButtonUp");
                return;
            }

            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
            {
                if (null != hit.collider)
                {
                    Debug.LogError(hit.collider.name);
                }
            }
        }
    }

再次運(yùn)行游戲就發(fā)現(xiàn)第一次隔離UI是被GetMouseButtonUp捕捉到了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容