就像上图一样,我们有一个模型和它前面的 Canvas 。它的 footer 有碰撞器,我还需要按钮中的 onclick 事件。我想要的是当用户单击按钮时游戏对象没有任何 Action ,但现在很难单击按钮。
下面是我的代码:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
EventSystem es = EventSystem.current;
Debug.Log("IsPointerOverGameObject === " + es.IsPointerOverGameObject(Input.GetTouch(0).fingerId));
Debug.Log("currentSelectedGameObject === " + es.currentSelectedGameObject);
if (!(es.IsPointerOverGameObject(Input.GetTouch(0).fingerId) && es.currentSelectedGameObject != null))
{
Debug.Log("Handle touch === ");
HandleTouchEvent();
}
}
有人有什么想法吗?
最佳答案
我认为当您触摸 gameobject
时,您的 UI 和其他游戏对象都会受到影响,因此您应该排除用户使用这样的 UI 元素的时间,
public class TouchExample : MonoBehaviour {
void Update ()
{
// Check if there is a touch
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
// Check if finger is over a UI element
if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Debug.Log("UI is touched");
//so when the user touched the UI(buttons) call your UI methods
}
else
{
Debug.Log("UI is not touched");
//so here call the methods you call when your other in-game objects are touched
}
}
}
}
关于unity-game-engine - Unity触摸和ui按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41560432/