c# - 如果玩家在空白处单击统一 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();
}
}
解决方案
检查 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();
}
}
推荐阅读
- html - 其他组件的 ScrollIntoView 不起作用(类型错误)
- php - 国际化路由不适用于安全访问控制
- javascript - 如何根据单击的元素在javascript中设置警报的内容?
- hash - 使用 MAD 的字符串哈希函数
- azure-functions - 绑定 SQL 查询 Azure Functions
- javascript - 没有使用 create-react-app 获得服务人员
- python - 定义定义另一个函数的函数时出错
- php - 在laravel中将一个表中的aa值分配给不相关的表
- python - Ubuntu 18.04,PyQt5,(不是 PySide2)conda,python:如何导入 QWebEngineWidgets?
- django - 在 django 的同一个查询集中返回两个不同的模型