我知道这是一个常见问题,但没有一个有效。可能按键设置不对。我有一个没有图像的面板,在左上角的设置按钮内,单击后会打开另一个场景。我尝试使用这 3 种方法,但都不起作用。它始终将其检测为游戏对象。
bool isGameStarted
正在检查玩家是否应该移动。
请指导我完成
尝试过
if (Swipe.Instance.Tap && !isGameStarted)
{
if (EventSystem.current.IsPointerOverGameObject() )
{
isGameStarted = false;
}
else
{
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
}
也尝试过使用触发器,但它通过 UI。
这是原始代码。
if (Swipe.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
点击屏幕后,播放器开始移动。如果单击设置按钮,我不需要它移动或启动游戏。
最佳答案
我遇到了同样的问题,直到我发现 IsPointerOverGameObject 似乎对任何游戏对象(可能带有碰撞器)而不仅仅是 UI 对象返回 true。
所以我编写了一个自定义静态类来仅检查 UI 对象。您需要将每个面板、图像、按钮等的图层设置为 UI 才能正常工作。
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public static class MouseOverUILayerObject
{
public static bool IsPointerOverUIObject()
{
PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
for (int i = 0; i < results.Count; i++)
{
if (results[i].gameObject.layer == 5) //5 = UI layer
{
return true;
}
}
return false;
}
}
像这样使用它:
private void OnMouseDown()
{
if (!MouseOverUILayerObject.IsPointerOverUIObject())
HandleClick();
}
关于c# - Unity IsPointerOverGameObject 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57010713/