c# - 在 Unity 中使游戏对象可触摸 - 增强现实

标签 c# ios unity3d augmented-reality vuforia

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

目前,我能够让我的游戏对象按预期悬停在我的标记上。然而,当点击它们时,没有任何反应。

这是我的层次结构。

enter image description here

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

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

触摸管理器:

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

可触摸的游戏对象:

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

到目前为止,我的应用程序所做的只是显示两个游戏对象。当我点击它们时,没有任何反应。代码应在点击时更改立方体的颜色。

version1

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

编辑:添加了 Box Collider。查看屏幕截图。

enter image description here

最佳答案

尝试改变方法调用:

recipient.SendMessage ("onTouchExit")

我明白参数的“可选”,但是突然...

更多 我建议进入调试器来检查变量的值。也许某处为空。无论如何,这样您就可以了解未调用哪个方法。

关于c# - 在 Unity 中使游戏对象可触摸 - 增强现实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967750/

相关文章:

ios - 从服务器接收到数据后,如何在我的应用程序的 View Controller 标签中显示数据(从静态硬编码值更新)

ios - 我们如何在swift中使用协议(protocol)实现并发线程?

c# - SqlDependency.Start 在 Unity 中不起作用?

iphone - 让应用程序识别 "the right answer"

c# - 实例化 C# 时更改附加脚本中 var 的值

android - 适用于 iOS 和安卓的 Unity3D : Multiplayer (Bluetooth Connection)

c# - 使用 C#.Net : 仅显示 CheckBoxList 中的选中项

C#:通常首选哪种类型的数据容器?

c# - 在 CQRS 中注册 EventHandler

c# - 从查询字符串开始时比较 EntitySQL 中的 DateTime 值