处理未在 UI 上启动的触摸 [英] Handle touches not started on the UI
问题描述
我正在寻找一种方法来处理不在 Unity 引擎中的 UI 元素上开始的触摸.
这样做的目的是在地图"上进行旋转、平移和放大(从现在开始应如此称呼).但如果触摸事件发生在任何 UI 元素上,则应由该 UI 元素而非地图处理.
我认为这样的一个例子是 Google 的 Maps android 应用程序.
我尝试了几种解决方案:
鼠标事件 - 但这将所有触摸合并为一个
Input.touches - 但我不知道如何确定触摸是否应该由另一个 UI 元素来处理(如果可能的话,我希望不使用if"来检查触摸是否打开任何其他 UI 元素,因为这可能证明是真正的性能问题)
我寻求一种方法来处理不在 UI 元素上开始的触摸Unity Engine ...但是如果触摸事件发生在任何一个 UI 上元素应由该 UI 元素而不是地图处理.
IsPointerOverGameObject
函数是用来简化这个.如果它返回 false,你是否平移、旋转.如果返回 true,则鼠标位于任何 UI 元素上.使用它比使用由每个 UI 元素修改的布尔变量更好、更容易.使用 OnPointerClick
检测 UI(地图)上的事件.请参阅此了解更多信息信息.
void Update(){checkTouchOnScreen();}无效 checkTouchOnScreen(){#if UNITY_IOS ||UNITY_ANDROID ||UNITY_TVOSif (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){//确保手指不在 UI 元素上if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)){屏幕触摸();}}#别的//检查鼠标左键是否被点击if (Input.GetMouseButtonDown(0)){//确保手指不在 UI 元素上if (!EventSystem.current.IsPointerOverGameObject()){屏幕触摸();}}#万一}无效的屏幕触摸(){Debug.Log("触摸到没有UI的屏幕");//在此处旋转/平移/缩放相机}
<块引用>
如果可能的话,我希望不要使用if"来检查触摸是否打开任何其他 UI 元素,因为这可能证明是真正的性能问题
如果没有 if
语句,就没有其他方法可以做到这一点.即使是上面的简单答案也需要它.它不会影响性能.
I seek a way to handle touches that do not start on UI elements in Unity Engine.
The purpose of this is rotating, panning and zooming in on a "map" (it shall be called so from now on). 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.
I think one such example is Google's Maps android application.
I have tried several solutions:
Mouse events - but this combines all of the touches into a single one
Input.touches - but I do not know how to find out if the touch should be handled by another UI element instead (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)
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.
The IsPointerOverGameObject
function is used to simplify this. If it returns false, do you panning, rotation. If it returns true then the mouse is over any UI element. It is better and easier to use this than to use a boolean variable that is modifed by every UI element. Use OnPointerClick
to detect events on the UI(map). See this for more information.
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
There is no other way to do this without an if
statement. Even the simple answer above requires it. It doesn't affect the performance.
这篇关于处理未在 UI 上启动的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!