By kidsoul
on Wed, 05/20/2015 - 03:10
Hi all,
I am trying to use AssetBundle but I got a problem. On AssetBundleAugmenter.cs like above:
public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler { public string AssetName; public int Version; private GameObject mBundleInstance = null; private TrackableBehaviour mTrackableBehaviour; private bool mAttached = false; public bool targetIsFound = false; void Start() { StartCoroutine(DownloadAndCache()); mTrackableBehaviour = GetComponent<TrackableBehaviour>(); if (mTrackableBehaviour) { mTrackableBehaviour.RegisterTrackableEventHandler(this); } } void Update(){ if (targetIsFound) { StartCoroutine (DownloadAndCache ()); mTrackableBehaviour = GetComponent<TrackableBehaviour> (); if (mTrackableBehaviour) { mTrackableBehaviour.RegisterTrackableEventHandler (this); } targetIsFound = false; } } // Update is called once per frame IEnumerator DownloadAndCache() { while(!Caching.ready) yield return null; // example URL of file on PC filesystem (Windows) // string bundleURL = "file:///D:/Unity/AssetBundles/MyAssetBundle.unity3d"; // example URL of file on Android device SD-card string bundleURL = "http://17.16.71.1/3dmodel/agentwalking.unity3d"; using (WWW www = WWW .LoadFromCacheOrDownload(bundleURL, Version)) { yield return www; if (www .error != null) throw new UnityException("WWW Download had an error: " + www .error); AssetBundle bundle = www .assetBundle; if (AssetName == "") { mBundleInstance = Instantiate (bundle.mainAsset) as GameObject; } else { mBundleInstance = Instantiate(bundle.Load (AssetName)) as GameObject; } } } public void OnTrackableStateChanged( TrackableBehaviour.Status previousStatus, TrackableBehaviour.Status newStatus) { if (newStatus == TrackableBehaviour.Status.DETECTED || newStatus == TrackableBehaviour.Status.TRACKED || newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED) { if (!mAttached && mBundleInstance) { // if bundle has been loaded, let's attach it to this trackable mBundleInstance.transform.parent = this.transform; mBundleInstance.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f); mBundleInstance.transform.localPosition = new Vector3(0.0f, 0.15f, 0.0f); mBundleInstance.transform.gameObject.SetActive(true); //mBundleInstance.animation.Play ("agentwalking"); Debug.Log("OnTrackableStateChanged_____AssetBundleAugmenter: "); mBundleInstance.transform.Rotate (Vector3.up * 15 * Time.deltaTime); mAttached = true; } } } }
agentwalking.unity3d is created by Prefab ( drag ImageTarget from Hierarchy to Project then BuildAssetBundle ). Prefag ImageTarget has child that called agentwalk . See image attached.
And this is my SimpleCloudHandler.cs:
public class SimpleCloudHandler : MonoBehaviour, ICloudRecoEventHandler { private CloudRecoBehaviour mCloudRecoBehaviour; private bool mIsScanning = false; public ImageTargetBehaviour ImageTargetTemplate; private string mTargetMetadata = ""; GameObject agentwalk; // Use this for initialization void Start () { agentwalk = GameObject.Find ("agentwalk"); // register this event handler at the cloud reco behaviour mCloudRecoBehaviour = GetComponent<CloudRecoBehaviour>(); if (mCloudRecoBehaviour) { mCloudRecoBehaviour.RegisterEventHandler(this); } } void Update(){ agentwalk.animation.Play ("agentwalking"); } 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()); } public void OnStateChanged(bool scanning) { mIsScanning = scanning; if (scanning) { // clear all known trackables ObjectTracker ot = TrackerManager.Instance.GetTracker<ObjectTracker>(); ot.TargetFinder.ClearTrackables(false); } } // Here we handle a cloud target recognition event public void OnNewSearchResult(TargetFinder.TargetSearchResult targetSearchResult) { // do something with the target metadata mTargetMetadata = targetSearchResult.MetaData; // stop the target finder (i.e. stop scanning the cloud) mCloudRecoBehaviour.CloudRecoEnabled = false; // Build augmentation based on target if (ImageTargetTemplate) { // enable the new result with the same ImageTargetBehaviour: ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>(); ImageTargetBehaviour imageTargetBehaviour = (ImageTargetBehaviour)tracker.TargetFinder.EnableTracking( targetSearchResult, ImageTargetTemplate.gameObject); } } void OnGUI() { // Display current 'scanning' status GUI.Label (new Rect (50,50,150,50), mTargetMetadata); // If not scanning, show button // so that user can restart cloud scanning if (!mIsScanning) { if (GUI.Button(new Rect(50,100,120,50), "Rescan")) { // Restart TargetFinder mCloudRecoBehaviour.CloudRecoEnabled = true; } } } }
My questions:
1. Do Vuforia SDK auto sync after AssetBundle downloading model (.unity3d) ? More explain, Is "agentwalk" found after download?
2. What should I do on SimpleCloudHandler.cs to use model downloaded?