首页 > 解决方案 > 如果玩家在空白处单击统一 2d,则播放

问题描述

我正在为移动设备制作 2d 游戏,并在画布上创建了一个暂停按钮。问题是当我点击暂停按钮时,玩家会射击。我希望有一种方法可以在触摸 UI 元素时阻止镜头。有什么简单的方法可以做到这一点吗?

这是我为检测点击而编写的代码:

if(Input.touchCount > 0)
        {
            touch_ = Input.GetTouch(0);
            
            if(touch_.phase == TouchPhase.Began)
            {
                touching = true;
                touch_began_fun();
            }
            if(tocando)
            {
                toching_fun();
            }
            if(touch_.phase == TouchPhase.Ended)
            {
                touching = false;
                touch_ended_fun();
            }
        }

标签: c#unity3d2d

解决方案


检查 EventSystem 当前选中的游戏对象是否为空。如果它不为 null,则触摸在 UI 元素上。

if(Input.touchCount > 0)
{
    touch_ = Input.GetTouch(0);

    if(touch_.phase == TouchPhase.Began && EventSystem.current.currentSelectedGameObject == null)
    {
        touching = true;
        touch_began_fun();
    }
    if(tocando)
    {
        toching_fun();
    }
    if(touch_.phase == TouchPhase.Ended)
    {
        touching = false;
        touch_ended_fun();
    }
}

推荐阅读