如何避免将对象放在按钮后面?

时间:2017-07-11 15:32:14

标签: c# unity3d google-project-tango tango

Google Tango和Unity相关问题。

使用 this教程我们可以通过点击屏幕在表面上放置一个对象。

以下是代码:

using UnityEngine;
using System.Collections;

public class KittyUIController : MonoBehaviour
{
    public GameObject m_kitten;
    private TangoPointCloud m_pointCloud;

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

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

    void PlaceKitten(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 kitten on the surface, and make it always face the camera.
        if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f)
        {
            Vector3 up = plane.normal;
            Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized;
            Vector3 forward = Vector3.Cross(right, plane.normal).normalized;
            Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up));
        }
        else
        {
            Debug.Log("surface is too steep for kitten to stand on.");
        }
    }
}

我在屏幕上有按钮,当我按下它们时,我也会在它后面的表面上放置一个物体。

我该如何避免它?所以,如果我按下按钮,我不想同时放置一个物体。

如果我在点击屏幕时执行了一项功能,如果点击屏幕上的按钮,如何阻止它执行?

2 个答案:

答案 0 :(得分:1)

实现此目的的一种方法是在按钮上添加标签,触摸投射光线,并查看光线是否与按钮的标签碰撞对象。

    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        Ray ray = Camera.main.ScreenPointToRay(t.position);
        RaycastHit hit;

        if(t.phase == TouchPhase.Ended && Physics.Raycast(ray,out hit,100))
        {
            if(hit.collider.tag == "button")
            {
                //do button stuff
            }
            else
            {
                PlaceKitten(t.position);
            }
        }
    }

答案 1 :(得分:1)

之前我已经回答了这个问题,但无法找到要复制此问题的副本。当我找到它时,我会关闭它。

在将其视为有效点击之前,您需要检查鼠标位置是否在UI之上。

这是通过IsPointerOverGameObject函数完成的。

void Update()
{
    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended)
        {
            //Make sure we are not over the UI
            if (!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Clicked outside the UI");
                PlaceKitten(t.position);
            }
        }
    }
}

对于触摸屏设备,您必须将 fingerId 传递给该功能:

void Update()
{
    if (Input.touchCount == 1)
    {
        // Trigger place kitten function when single touch ended.
        Touch t = Input.GetTouch(0);
        if (t.phase == TouchPhase.Ended)
        {
            //Make sure we are not over the UI
            if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched outside the UI");
                PlaceKitten(t.position);
            }
        }
    }
}