C# Unity IsPointerOverGameObject问题
我知道这是一个常见的问题,但没有一个有效。可能按钮设置错误。我有一个没有图像的面板,在左上角的一个设置按钮中,单击该按钮可以打开另一个场景。我试过使用这三种方法,但都不管用。它总是将其检测为游戏对象 boolC# Unity IsPointerOverGameObject问题,c#,unity3d,C#,Unity3d,我知道这是一个常见的问题,但没有一个有效。可能按钮设置错误。我有一个没有图像的面板,在左上角的一个设置按钮中,单击该按钮可以打开另一个场景。我试过使用这三种方法,但都不管用。它总是将其检测为游戏对象 boolisGameStarted正在检查玩家是否应该移动。 请引导我通过 试过 if (Swipe.Instance.Tap && !isGameStarted) { if (EventSystem.current.IsPointerOverGameObject() )
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");
}
一旦你点击屏幕,播放器开始移动。如果单击“设置”按钮,我不需要它来移动或启动游戏。的重载包含一个参数
int pointerId
指针(触摸/鼠标)ID
及
如果在没有参数的情况下使用IsPointerOverGameObject()
,它将指向“鼠标左键”(pointerId=-1
)
因此,当使用<代码> iPoTeObjulgAgObjult< /Calp><强>触摸时,应考虑将PoTrID传递给它。<强>
从这个例子来看
if (Swipe.Instance.Tap && !isGameStarted)
{
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
isGameStarted = false;
return;
}
isGameStarted = true;
motor.StartRunning();
gameCanvas.SetTrigger("Show");
}
否则,如果您有多个可能的接触,您也可以像这样检查所有接触
if (Swipe.Instance.Tap && !isGameStarted)
{
foreach(var touch in Input.touches)
{
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
isGameStarted = false;
return;
}
}
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();
}