我正在尝试使用Vuforia SDK在Unity中开发增强现实iOS应用程序 . 我正在努力尝试使我的GameObjects可触摸 .
目前,我可以让我的GameObjects按预期悬停在我的标记上 . 但是,当点击它们时,没有任何反应 .
这是我的层次结构 .
现在,我一直在浏览论坛和在线教程,了解如何执行此操作,这是我到目前为止的代码 .
我有两个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 . 当我点击它们时,没有任何反应 . 点击时,代码应更改多维数据集的颜色 .
请,任何帮助将不胜感激 . 请引导我走正确的道路 .
编辑:添加了Box Colliders . 见截图 .
3 回答
尝试更改方法调用:
我理解“可选”的参数,但突然......
我建议踩入调试器来检查变量的值 . 也许在某处Null . 在任何情况下,您都可以了解未调用哪种方法 .
我只是使用Unity方法让它工作
void OnMouseDown(){
// ...在这里写下你的东西,点击后执行
}
好吧,经过多次摆弄和头部冲击,我设法弄明白了 . 我发布了一些对我有用的东西,因为一些可怜的未来灵魂可以从中获得一些 Value .
我会回答我自己的问题,希望任何追随我的人都会觉得这很有帮助 .
以下代码对我有用 . 将
touchableGameObject.cs
附加到游戏对象并将touchableManager.cs
附加到ARCamera .touchableGameObject.cs:
touchableManager.cs
此外,在Unity编辑器的右上角,单击“图层”下拉列表 . 编辑图层 . 我只是在字段中键入“触摸输入”(例如,用户第8层,或任何可用的内容) . 然后,选择ARCamera并在“检查器”选项卡中,在“图层”下拉列表中选择新命名的图层 . 对每个GameObject执行相同操作 . 此外,在ARCamera中,在“touchableManager”组件下,您还必须在“触摸输入掩码”下拉列表中选择新命名的图层 .
希望这有助于某人 .