如果图像锚丢失,图像检测中产生的图像将被 "粘 "在屏幕上。

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

因此,在Unity中,我试图制作一个AR应用,在检测到某个图像后会产生一些东西。如果相机失去了图像或对象的踪迹,它就会将生成的对象粘在屏幕上。我怎样才能避免这种情况,有没有一个变量可以显示状态?

我已经尝试过 "TrackingState "变量,可惜没有用。"Limited不仅适用于被粘住的状态(在屏幕上),而且还适用于 "受损 "状态,这将导致对象取消pawn,即使它仍然正常显示。不幸的是,这将导致非常敏感的相机处理,所以到目前为止它不是一个选项。"无 "在另一方面从未出现。

谁能告诉我可以用什么元素来解决这个问题?或者解释一下我在 "TrackingState "上做错了什么?

这是我目前尝试的代码(我也有一个记录器来显示每半秒的状态)。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

public class ImagesToFigures : MonoBehaviour
{
    private ARTrackedImageManager _arTrackedImageManager;
    private ARManager _arManager;
    private GameObject spawnedObject;
    private string existingString;
    private bool isActive;

    private GameObject[] children;
    private FoundStickerEvent foundStickerEvent;
    private string addedSticker = "";

    private XRReferenceImage referenceImage;



    void Awake()
    {
        _arTrackedImageManager = GetComponentInParent<ARTrackedImageManager>();
        // if (GetComponentInParent<ARManager>()) {
        _arManager = GetComponentInParent<ARManager>();
        // }
        existingString = "";

        children = new GameObject[this.transform.childCount];

        for (int i = 0; i < this.transform.childCount; i++)
        {
            GameObject child = this.transform.GetChild(i).gameObject;
            children[i] = child;
        }
    }

    public void activate()
    {
        isActive = true;
    }

    public void OnEnable()
    {
        _arTrackedImageManager.trackedImagesChanged += OnImageChanged;
    }

    public void OnDisable()
    {
        _arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
    }

    public void destroySpawnedObject()
    {
        // existingString = "";
        foreach (var child in children)
        {
            child.SetActive(false);
        }
        // addedSticker = "";
        // GameManager.instance.log("destroyed");
    }

    private void OnImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
    {
        if (GameManager.instance.currentState == GameState.AR_SCANNING)
        {
            // GameManager.instance.log(eventArgs.updated.Count +" " + referenceImage.name);
            foreach (GameObject child in children)
            {
                if (addedSticker == "")
                {
                    foreach (ARTrackedImage trackedImage in eventArgs.added)
                    {
                        addedSticker = trackedImage.referenceImage.name;
                        // GameManager.instance.log(addedSticker);
                    }
                }
                else
                {
                    // GameManager.instance.log(eventArgs.updated.Count.ToString());
                    foreach (ARTrackedImage trackedImage in eventArgs.updated)
                    {
                        try
                        {
                            if (child.name == trackedImage.referenceImage.name &&
                                (child.name == addedSticker || (referenceImage.name == trackedImage.referenceImage.name && child.name == referenceImage.name))
                            )
                            {
                                if (trackedImage.trackingState == TrackingState.Tracking /*|| trackedImage.trackingState == TrackingState.Limited*/)
                                {
                                    child.SetActive(true);
                                    GameManager.instance.currentState = GameState.AR_SCANNED;
                                    GameManager.instance.log(trackedImage.referenceImage.name);
                                    referenceImage = trackedImage.referenceImage;

                                    // GameManager.instance.log(child.transform.position.ToString());
                                    // trackedImage.destroyOnRemoval = true;
                                    _arManager.foundStickerEvent.Invoke(trackedImage.referenceImage.name);
                                    // _arManager.displayInfosEvent.Invoke("Tippe auf die Figur für spannende Infos!");

                                }
                            }
                            // else
                            // {
                            //     child.SetActive(false);
                            // }
                        }
                        catch (Exception e)
                        {
                            GameManager.instance.log("error: " + e.ToString());
                        }
                    }
                }
            }
        }

if ((currentlyActiveImage.trackingState == TrackingState.None) || currentlyActiveImage.trackingState == TrackingState.Limited))
{
  GameManager.instance.destroyARObject();
  GameManager.instance.ResetState();
}
unity3d augmented-reality
1个回答
0
投票

在Tracked Image脚本中,你可以定义当你失去踪迹时会发生什么。然后,你可以根据你的需要,将对象设置为非活动状态或销毁它。

© www.soinside.com 2019 - 2024. All rights reserved.