Context: I’m trying to display and superimpose a video on a cloud image target. Basically, each image target has a dedicated video that should play. Everything is working essentially, the debug log is indicating the correct video url that should be played based on the tracked image.
Issue: the debug log is saying the video is playing, but I don’t see the video being played in the screen.
– If possible, can you guide me to fix the scene and script setup if needed
Here’s the SimpleCloudRecoEventHandler modified to handle playing videos:
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Video;
using Vuforia;
public class SimpleCloudRecoEventHandler : MonoBehaviour
{
CloudRecoBehaviour mCloudRecoBehaviour;
bool mIsScanning = false;
string mTargetMetadata = "";
public ImageTargetBehaviour ImageTargetTemplate;
public FirebaseManager firebaseManager; // Reference to FirebaseManager
public GameObject videoPlayerPrefab; // Reference to the VideoPlayer prefab
private GameObject currentVideoPlayer; // Instance of the VideoPlayer prefab
// Register cloud reco callbacks
void Awake()
{
UnityEngine.Debug.Log("Awake: Registering cloud reco callbacks");
mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>();
if (mCloudRecoBehaviour == null)
{
UnityEngine.Debug.LogError("Awake: CloudRecoBehaviour component not found!");
return;
}
mCloudRecoBehaviour.RegisterOnInitializedEventHandler(OnInitialized);
mCloudRecoBehaviour.RegisterOnInitErrorEventHandler(OnInitError);
mCloudRecoBehaviour.RegisterOnUpdateErrorEventHandler(OnUpdateError);
mCloudRecoBehaviour.RegisterOnStateChangedEventHandler(OnStateChanged);
mCloudRecoBehaviour.RegisterOnNewSearchResultEventHandler(OnNewSearchResult);
}
// Unregister cloud reco callbacks when the handler is destroyed
void OnDestroy()
{
UnityEngine.Debug.Log("OnDestroy: Unregistering cloud reco callbacks");
if (mCloudRecoBehaviour != null)
{
mCloudRecoBehaviour.UnregisterOnInitializedEventHandler(OnInitialized);
mCloudRecoBehaviour.UnregisterOnInitErrorEventHandler(OnInitError);
mCloudRecoBehaviour.UnregisterOnUpdateErrorEventHandler(OnUpdateError);
mCloudRecoBehaviour.UnregisterOnStateChangedEventHandler(OnStateChanged);
mCloudRecoBehaviour.UnregisterOnNewSearchResultEventHandler(OnNewSearchResult);
}
}
public void OnInitialized(CloudRecoBehaviour cloudRecoBehaviour)
{
UnityEngine.Debug.Log("OnInitialized: Cloud Reco initialized");
}
public void OnInitError(CloudRecoBehaviour.InitError initError)
{
UnityEngine.Debug.Log("OnInitError: Cloud Reco init error " + initError.ToString());
}
public void OnUpdateError(CloudRecoBehaviour.QueryError updateError)
{
UnityEngine.Debug.Log("OnUpdateError: Cloud Reco update error " + updateError.ToString());
}
public void OnStateChanged(bool scanning)
{
UnityEngine.Debug.Log("OnStateChanged: Scanning state changed to " + scanning);
mIsScanning = scanning;
if (scanning)
{
UnityEngine.Debug.Log("OnStateChanged: Clearing all known targets");
// Clear all known targets
}
}
// Here we handle a cloud target recognition event
public void OnNewSearchResult(CloudRecoBehaviour.CloudRecoSearchResult cloudRecoSearchResult)
{
UnityEngine.Debug.Log("OnNewSearchResult: New search result found");
UnityEngine.Debug.Log("OnNewSearchResult: Target Name: " + cloudRecoSearchResult.TargetName);
// Store the target metadata
mTargetMetadata = cloudRecoSearchResult.MetaData;
// Stop the scanning by disabling the behaviour
mCloudRecoBehaviour.enabled = false;
// Retrieve the githubPageUrl using the targetId
string githubPageUrl = firebaseManager.GetGithubPageUrlByDocumentId(cloudRecoSearchResult.TargetName);
UnityEngine.Debug.Log("OnNewSearchResult: Retrieved GitHub page URL: " + githubPageUrl);
if (!string.IsNullOrEmpty(githubPageUrl))
{
// Instantiate the video player prefab
if (currentVideoPlayer != null)
{
UnityEngine.Debug.Log("OnNewSearchResult: Destroying existing video player");
Destroy(currentVideoPlayer);
}
UnityEngine.Debug.Log("OnNewSearchResult: Instantiating new video player");
currentVideoPlayer = Instantiate(videoPlayerPrefab, ImageTargetTemplate.transform.position, ImageTargetTemplate.transform.rotation);
// Set the video URL and play the video
VideoPlayer videoPlayer = currentVideoPlayer.GetComponent<VideoPlayer>();
videoPlayer.url = githubPageUrl;
videoPlayer.Play();
UnityEngine.Debug.Log("OnNewSearchResult: Playing video from URL: " + githubPageUrl);
// Set the video player size to match the image target
currentVideoPlayer.transform.localScale = ImageTargetTemplate.transform.localScale;
}
else
{
UnityEngine.Debug.LogError("OnNewSearchResult: No matching targetId found in the library.");
}
}
void OnGUI()
{
// Display current 'scanning' status
GUI.Box(new Rect(100, 100, 200, 50), mIsScanning ? "Scanning" : "Not scanning");
// Display metadata of latest detected cloud-target
GUI.Box(new Rect(100, 200, 200, 50), "Metadata: " + mTargetMetadata);
// If not scanning, show button
// so that user can restart cloud scanning
if (!mIsScanning)
{
if (GUI.Button(new Rect(100, 300, 200, 50), "Restart Scanning"))
{
UnityEngine.Debug.Log("OnGUI: Restarting scanning");
// Reset Behaviour
mCloudRecoBehaviour.enabled = true;
mTargetMetadata = "";
}
}
}
}
Cloud Recognition setup: