Search Unity

Asset Bundle download progress bar

Discussion in 'Asset Bundles' started by jorge_nasciment, Feb 8, 2019.

  1. jorge_nasciment

    jorge_nasciment

    Joined:
    Apr 3, 2017
    Posts:
    5
    I'm having a hard time developing an app with asset bundle and vuforia: the app is able to download the asset bundle normally when it recognizes the marker, and sets the model to this marker. But the client does not know whether the asset bundle is being downloaded or not, especially in larger files, which require a little more time to download. I wonder if anyone of you can help me put up an indication for as long as the app is downloading the asset bundle.

    Below is my code, adapted from the site of the vuforia:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vuforia;

    public class AssetBundleAugmenter : MonoBehaviour, ITrackableEventHandler {
    public string AssetName;
    public int Version;
    private GameObject mBundleInstance = null;
    private TrackableBehaviour mTrackableBehaviour;
    private bool mAttached = false;
    void Start() {
    StartCoroutine(DownloadAndCache());
    mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    if (mTrackableBehaviour) {
    mTrackableBehaviour.RegisterTrackableEventHandler(this);
    }
    }
    // 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 = "https://www.dropbox.com/s/zjkullh4exz8jew/horse?dl=1";

    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.LoadAsset(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.0f, 0.0f);
    mBundleInstance.transform.gameObject.SetActive(true);
    mAttached = true;
    }
    }
    }
    }
     
    aryan_abbhi likes this.
  2. aryan_abbhi

    aryan_abbhi

    Joined:
    Nov 25, 2015
    Posts:
    4
    same problem here cant able to find solution...
     
  3. aryan_abbhi

    aryan_abbhi

    Joined:
    Nov 25, 2015
    Posts:
    4
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. using System.IO;
    6. using UnityEngine.SceneManagement;
    7. using Vuforia;
    8. public class assetbundlesceneloader : MonoBehaviour , ITrackableEventHandler
    9. {
    10.     public string url;
    11.     public string assetname;
    12.     public int downloaded = 0;
    13.     AssetBundle bundle;
    14.     public System.Object test;
    15.     public Slider progressbar;
    16.     public float progress;
    17.     private GameObject mBundleInstance = null;
    18.     private TrackableBehaviour mTrackableBehaviour;
    19.     private bool mAttached = false;
    20.     public GameObject destroyables;
    21.  
    22.  
    23.  
    24.     WWW www;
    25.     void Update()
    26.     {
    27.         progress = www.progress;
    28.         progressbar.value = progress;
    29.     }
    30.  
    31.     IEnumerator Start()
    32.     {
    33.         mTrackableBehaviour = GetComponent<TrackableBehaviour>();
    34.         if (mTrackableBehaviour)
    35.         {
    36.             mTrackableBehaviour.RegisterTrackableEventHandler(this);
    37.         }
    38.  
    39.  
    40.  
    41.         ClearCacheExample();
    42.         if (downloaded == 0)
    43.         {
    44.             using (www = WWW.LoadFromCacheOrDownload(url, 0))
    45.             {
    46.                 yield return www;
    47.                 if (www.error != null)
    48.                     throw new Exception("WWW download had an error:" + www.error);
    49.                 if (www.error == null)
    50.                 {
    51.                     bundle = www.assetBundle;
    52.                     if (assetname == "")
    53.                     {
    54.  
    55.                         mBundleInstance = Instantiate(bundle.mainAsset) as GameObject;
    56.                         //mBundleInstance.transform.parent = this.transform;
    57.  
    58.  
    59.                     }
    60.                     else
    61.                     {
    62.                         mBundleInstance = Instantiate(bundle.LoadAsset(assetname)) as GameObject;
    63.                         mBundleInstance.transform.parent = this.transform;
    64.                     }
    65.                 }
    66.             }
    67.             if (Caching.ready == true)
    68.             {
    69.                 downloaded = 1;
    70.                 Destroy(destroyables);
    71.             }
    72.         }
    73.     }
    74.  
    75.     void ClearCacheExample()
    76.     {
    77.         Directory.CreateDirectory("Cache1");
    78.         Directory.CreateDirectory("Cache2");
    79.         Directory.CreateDirectory("Cache3");
    80.  
    81.         Caching.AddCache("Cache1");
    82.         Caching.AddCache("Cache2");
    83.         Caching.AddCache("Cache3");
    84.  
    85.         bool success = Caching.ClearCache();
    86.  
    87.         if (!success)
    88.         {
    89.             Debug.Log("Unable to clear cache");
    90.         }
    91.     }
    92.  
    93.     public void OnTrackableStateChanged(
    94.           TrackableBehaviour.Status previousStatus,
    95.           TrackableBehaviour.Status newStatus)
    96.     {
    97.         if (newStatus == TrackableBehaviour.Status.DETECTED ||
    98.             newStatus == TrackableBehaviour.Status.TRACKED ||
    99.             newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
    100.         {
    101.             if (!mAttached && mBundleInstance)
    102.             {
    103.                 // if bundle has been loaded, let's attach it to this trackable
    104.                 mBundleInstance.transform.parent = this.transform;
    105.                 //mBundleInstance.transform.localScale = new Vector3(1f, 1f, 1f);
    106.                 mBundleInstance.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
    107.                 mBundleInstance.transform.gameObject.SetActive(true);
    108.                 mAttached = true;
    109.  
    110.             }
    111.         }
    112.     }
    113. }
    114.  
    115.  
     
    melvard likes this.
  4. aryan_abbhi

    aryan_abbhi

    Joined:
    Nov 25, 2015
    Posts:
    4
    Jorge_nasciment try the above code it works for me...…… sorry for delayed reply .
     
  5. mehmetsamast

    mehmetsamast

    Joined:
    Mar 12, 2019
    Posts:
    2
    Hello, I have same problem. I used aryan_abbhi's code but asset bundle scene download is very slow (about 30 second) from google drive. how can I reduce download time server? please help me...
     
  6. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    Hi mehmet, super delayed reply but for anyone who's wondering in the future:
    There are three main components to how quickly you can download files from a server, those being:
    1. The maximum output speed on the server's network adapter
    2. How far the data needs to travel in order to reach the user (ping)
    3. The maximum input speed on the user device's network adapter and the internet speed of the end user.
    Obviously, as a developer you don't have control over 3, but you can try to change aspects 1 and 2 as best you can. For example:

    1 (Server) - Putting your files on a generic cloud like Google drive is totally fine but you don't really have any control on the server in this regard. I highly recommend learning to use more advanced cloud services like AWS for example. They will allow you for practically free host your files there, and you have control over how strong or basic each part of your server will be.
    2 (Distance) - Again, with more advanced cloud solutions you have something called "Edge locations". These cache data like the thing you're hosting on the cloud in a location that's physically close to your end-users. This can ensure that distance won't be an issue for your users, and also lowers pressure on your actual cloud server.
     
    aryan_abbhi likes this.