Hi,
I added a XRReferenceImageLibrary via Runtime and with the MutableRuntimeReferenceImageLibrary method ScheduleAddImageWithValidationJob I added a referenceImage.
The image is also recognized in the XR test environment.
Now the problem is, that when I want to read the name of the referenceImage on the event, it’s empty. The log just says:
“Found image:
UnityEngine.Debug:Log (object)”
So the name is missing.
Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.VisualScripting;
using Unity.XR.CoreUtils;
using UnityEditor.VersionControl;
using UnityEditor.XR.ARSubsystems;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Tilemaps;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class OnStartUp : MonoBehaviour
{
[SerializeField] private Response response;
[SerializeField]
private XRReferenceImageLibrary runtimeImageLibrary;
private ARTrackedImageManager trackImageManager;
// Start is called before the first frame update
public void Start()
{
Debug.Log("Started Game");
StartCoroutine(DownloadJson());
}
IEnumerator DownloadJson()
{
Debug.Log("Download json file");
String url = Config.APPURL + "data.json";
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log("Download json file failed");
Debug.Log(www.error);
}
else
{
Debug.Log("Download json file successfully");
Debug.Log(www.downloadHandler.text);
Debug.Log("Parse json file");
response = JsonUtility.FromJson<Response>(www.downloadHandler.text);
Debug.Log("Parse json file successfully");
Debug.Log("Download image targets");
trackImageManager = gameObject.AddComponent<ARTrackedImageManager>();
trackImageManager.referenceLibrary = trackImageManager.CreateRuntimeLibrary(runtimeImageLibrary);
trackImageManager.enabled = true;
trackImageManager.trackedImagesChanged += OnTrackedImagesChanged;
for (int i = 0; i < response.files.Count; i++)
{
StartCoroutine(RetrieveTextureFromWeb(response.files[i]));
}
}
}
IEnumerator RetrieveTextureFromWeb(FileData fileData)
{
String url = Config.APPURL + "TrackingImage/" + fileData.image;
Debug.Log("Download image target \"" + fileData.image + "\"");
using (UnityWebRequest uwr =
UnityWebRequestTexture.GetTexture(url))
{
yield return uwr.SendWebRequest();
if (uwr.result != UnityWebRequest.Result.Success)
{
Debug.Log(uwr.error);
}
else
{
Debug.Log("Download image target \"" + fileData.image + "\" successfully");
Texture2D imageFromWeb;
var texture = DownloadHandlerTexture.GetContent(uwr);
imageFromWeb = texture;
CreateImageTargetFromDownloadedTexture(fileData.image, imageFromWeb);
}
}
}
void CreateImageTargetFromDownloadedTexture(String name, Texture2D image)
{
float x = 0.2f;
Debug.Log("Add image target \"" + name + "\"");
if (trackImageManager.referenceLibrary is MutableRuntimeReferenceImageLibrary mutableLibrary) {
AddReferenceImageJobState jobState = mutableLibrary.ScheduleAddImageWithValidationJob(image, name, x);
if (jobState.status == AddReferenceImageJobStatus.Success)
{
Debug.Log($"Added image {name} successfully.");
}
else
{
Debug.LogWarning($"Failed to add image {name} to library. {jobState.status}");
}
}
else
{
Debug.Log($"The reference image library is not mutable.");
}
}
void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (ARTrackedImage trackedImage in eventArgs.added)
{
// Display the name of the tracked image
Debug.Log("Found image: " + trackedImage.referenceImage.name);
}
foreach (ARTrackedImage trackedImage in eventArgs.updated)
{
// Display the name of the tracked image
Debug.Log("Found image: " + trackedImage.referenceImage.name);
}
}
}
[System.Serializable]
public class Response
{
public List<FileData> files;
}
[System.Serializable]
public class FileData
{
public string image;
public string model;
public float scale;
}
