简体   繁体   English

Tango / Unity-UI不会阻止屏幕上的触摸

[英]Tango/Unity - UI not blocking touches on screen

We are building an example that is similar to the "Kitten - Placing Virtual objects in AR" as shown here: 我们正在构建一个类似于“小猫-在AR中放置虚拟对象”的示例,如下所示:

https://developers.google.com/tango/apis/unity/unity-howto-placing-objects . https://developers.google.com/tango/apis/unity/unity-howto-placing-objects

Basically when you touch the screen, a kitten appears on the real world plane (floor). 基本上,当您触摸屏幕时,小猫会出现在现实世界的平面(地板)上。

In our app we have a side menu, with a few buttons and each shows a different game object. 在我们的应用程序中,我们有一个侧面菜单,带有几个按钮,每个按钮显示一个不同的游戏对象。 We want to detect touch anywhere on the screen except where there is UI. 我们希望在屏幕上任何地方(除了有UI的地方)检测触摸。 We want the UI to block touches in Tango, and only allow touches to instantiate the related game objects on areas of the screen without UI elements. 我们希望UI阻止Tango中的触摸,并且仅允许触摸在没有UI元素的情况下实例化屏幕区域上的相关游戏对象。

The touch specific code is here: 触摸特定的代码在这里:

void Update() {
    if (Input.touchCount == 1) {
        // Trigger placepictureframe function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended) {
            PlacePictureFrame(t.position);
        }
    }
}

(The PlacePictureFrame() places a picture frame object at the touch position.) PlacePictureFrame()将相框对象放置在触摸位置。)

I can't find any Tango examples which has touch and UI combined. 我找不到任何结合了触摸和UI的Tango示例。 I've tried an asset called LeanTouch to block touches behind UI elements but it doesn't seem to work with Tango specifically. 我尝试了一种名为LeanTouch的资产来阻止UI元素后面的触摸,但是它似乎不适用于Tango。 Please help! 请帮忙!

I have tried using method 5 from this: 我已经尝试过使用方法5:

How to detect events on UI and GameObjects with the new EventSystem API 如何使用新的EventSystem API在UI和GameObjects上检测事件

and while it does add a PhysicsRaycaster to the TangoARCamera (which is tagged as MainCamera ), the OnPointerDown method produces no debug logs no matter where you touch the screen. 尽管确实向TangoARCamera (标记为MainCamera )添加了PhysicsRaycaster ,但无论您在何处触摸屏幕, OnPointerDown方法都不会产生调试日志。 Tango is a special case so this is not a duplicate question. 探戈是一个特例,因此这不是重复的问题。 See below: 见下文:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PictureFrameUIController : MonoBehaviour, IPointerClickHandler {

    public GameObject m_pictureFrame;
    private TangoPointCloud m_pointCloud;

    void Start() {
        m_pointCloud = FindObjectOfType<TangoPointCloud>();
        addPhysicsRaycaster();
    }

    void addPhysicsRaycaster() {
        PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
        if (physicsRaycaster == null) {
            Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        }
    }

    public void OnPointerClick(PointerEventData eventData) {
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
        PlacePictureFrame(eventData.pointerCurrentRaycast.screenPosition);
    }


    //void Update() {
    //  if (Input.touchCount == 1) {
    //      // Trigger placepictureframe function when single touch ended.
    //      Touch t = Input.GetTouch(0);
    //      if (t.phase == TouchPhase.Ended) {
    //          PlacePictureFrame(t.position);
    //      }
    //  }
    //}

    void PlacePictureFrame(Vector2 touchPosition) {
        // Find the plane.
        Camera cam = Camera.main;
        Vector3 planeCenter;
        Plane plane;
        if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) {
            Debug.Log("cannot find plane.");
            return;
        }

        // Place picture frame on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) > 60.0f && Vector3.Angle(plane.normal, Vector3.up) < 140.0f) {
            Vector3 forward = plane.normal;
            // Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            // Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_pictureFrame, planeCenter, Quaternion.LookRotation(forward, Vector3.up));
        } else {
            Debug.Log("surface is not steep enough for picture frame to be placed on.");
        }
    }

    public void DeleteAllFrames() {
        GameObject[] frames = GameObject.FindGameObjectsWithTag("Frame");
        if (frames == null) {
            return;
        }
        foreach (GameObject frame in frames) {
            Destroy(frame);
        }
    }
}

If you want to detect a click anywhere on the screen except for where there is a UI control/component, you have to check if the pointer is over the UI with EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) . 如果要检测屏幕上除存在UI控件/组件以外的任何位置的单击,则必须使用EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)检查指针是否在UI EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)

If on desktop, use EventSystem.current.IsPointerOverGameObject() . 如果在桌面上,请使用EventSystem.current.IsPointerOverGameObject() You are using Tango so EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) . 您正在使用Tango,所以EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) should be used. 应该使用。

void Update()
{
    if (Input.touchCount == 1)
    {
        //Trigger placepictureframe function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended)
        {
            //Make sure that pointer is not over UI before calling  PlacePictureFrame
            if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                PlacePictureFrame(t.position);
            }
        }
    }
}

Edit: 编辑:

It seems like this works with TouchPhase.Began only. 似乎仅适用于TouchPhase.Began

Change t.phase == TouchPhase.Ended to t.phase == TouchPhase.Began and this should work as expected. t.phase == TouchPhase.Ended更改to t.phase == TouchPhase.Began ,这应该可以正常工作。 Make sure to test with a mobile device/tango instead of your mouse. 确保使用移动设备/探戈而不是鼠标进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM