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

标签 c# unity-game-engine google-project-tango

我们正在构建一个类似于“Kitten - 在 AR 中放置虚拟对象”的示例,如下所示:

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

基本上,当您触摸屏幕时,一只小猫就会出现在现实世界的平面(地板)上。

在我们的应用程序中,我们有一个侧面菜单,其中有几个按钮,每个按钮都显示不同的游戏对象。我们希望检测屏幕上除 UI 之外的任何位置的触摸。我们希望 UI 阻止 Tango 中的触摸,并且只允许触摸在没有 UI 元素的屏幕区域上实例化相关游戏对象。

触摸特定代码在这里:

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);
        }
    }
}

(PlacePictureFrame() 在触摸位置放置一个图片框对象。)

我找不到任何将触摸和 UI 相结合的 Tango 示例。我尝试过一种名为 LeanTouch 的 Assets 来阻止 UI 元素后面的触摸,但它似乎不适用于 Tango。请帮忙!

我尝试过使用方法 5:

How to detect events on UI and GameObjects with the new EventSystem API

虽然它确实向 TangoARCamera(标记为 MainCamera)添加了 PhysicsRaycaster,但 OnPointerDown > 无论您触摸屏幕的哪个位置,方法都不会生成调试日志。 Tango 是一个特殊情况,因此这不是一个重复的问题。见下文:

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);
        }
    }
}

最佳答案

如果您想检测屏幕上除 UI 控件/组件之外的任何位置的点击,则必须使用 EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) 检查指针是否位于 UI 上方。 .

如果在桌面上,请使用EventSystem.current.IsPointerOverGameObject()。您正在使用 Tango,所以EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId) 。应该使用。

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);
            }
        }
    }
}

编辑:

看起来这仅适用于 TouchPhase.Began

t.phase == TouchPhase.Ended 更改为t.phase == TouchPhase.Began,这应该按预期工作。确保使用移动设备/探戈而不是鼠标进行测试。

关于c# - Tango/Unity - UI 不会阻止屏幕上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151553/

相关文章:

c# - 如何在 NEST 中附加两个 SearchDescriptors

c# - 打开 PDFSharp 文档并保存删除密码

c# - 如何加速无尽跑酷

unity-game-engine - Unity3D 中较远的对象与较近的对象重叠

c# - 如何在 Unity C# 中为多个游戏对象移动进行多线程处理

Java Tango支持 : Transforming several Cloud Point snapshots to the same space

java - 无法理解 TangoPoseData 中的 ij 术语

java - Tango 图像格式 YCRCB_420_SP

c# - 任务.WaitSubset/任务.WaitN?

c# - C:函数指针与 C# 委托(delegate)相比的不安全示例