未在UI上开始处理触摸 [英] Handle touches not started on the UI

查看:100
本文介绍了未在UI上开始处理触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻求一种方法来处理无法在Unity Engine中的UI元素上开始的触摸.

I seek a way to handle touches that do not start on UI elements in Unity Engine.

此操作的目的是在地图"上旋转,平移和放大(从现在开始应如此称呼).但是,如果触碰事件发生在任何UI元素上,则应由该UI元素而不是地图来处理.

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.

我认为这样的例子之一就是Google的Maps android应用程序.

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-但是我不知道如何找出是否应该由另一个UI元素来处理触摸(我希望尽可能不使用"if"来检查触摸是否打开)任何其他UI元素,因为这可能会证明是实际的性能问题)

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)

推荐答案

我寻求一种方法来处理在UI元素中未从UI元素开始的触摸 Unity Engine ...但是如果触摸事件在任何UI上 元素,则应由该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元素修改的布尔变量相比,使用此方法更好,更容易.使用OnPointerClick检测UI(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
}

我希望尽可能不使用"if"来检查触摸是否开启 任何其他UI元素,因为这可能会证明是实际的性能问题

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语句,没有其他方法可以执行此操作.甚至上面的简单答案也需要它.它不会影响性能.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆