我正在尝试使用 Vuforia SDK 在 Unity 中开发增强现实 iOS 应用程序。我正在努力尝试让我的游戏对象可触摸。
目前,我能够让我的游戏对象按预期悬停在我的标记上。然而,当点击它们时,没有任何反应。
这是我的层次结构。
现在,我一直在浏览论坛和在线教程以了解如何执行此操作,这是我目前拥有的代码。
我有两个 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;
}
}
到目前为止,我的应用程序所做的只是显示两个游戏对象。当我点击它们时,没有任何反应。代码应在点击时更改立方体的颜色。
拜托,任何帮助将不胜感激。请引导我走正确的道路。
编辑:添加了 Box Collider。查看屏幕截图。
最佳答案
尝试改变方法调用:
recipient.SendMessage ("onTouchExit")
我明白参数的“可选”,但是突然...
更多 我建议进入调试器来检查变量的值。也许某处为空。无论如何,这样您就可以了解未调用哪个方法。
关于c# - 在 Unity 中使游戏对象可触摸 - 增强现实,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967750/