Hello! =)
I managed to get Vuforia Cloud Recognition working with Asset Bundles. When a Cloud Reco target is scanned, I get a URL to Firebase from the metadata of that target (targetMetadata.FirebaseURL). It works almost perfectly, except one issue…
The issue I am having is – After recognizing the first target, “target #1”, it downloads the asset bundle and loads it as an Additive Scene to that target and it all works perfectly!
But when I aim at a different target, let’s say “target #2”, it just loads the same asset bundle scene from target #1.
I want target #1 and target #2 to load different asset scene bundles and load them accordingly.
Whichever target I recognize first is the asset bundle that gets attached to all targets INSTEAD of loading a new asset bundle for new targets.
If I restart the app on my device and aim at target #2 then it loads a different asset bundle like I want, but I don’t want to have to restart my app for every new Vuforia Cloud Target.
What am I overlooking?
To clarify, I want to use the targetMetadata to download Asset Scene Bundles from Firebase and Load that scene bundle Additively to that specific Vuforia Cloud Target. Right now, it places the initial downloaded asset scene bundle on all targets instead of downloading and switching to a different asset bundle for different Cloud Target.
using Firebase.Storage;
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using Vuforia;
using UnityEngine.UI;
public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler
{
private CloudRecoBehaviour _cloudRecoBehavior;
private ImageTargetBehaviour ImageTargetTemplate;
private AssetBundle bundle;
private TargetMetadata targetMetadata;
public Text debugText;
private bool loaded = false;
private bool _isScanning = false;
void Start()
{
CloudRecoBehaviour cloudRecoBehavior = GetComponent<CloudRecoBehaviour>();
if (cloudRecoBehavior != null)
{
cloudRecoBehavior.RegisterEventHandler(this);
}
_cloudRecoBehavior = cloudRecoBehavior;
}
public void OnStateChanged(bool scanning)
{
debugText.text = "Scanning : " + scanning;
_isScanning = scanning;
if (scanning)
{
ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
tracker.TargetFinder.ClearTrackables(false);
}
}
public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult)
{
if (!loaded)
{
targetMetadata = JsonUtility.FromJson<TargetMetadata>(targetSearchResult.MetaData);
FirebaseStorage storage = FirebaseStorage.DefaultInstance;
StorageReference storage_ref = storage.GetReferenceFromUrl(targetMetadata.FirebaseURL);
debugText.text = Time.time + targetMetadata.FirebaseURL;
storage_ref.GetDownloadUrlAsync().ContinueWith((Task<Uri> task) =>
{
if (!task.IsFaulted && !task.IsCanceled)
{
Debug.Log("Download URL: " + task.Result);
// ... now download the file via WWW or UnityWebRequest.
StartCoroutine(LoadBundleEnableTracking(task.Result, targetSearchResult));
}
});
}
if (loaded)
{
ImageTargetTemplate = FindObjectOfType<ImageTargetBehaviour>(); //dmr
if (ImageTargetTemplate != null)
{
GameObject newImageTarget = Instantiate(ImageTargetTemplate.gameObject) as GameObject;
if (ImageTargetTemplate)
{
ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
ImageTargetBehaviour imageTargetBehaviour = tracker.TargetFinder.EnableTracking(targetSearchResult, newImageTarget);
}
}
if (!_isScanning)
{
_cloudRecoBehavior.CloudRecoEnabled = true;
}
}
}
public IEnumerator LoadBundleEnableTracking(Uri uri, TargetFinder.TargetSearchResult targetSearchResult)
{
if (!loaded)
{
using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(uri))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
bundle = DownloadHandlerAssetBundle.GetContent(uwr);
string[] scenePath = bundle.GetAllScenePaths();
yield return SceneManager.LoadSceneAsync(scenePath[0], LoadSceneMode.Additive);
loaded = true;
ImageTargetTemplate.enabled = false;
}
}
}
if (loaded)
{
yield return null;
}
}
public void OnInitialized()
{
Debug.Log("Cloud Reco initialized");
}
public void OnInitError(TargetFinder.InitState initError)
{
Debug.Log("Cloud Reco init error " + initError.ToString());
}
public void OnUpdateError(TargetFinder.UpdateState updateError)
{
Debug.Log("Cloud Reco update error " + updateError.ToString());
}
}
If you can help me figure out what I am missing or overlooking I will love you forever!
THANK YOU