unity里如何在点击UI时,避免触发游戏内操作。

如果在游戏中同时有点击UI和检测鼠标点击的操作时,要避免在点击UI时,避免触发游戏内操作。
用eventsystem去检测是否点到ui,方法如下:

if (Input.GetMouseButtonDown(0))
        {
    
    
            //试图获取当前点击时选择的物体
            GameObject btn = EventSystem.current.currentSelectedGameObject;
            if (btn == null)
            {
    
    
                Debug.Log("是游戏内操作");
            }
            else
            {
    
    
                Debug.Log("是UI操作");
            }
        }

猜你喜欢

转载自blog.csdn.net/farcor_cn/article/details/116268546