破坏Unity ARCore中的锚点

问题描述 投票:0回答:1

我有一个项目,将游戏对象放在用户触摸屏幕的位置。锚是这样制作的:

private Anchor anchor;

anchor = detectedPlane.CreateAnchor (new Pose (anchorPosition, Quaternion.identity));

transform.position = anchorPosition;

transform.SetParent (anchor.transform);

在用户第二次触摸屏幕或之后的任意次数创建锚点之前,我确保使用以下代码销毁锚点。

if (anchor != null) {
    Destroy(anchor);
}

但是,当我在Instant Preview上测试我的代码时,所有锚点在我多次触摸屏幕后仍然存在。这是破坏锚点的正确方法吗?

unity3d arcore
1个回答
1
投票

Android的

如果要从场景中删除AnchorNode,则在Android ARCore中建议使用parent.removeChild(child)或node.setParent(null)。

要让ARCore停止跟踪Anchor,可以使用yourAnchor.detach()。

更多细节 - Android ARCore

统一

统一中的Trackable类有一种方法来创建Anchors而不是销毁它们。但是,似乎使用的方法是销毁代表Anchor的Unity GameObject。

请参阅Unity ARCore SDK中的示例:https://github.com/google-ar/arcore-unity-sdk/blob/9448df3e371e7e04a3e99ed6745890021d8f3e32/Assets/GoogleARCore/Examples/ObjectManipulation/Scripts/Manipulators/TranslationManipulator.cs

相关代码的摘录:

            GameObject oldAnchor = transform.parent.gameObject;

            Pose desiredPose = m_LastHit.Pose;

            Vector3 desiredLocalPosition = transform.parent.InverseTransformPoint(desiredPose.position);

            if (desiredLocalPosition.magnitude > MaxTranslationDistance)
            {
                desiredLocalPosition = desiredLocalPosition.normalized * MaxTranslationDistance;
            }

            desiredPose.position = transform.parent.TransformPoint(desiredLocalPosition);

            Anchor newAnchor = m_LastHit.Trackable.CreateAnchor(desiredPose);
            transform.parent = newAnchor.transform;

            Destroy(oldAnchor);
© www.soinside.com 2019 - 2024. All rights reserved.