使GameObjects在Unity中可触摸 - 增强现实

时间:2015-11-28 04:38:01

标签: c# ios unity3d augmented-reality vuforia

我正在尝试使用Vuforia SDK在Unity中开发增强现实iOS应用程序。我正努力让我的GameObjects可以触摸。

目前,我可以让我的GameObjects按预期悬停在我的标记上。但是,当点击它们时,没有任何反应。

这是我的层次结构。

enter image description here

现在,我一直在浏览论坛和在线教程,了解如何执行此操作,这是我到目前为止的代码。

我有两个C#脚本:touchableManager(附加到ARCamera)和touchableGameobject(附加到Cube和Cube)

touchableManager:

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

public class touchableManager : MonoBehaviour
{
    public LayerMask touchInputMask;
    private List<GameObject> touchList = new List<GameObject>();
    private GameObject[] touchesOld;
    private RaycastHit hit;

    void Update () 
    {
        if (Input.touchCount > 0)
        {
            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();

            foreach (Touch touch in Input.touches)
            {
                Ray ray = GetComponent<Camera>().ScreenPointToRay (touch.position);    //attached the main camera

                if (Physics.Raycast(ray, out hit, 100f, touchInputMask.value))
                {

                    GameObject recipient = hit.transform.gameObject;
                    touchList.Add(recipient);

                    if (touch.phase == TouchPhase.Began) {
                        recipient.SendMessage ("onTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Ended) {
                        recipient.SendMessage ("onTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
                        recipient.SendMessage ("onTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Canceled) {
                        recipient.SendMessage ("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                    }
                }
            }
            foreach (GameObject g in touchesOld)
            {
                if (!touchList.Contains(g)) 
                {
                    g.SendMessage("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
}

touchableGameobject:

using UnityEngine;
using System.Collections;

public class touchableGameobject : MonoBehaviour
{
    public Color defaultColor;
    public Color selectedColor;
    private Material mat;

    void Start()
    {
        mat = GetComponent<Renderer>().material;
    }

    void onTouchDown()
    {
        mat.color = selectedColor;
    }

    void onTouchUp()
    {
        mat.color = defaultColor;
    }

    void onTouchStay()
    {
        mat.color = selectedColor;
    }

    void onTouchExit()
    {
        mat.color = defaultColor;
    }
}

到目前为止,我的所有应用程序都是显示两个GameObjects。当我点击它们时,没有任何反应。点击时,代码应更改多维数据集的颜色。

version1

请,任何帮助将不胜感激。请引导我走正确的道路。

编辑:添加了Box Colliders。见截图。

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试更改方法调用:

recipient.SendMessage ("onTouchExit")

我理解&#34;可选&#34;的参数,但突然间......

更多我建议单步调试器来检查变量的值。也许在某处Null。在任何情况下,您都可以了解未调用哪种方法。

答案 1 :(得分:0)

我只是使用Unity方法

让它工作

void OnMouseDown(){

// ...在这里写下你的东西,点击

}

答案 2 :(得分:0)

好吧,经过多次摆弄和头部冲击,我设法弄明白了。我发布了一些对我有用的东西,因为一些可怜的未来灵魂可以从中获得一些价值。

我会回答我自己的问题,希望任何追随我的人都会觉得这很有帮助。

以下代码对我有用。将touchableGameObject.cs附加到游戏对象并将touchableManager.cs附加到ARCamera。

touchableGameObject.cs:

using UnityEngine;
using System.Collections;

public class touchableGameobject : MonoBehaviour
{
    public Color defaultColor;
    public Color selectedColor;
    private Material mat;

    void Start()
    {
        mat = GetComponent<Renderer>().material;
    }

    void onTouchDown()
    {
        mat.color = selectedColor;
    }

    void onTouchUp()
    {
        mat.color = defaultColor;
    }

    void onTouchStay()
    {
        mat.color = selectedColor;
    }

    void onTouchExit()
    {
        mat.color = defaultColor;
    }
}

touchableManager.cs

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

public class touchableManager : MonoBehaviour
{
    public LayerMask touchInputMask;
    private List<GameObject> touchList = new List<GameObject>();
    private GameObject[] touchesOld;
    private RaycastHit hit;

    void Update ()
    {
        if (Input.touchCount > 0)
        {
            touchesOld = new GameObject[touchList.Count];
            touchList.CopyTo(touchesOld);
            touchList.Clear();

            foreach (Touch touch in Input.touches)
            {
                Ray ray = Camera.main.ScreenPointToRay(touch.position);    //attached the main camera

                if (Physics.Raycast(ray, out hit, Mathf.Infinity, touchInputMask.value))
                {
                    GameObject recipient = hit.transform.gameObject;
                    touchList.Add(recipient);

                    if (touch.phase == TouchPhase.Began) {
                        Debug.Log("Touched: " + recipient.name);
                        recipient.SendMessage ("onTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Ended) {
                        recipient.SendMessage ("onTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
                        recipient.SendMessage ("onTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                    }

                    if (touch.phase == TouchPhase.Canceled) {
                        recipient.SendMessage ("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                    }
                }
            }
            foreach (GameObject g in touchesOld)
            {
                if (!touchList.Contains(g))
                {
                    g.SendMessage("onTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                }
            }
        }
    }
}

此外,在Unity编辑器的右上角,单击&#34;图层&#34;落下。编辑图层。我刚输入&#34;触摸输入&#34;到一个字段(例如用户第8层,或任何可用的)。然后,选择ARCamera并在Inspector选项卡中,在&#34; Layer&#34;中选择新命名的图层。落下。对每个GameObject执行相同操作。此外,在ARCamera中,在&#34; touchableManager&#34;组件,您还必须在&#34;触摸输入掩码&#34;中选择新命名的图层。落下。

希望这有助于某人。