Hello everyone. I am developing an augmented reality application. I am a newbie in Unity, so do not judge strictly if this is some kind of trivial question. The task is as follows: I have a server from which I download images and videos after authorization. Then the user can go to the AR scene. Accordingly, when scanning one of the downloaded images, the corresponding video should appear. For this approach, I use Mutable Runtime Reference Image Library. Images are added to the library after the scene is opened, but when scanning, it displays a warning that there is no video. It also does not display the name of the scanned image, but displays an empty value. For the test, I added one image to the regular image library (Reference Image Library), and added a video with the same name to the folder with downloaded videos. Everything works when scanning this image. That is, I do not work with Mutable Runtime Reference Image Library? If so, how can I do this? I am attaching a script for loading images into the library and a script for scanning images.
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using System.Collections;
using System.IO;
public class DynamicImageLoading : MonoBehaviour
{
[SerializeField]
private ARTrackedImageManager arTrackedImageManager;
[SerializeField]
private DynamicVideoManager dinamicVideoManager;
private MutableRuntimeReferenceImageLibrary mutableLibrary;
private string imagesFolder;
void Start()
{
imagesFolder = Path.Combine(Application.persistentDataPath, "DownloadedImages");
StartCoroutine(WaitForARSessionAndAddImages());
}
IEnumerator WaitForARSessionAndAddImages()
{
while (ARSession.state != ARSessionState.Ready && ARSession.state != ARSessionState.SessionTracking)
{
yield return null;
}
mutableLibrary = (MutableRuntimeReferenceImageLibrary)arTrackedImageManager.referenceLibrary;
StartCoroutine(AddImagesToLibrary());
}
IEnumerator AddImagesToLibrary()
{
string[] pngFiles = Directory.GetFiles(imagesFolder, "*.png");
string[] jpgFiles = Directory.GetFiles(imagesFolder, "*.jpg");
string[] allImageFiles = new string[pngFiles.Length + jpgFiles.Length];
pngFiles.CopyTo(allImageFiles, 0);
jpgFiles.CopyTo(allImageFiles, pngFiles.Length);
foreach (var imagePath in allImageFiles)
{
Texture2D texture = LoadImageFromPath(imagePath);
if (texture != null)
{
string imageName = Path.GetFileNameWithoutExtension(imagePath);
var jobHandle = mutableLibrary.ScheduleAddImageWithValidationJob(texture, imageName, 0.1f);
while (!jobHandle.jobHandle.IsCompleted)
{
yield return null;
}
if (jobHandle.jobHandle.IsCompleted)
{
Debug.Log($"Image {imagePath} added to the library.");
for (int i = 0; i < mutableLibrary.count; i++)
{
var image = mutableLibrary[i];
Debug.Log($"Image in library: {image.name}");
}
}
else
{
Debug.LogWarning($"Failed to add image {imagePath} to the library.");
}
}
}
dinamicVideoManager.enabled = true;
}
private Texture2D LoadImageFromPath(string path)
{
byte[] imageBytes = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imageBytes);
return texture;
}
}
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class DynamicVideoManager : MonoBehaviour
{
[SerializeField]
private ARTrackedImageManager trackedImages;
void Start()
{
}
void OnEnable()
{
trackedImages.trackablesChanged.AddListener(OnTrackedImagesChanged);
}
void OnDisable() => trackedImages.trackablesChanged.RemoveListener(OnTrackedImagesChanged);
private void OnTrackedImagesChanged(ARTrackablesChangedEventArgs<ARTrackedImage> eventArgs)
{
foreach (var trackedImage in eventArgs.added)
{
string imageName = trackedImage.referenceImage.name;
Debug.Log("Image detected: " + imageName);
StartCoroutine(LoadVideoForImage(imageName, trackedImage.transform));
}
}
private IEnumerator LoadVideoForImage(string imageName, Transform imageTransform)
{
string videosFolder = Path.Combine(Application.persistentDataPath, "DownloadedVideos");
string videoPath = Path.Combine(videosFolder, imageName + ".mp4");
if (System.IO.File.Exists(videoPath))
{
GameObject videoObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
videoObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
videoObject.transform.position = imageTransform.position + Vector3.up * 0.1f;
videoObject.transform.rotation = imageTransform.rotation;
VideoPlayer videoPlayer = videoObject.AddComponent<VideoPlayer>();
videoPlayer.url = videoPath;
videoPlayer.playOnAwake = false;
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
{
yield return null;
}
videoPlayer.Play();
}
else
{
Debug.LogWarning("Video file not found: " + videoPath);
}
}
}