I need to dynamically get images from the internet for AR image tracking. The images downloaded don’t track. The preloaded images track fine. I see there were other users who had this issue in android but I don’t see the solution. Any ideas?
private void Start() {
trackedImageManager = GetComponent<ARTrackedImageManager>();
this.mutableLibrary = (MutableRuntimeReferenceImageLibrary)trackedImageManager.referenceLibrary;
StartCoroutine(UpdateImagesAtRuntime(url));
}
private IEnumerator UpdateImagesAtRuntime(string imageUrl) {
yield return new WaitForSeconds(someDelay); //fails if download starts right away
if (!(ARSession.state == ARSessionState.SessionInitializing || ARSession.state == ARSessionState.SessionTracking))
{
yield break;
}
Texture2D downloadedTexture = null;
using (UnityWebRequest req = UnityWebRequestTexture.GetTexture(imageUrl))
{
yield return req.SendWebRequest();
if (req.result == UnityWebRequest.Result.Success)
{
downloadedTexture = DownloadHandlerTexture.GetContent(req);
}
}
if (downloadedTexture == null)
{
yield break;
}
// Add the image to the mutable library
AddImageToLibrary(downloadedTexture, name);
}
private void AddImageToLibrary(Texture2D texture, string name)
{
try
{
// Convert the Texture2D to XRReferenceImage
var jobState = this.mutableLibrary.ScheduleAddImageWithValidationJob(
texture,
name,
(float)(texture.width * 0.0002645833)); // size in meters
// Wait for the job to complete
jobState.jobHandle.Complete();
Debug.Log($"Image '{name}' added to the library.");
}
catch (Exception ex)
{
}
}