I'm using Unity 2021.3.22f1 and the version 4.2.7 of AR Foundation. I've already added to my scene the AR Session and AR Session Origin, and also AR Tracked Image Manager as a component of AR Session Origin.
I would like to create a MutableRuntimeReferenceImageLibrary which, runtime, add itself dynamically images that are contained in a project's folder.
I wrote this snippet of code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ImageLibraryManager : MonoBehaviour
{
public ARTrackedImageManager trackedImageManager;
public string imageFolderPath;
void Start()
{
// Create a list to hold XRReferenceImage objects
var imageList = new List<XRReferenceImage>();
// Get the files in the image folder
var imageFiles = System.IO.Directory.GetFiles(imageFolderPath);
// Loop through the image files and add them to the list
foreach (var imageFile in imageFiles)
{
// Load the image from the file
var imageBytes = System.IO.File.ReadAllBytes(imageFile);
var texture = new Texture2D(2, 2);
texture.LoadImage(imageBytes);
var newGuid = System.Guid.NewGuid();
var guidBytes = newGuid.ToByteArray();
var image = new XRReferenceImage(
// Use a new guid for each image
new SerializableGuid(
(ulong)(guidBytes[3] << 24 | guidBytes[2] << 16 | guidBytes[1] << 8 | guidBytes[0]),
(ulong)(guidBytes[7] << 24 | guidBytes[6] << 16 | guidBytes[5] << 8 | guidBytes[4])
),
// Use an empty guid for the second parameter
SerializableGuid.empty,
// Set the size of the image in meters
new Vector2(texture.width, texture.height) / 1000f,
// Set the name of the image
System.IO.Path.GetFileNameWithoutExtension(imageFile),
// Set the texture of the image
texture
);
imageList.Add(image);
}
// Create a new MutableRuntimeReferenceImageLibrary and assign it to the ARTrackedImageManager
var imageLibrary = trackedImageManager.CreateRuntimeLibrary() as MutableRuntimeReferenceImageLibrary;
foreach (var image in imageList)
{
// Add each image to the image library
imageLibrary.AddReferenceImage(image);
}
trackedImageManager.referenceLibrary = imageLibrary;
}
}
However, this gives to me this error:
Assets/ImageLibraryManager.cs(53,26): error CS1061: 'MutableRuntimeReferenceImageLibrary' does not contain a definition for 'AddReferenceImage' and no accessible extension method 'AddReferenceImage' accepting a first argument of type 'MutableRuntimeReferenceImageLibrary' could be found (are you missing a using directive or an assembly reference?)
I've already tried with other methods like "Add" or "AddImage", but it always gives to me the same error.
Can you help me to solve my issues and to create this type of MutableRuntimeReferenceImageLibrary?
Thank u