gpt4 book ai didi

c# - 处理未在 UI 上启动的触摸

转载 作者:行者123 更新时间:2023-11-30 23:08:57 25 4
gpt4 key购买 nike

我寻求一种方法来处理未在 Unity 引擎中的 UI 元素上启动的触摸。

这样做的目的是在一张“ map ”(以后都这么叫)上旋转、平移、放大。但是,如果触摸事件发生在任何 UI 元素上,则应由该 UI 元素而不是 map 处理。

我认为 Google 的 map android 应用程序就是这样的一个例子。

我尝试了几种解决方案:

  • 鼠标事件 - 但这将所有触摸合并为一个

  • Input.touches - 但我不知道如何确定触摸是否应该由另一个 UI 元素处理(我希望尽可能不使用“if”来检查触摸是否打开任何其他 UI 元素,因为这可能证明是真正的性能问题)

最佳答案

I seek a way to handle touches that do not start on UI elements in Unity Engine ...But if the touch down event is on any of the UI elements it shall be handled by that UI element instead of the map.

IsPointerOverGameObject函数用于简化此操作。如果返回false,你是否平移,旋转。如果它返回 true,则鼠标位于任何 UI 元素上。使用它比使用由每个 UI 元素修改的 bool 变量更好、更容易。使用 OnPointerClick 检测 UI( map )上的事件。参见 this获取更多信息。

void Update()
{
checkTouchOnScreen();
}

void checkTouchOnScreen()
{
#if UNITY_IOS || UNITY_ANDROID || UNITY_TVOS
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
//Make sure finger is NOT over a UI element
if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
OnSCreenTouched();
}
}
#else
// Check if the left mouse button was clicked
if (Input.GetMouseButtonDown(0))
{
//Make sure finger is NOT over a UI element
if (!EventSystem.current.IsPointerOverGameObject())
{
OnSCreenTouched();
}
}
#endif
}


void OnSCreenTouched()
{
Debug.Log("Touched on the Screen where there is no UI");

//Rotate/Pan/Zoom the camera here
}

I would like if possible to not use "if"s to check if the touch is on any another UI element as this might prove a real performance issue

没有 if 语句,没有其他方法可以做到这一点。即使是上面的简单答案也需要它。它不会影响性能。

关于c# - 处理未在 UI 上启动的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com