【问题标题】:Vuforia edit default scriptVuforia 编辑默认脚本
【发布时间】:2020-05-28 19:30:08
【问题描述】:

我正在尝试使用 Vuforia 制作一个应用程序来匹配对应于同一组的图像。 例如,如果相机显示存储的斑马图像和存储的狗图像,则应设置文本表示正确,但如果相机捕捉到斑马图像和汽车图像,则应设置文本说错误。 我已经完成了相机相机识别,但我不知道如何处理代码以避免默认行为。 我想我必须修改 C# 脚本的这些部分

protected virtual void OnTrackingFound()
    {
        if (mTrackableBehaviour)
        {

            Debug.Log("Trackable " + mTrackableBehaviour.TrackableName);

            var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
            var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
            var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);


            // Enable rendering:
            foreach (var component in rendererComponents)
                component.enabled = true;

            // Enable colliders:
            foreach (var component in colliderComponents)
                component.enabled = true;

            // Enable canvas':
            foreach (var component in canvasComponents)
                component.enabled = true;
        }
    }


    protected virtual void OnTrackingLost()
    {
        if (mTrackableBehaviour)
        {
            var rendererComponents = mTrackableBehaviour.GetComponentsInChildren<Renderer>(true);
            var colliderComponents = mTrackableBehaviour.GetComponentsInChildren<Collider>(true);
            var canvasComponents = mTrackableBehaviour.GetComponentsInChildren<Canvas>(true);

            // Disable rendering:
            foreach (var component in rendererComponents)
                component.enabled = false;

            // Disable colliders:
            foreach (var component in colliderComponents)
                component.enabled = false;

            // Disable canvas':
            foreach (var component in canvasComponents)
                component.enabled = false;
        }
    }

但我不知道该怎么做,所以如果有人可以帮助我提供解决方案的想法,将不胜感激。

非常感谢。

【问题讨论】:

    标签: c# unity3d vuforia


    【解决方案1】:

    一种更简单的方法是创建一个附加到您要比较的每个标记的新脚本。例如:

    public class MyMarker : MonoBehaviour
    {
      public string markerName; //Can be edited in the inspector
    }
    

    那么你需要知道,当 Vuforia 找到或丢失标记时,GameObject 是启用还是禁用。

    您可以使用OnEnable()OnDisable() 方法在找到(启用)或丢失(禁用)时具有特定行为。

    public class MyMarker : MonoBehaviour
    {
      public string markerName; //Can be edited in the inspector
    
      void OnEnable()
      {
        //The object is active
      }
    
      void OnDisable()
      {
        //The object is inactive 
      }
    }
    

    此外,您可以使用myGameObject.activeInHierarchy 了解游戏对象是否处于活动状态。

    您现在知道哪些标记处于活动状态。

    【讨论】:

    • 您好,首先感谢您回答我的问题。我对 vuforia 和 unity 很陌生,所以我需要你更具体地回答。首先,我需要知道将脚本放在哪里以及何时运行。我不知道它是否应该在 imageTargets 上替换默认的可跟踪事件处理程序,或者添加一个附加脚本但不触及默认的可跟踪事件处理程序,或者即使脚本应该在与每个图像目标相关联的 3D 对象上.另一方面,我不知道使用代码显示 3D 对象...非常感谢您的宝贵时间
    • 不要编辑默认的可跟踪事件处理程序,也不要删除它,这是必不可少的。您应该在每个图像目标中附加“MyMarker”脚本,以识别它们。然后你可以有另一个脚本来管理标记是否处于活动状态。您不需要使用代码显示 3d 对象,通常只需将该对象设置为图像目标的子对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多