Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使游戏对象在统一增强现实中可触摸_C#_Ios_Unity3d_Augmented Reality_Vuforia - Fatal编程技术网

C# 使游戏对象在统一增强现实中可触摸

C# 使游戏对象在统一增强现实中可触摸,c#,ios,unity3d,augmented-reality,vuforia,C#,Ios,Unity3d,Augmented Reality,Vuforia,我正在尝试与Vuforia SDK联合开发增强现实iOS应用程序。我正在努力让我的游戏对象可以触摸 目前,我可以让我的游戏对象像预期的那样悬停在我的标记上。然而,当点击它们时,什么也没有发生 这是我的等级制度 现在,我一直在浏览论坛和在线教程,了解如何做到这一点,下面是我到目前为止的代码 我有两个C#脚本:touchableManager(附加到ARCamera)和touchableGameobject(附加到Cube和Cube) touchableManager: using UnityEn

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

目前,我可以让我的游戏对象像预期的那样悬停在我的标记上。然而,当点击它们时,什么也没有发生

这是我的等级制度

现在,我一直在浏览论坛和在线教程,了解如何做到这一点,下面是我到目前为止的代码

我有两个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);
                }
            }
        }
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类touchableManager:MonoBehavior
{
公共层掩码touchInputMask;
私有列表touchList=新列表();
私有游戏对象[]触摸屏;
二等兵雷卡斯特命中;
无效更新()
{
如果(Input.touchCount>0)
{
touchesOld=新游戏对象[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach(输入触摸,触摸)
{
Ray Ray=GetComponent().ScreenPointToRay(touch.position);//已连接主摄像头
if(物理.光线投射(光线,外击,100f,触摸输入掩码.值))
{
GameObject recipient=hit.transform.GameObject;
touchList.Add(收件人);
if(touch.phase==TouchPhase.start){
recipient.SendMessage(“onTouchDown”,hit.point,SendMessageOptions.DontRequireReceiver);
}
如果(touch.phase==TouchPhase.end){
recipient.SendMessage(“onTouchUp”、hit.point、SendMessageOptions.DontRequireReceiver);
}
if(touch.phase==TouchPhase.stative | | touch.phase==TouchPhase.Moved){
recipient.SendMessage(“onTouchStay”,hit.point,SendMessageOptions.DontRequireReceiver);
}
如果(touch.phase==touch.phase.cancelled){
recipient.SendMessage(“onTouchExit”,hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach(touchesOld中的游戏对象g)
{
如果(!touchList.Contains(g))
{
g、 SendMessage(“onTouchExit”,hit.point,SendMessageOptions.DontRequestReceiver);
}
}
}
}
}
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;
    }
}
使用UnityEngine;
使用系统集合;
公共类touchableGameobject:MonoBehavior
{
公共色彩;
公共颜色选择颜色;
私人材料垫;
void Start()
{
mat=GetComponent().材料;
}
void onTouchDown()
{
mat.color=所选颜色;
}
void onTouchUp()
{
mat.color=默认颜色;
}
逗留期间无效()
{
mat.color=所选颜色;
}
void onTouchExit()
{
mat.color=默认颜色;
}
}
到目前为止,我的应用程序所做的只是显示这两个游戏对象。当我轻触它们时,什么也没发生。点击时,代码应更改立方体的颜色

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

编辑:添加框碰撞器。见截图


尝试更改方法调用:

recipient.SendMessage ("onTouchExit")
我知道参数是“可选的”,但突然


更多信息,我建议在调试器中检查变量的值。也许在某个地方。在任何情况下,您都可以理解哪个方法没有被调用。

我使用Unity方法就可以让它正常工作

void OnMouseDown(){

//…在这里写下你的东西,然后点击执行


}

好吧,在反复摆弄和头部撞击之后,我终于找到了答案。我在这里发布了对我有用的东西,以防未来的可怜灵魂能从中获得一些价值

我将回答我自己的问题,希望在我之后的任何人都会觉得这很有帮助

下面的代码对我很有用。将
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;
    }
}
使用UnityEngine;
使用系统集合;
公共类touchableGameobject:MonoBehavior
{
公共色彩;
公共颜色选择颜色;
私人材料垫;
void Start()
{
mat=GetComponent().材料;
}
void onTouchDown()
{
mat.color=所选颜色;
}
void onTouchUp()
{
mat.color=默认颜色;
}
逗留期间无效()
{
mat.color=所选颜色;
}
void onTouchExit()
{
mat.color=默认颜色;
}
}
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);
                }
            }
        }
    }
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类touchableManager:MonoBehavior
{
公共层掩码touchInputMask;
私有列表touchList=新列表();
私有游戏对象[]触摸屏;
二等兵雷卡斯特命中;
无效更新()
{
如果(Input.touchCount>0)
{
touchesOld=新游戏对象[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach(输入触摸,触摸)
{
Ray Ray=Camera.main.ScreenPointRoay(touch.position);//连接了主摄像机
if(Physics.Raycast(光线、外击、数学无限、touchInputMask.value))
{
GameObject recipient=hit.transform.GameObject;
touchList.Add(收件人);
if(touch.phase==TouchPhase.start){