I am tracking 2 images and the tracking works fine with ARKit. The problem is, after I track both the images, I cannot go back to track the first one again. That is, first I track Image 1. Then I track Image 2. After this when I go back and track Image 1, I do not get any debug. I feel it ends at foreach (var addedImage in _args.added)
. How do I make the tracking unlimited?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARFoundation;
public class MultipleImageTrack : MonoBehaviour
{
[Header("The length of this list must match the number of images in Reference Image Library")]
public List<GameObject> ObjectsToPlace;
public int checkPoint = 0;
public GameObject preloadedImage;
private int refImageCount;
private Dictionary<string, GameObject> allObjects;
public ARTrackedImageManager arTrackedImageManager;
private IReferenceImageLibrary refLibrary;
void Awake()
{
}
private void OnEnable()
{
arTrackedImageManager.trackedImagesChanged += OnImageChanged;
}
private void OnDisable()
{
arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
}
private void Start()
{
refLibrary = arTrackedImageManager.referenceLibrary;
refImageCount = refLibrary.count;
LoadObjectDictionary();
}
void LoadObjectDictionary()
{
allObjects = new Dictionary<string, GameObject>();
for (int i = 0; i < refImageCount; i++)
{
allObjects.Add(refLibrary[i].name, ObjectsToPlace[i]);
ObjectsToPlace[i].SetActive(false);
}
}
void ActivateTrackedObject(string _imageName)
{
debug.text = "scanned: " + _imageName;
for (int i = 0; i < refImageCount; i++)
{
ObjectsToPlace[i].SetActive(false);
}
allObjects[_imageName].SetActive(true);
}
public void OnImageChanged(ARTrackedImagesChangedEventArgs _args)
{
foreach (var addedImage in _args.added)
{
checkPoint += 1; //I track here how many times the images get tracked. Maximum it goes is 2
ActivateTrackedObject(addedImage.referenceImage.name);
}
foreach (var updated in _args.updated)
{
allObjects[updated.referenceImage.name].transform.position = updated.transform.position;
allObjects[updated.referenceImage.name].transform.rotation = updated.transform.rotation;
}
}
}
question from:
https://stackoverflow.com/questions/65876078/arkit-tracking-for-image 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…