使用 Vuforia 和 Unity 仅创建一次具有地平面检测的对象

Create objects with ground plane detection only once with Vuforia & Unity

我正在尝试使用 Unity 和 Vuforia 创建一个 AR 应用程序。我有一个 3D 模型需要在地平面 detected.But 时生成,这只需要发生一次。 Vuforia 的工作方式是,当检测到新平面时,它会继续生成对象。所以我需要做的是只检测一次平面或只生成一次对象。由于我是 Unity 的新手,因此我需要帮助。如果有人能告诉我我需要做什么才能实现这一点,那就太好了。

在您的应用中,您应该在某处有一个 Plane Finder 对象,默认设置了以下属性

Plane Finder 对象附加了一个行为组件,如果找到平面,该组件会调用 Position Content 方法。该方法属于 Content Positioning Behaviour,它创建了您的 Ground Plane Stage 的一个实例(克隆)。为了避免出现多个实例,您应该导入位于此处的 vuforia Deploy Stage Once 脚本:https://library.vuforia.com/articles/Solution/ground-plane-guide.html and you should change the Plane Finder Behaviour as the following:

Vuforia updated.Now 没有 DeploymentStageOnce script.Inorder 在我们触摸时停止复制,我们必须在内容定位行为(脚本)中关闭复制阶段当我们点击 Plane Finder 时检查 Inspector .

更新版本:

转到 "Advanced" 设置和 "On Interactive Hit Test" 脚本 -> Select "Off" 脚本选项。

我纠结了很久,简而言之,我们必须在命中后禁用 AnchorInputListenerBehaviour

  1. 我在 PlaneFinder 上附加了一个新脚本,代码如下:

    <!-- language-all: c# -->
    public void OnInteractiveHitTest(HitTestResult result)
    {
        var listenerBehaviour = GetComponent<AnchorInputListenerBehaviour>();
        if (listenerBehaviour != null)
        {
            listenerBehaviour.enabled = false;
        }
     }
    
  2. 我在 Plane Finder 行为上添加了事件

就这些了,希望对你有用

请尝试 vuforia 网站解决此问题

Introduction to Ground Plane in Unity

大多数答案都是正确的,但有点过时了,正确的方法是通过代码。

创建一个名为 GameManager 的游戏对象,并将 GroundPlaneStage 和您想要生成的对象的预制件传递给附加到该 GameManager 的脚本,例如调用它 GameManagerScript.cs,并创建一个名为 spawnObjects 的小函数它执行以下操作:

public class SceneManagerScript : MonoBehaviour {

    public GameObject objPrefab;
    public GameObject ground;
    private int count = 0;

    public void spawnObject() {
        Instantiate(objPrefab, new Vector3(count, 0, 0), Quaternion.identity, ground.transform);
    count += 2; 
    }
}

然后转到专门针对 PlaneFinderBehaviour.cs 组件的 PlaneFinder,您将获得 OnInteractiveHitTest 和 OnAutomaticHitTest 的回调,在您的情况下,您需要 OnAutomativeHitTest,单击 + 并添加一个新的回调(spawnObject 中的函数上面的代码如下图所示)

  1. 另外,当您通过预制件实例化您喜欢的对象时,不要忘记编写正确的位置更新,以防止对象被添加到相同的位置
  2. 也不要忘记使 GroundPlaneStage 成为对象的父对象,并意识到您在 Instantiate() 函数中添加的位置是相对于该父对象(GroundPlaneStage,在上面的代码中用变量表示地面)
  3. 最后不要忘记在 Plane Finder 的 "Content Positioning Behaviour" 组件中取消选中 Duplicate Stage,如下图所示:

希望对您有所帮助