0

I'm relatively new to programming in unity and I had never used assetbundles before. I'm using the example project which everyone can download from the unity website and adapting it to my needs, I already know how to use the load scenes function, which is what I'll need, but the load scenes script that I'm currently using doesn't downloads the asset bundles, but loads it from somewhere already in the computer. I'm working on an Android/IOS aplication, our objective is to create a simple menu scene and then DOWNLOAD the asset bundles from a server and load the scenes. All data needs to be stored at the phone once the user downloads it. I tried everything but I can't make it work, even the code at the unity documentation doesn't seems to work for me. Please, if anyone could help me, here's the code for the LoadScenes script. The only modification I've made on the original code that comes with the asset bundle manager of unity is that the name of the bundle, and the name of the scene is passed by a button. This script currently loads the bundles from a folder in the computer, which is not what I need, I need the bundles to be downloaded from a server AND THEN loaded from a folder in the device. Thanks!

using UnityEngine;
using System.Collections;
using AssetBundles;
using UnityEngine.UI;


public class LoadScenes : MonoBehaviour{

public string sceneAssetBundle;
public string sceneName;
public string sName;
public string bName;

// Use this for initialization
IEnumerator Start ()
{   
    yield return StartCoroutine(Initialize() );

    // Load level.
    yield return StartCoroutine(InitializeLevelAsync (sceneName, true) );
}

public void getScene(string sName){
    sceneName = sName;

}

public void getBundle(string bName){
    sceneAssetBundle = bName;

}
    // Initialize the downloading url and AssetBundleManifest object.
public IEnumerator Initialize(){


    // Don't destroy this gameObject as we depend on it to run the loading script.
    //DontDestroyOnLoad(gameObject);

    // With this code, when in-editor or using a development builds: Always use the AssetBundle Server
    // (This is very dependent on the production workflow of the project. 
    //  Another approach would be to make this configurable in the standalone player.)
    #if DEVELOPMENT_BUILD || UNITY_EDITOR
    AssetBundleManager.SetDevelopmentAssetBundleServer ();
    #else
    // Use the following code if AssetBundles are embedded in the project for example via StreamingAssets folder etc:
    AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/");
    // Or customize the URL based on your deployment or configuration
     AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles");
    #endif

    // Initialize AssetBundleManifest which loads the AssetBundleManifest object.
    var request = AssetBundleManager.Initialize();

    if (request != null)
        yield return StartCoroutine(request);
}





public IEnumerator InitializeLevelAsync (string levelName, bool isAdditive)
{
    // This is simply to get the elapsed time for this phase of AssetLoading.
    float startTime = Time.realtimeSinceStartup;

    // Load level from assetBundle.
    AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(sceneAssetBundle, levelName, isAdditive);
    if (request == null)
        yield break;
    yield return StartCoroutine(request);

    // Calculate and display the elapsed time.
    float elapsedTime = Time.realtimeSinceStartup - startTime;
    Debug.Log("Finished loading scene " + levelName + " in " + elapsedTime + " seconds" );
}
}
  • The correct usage of LoadFromCacheOrDownload is to provide a version number with the URL of the asset bundle you're trying to download. That is, you say what version it will be. If you made the call to the exact same bundle with the same version number before, it will try to load it from the cache. If not, it will download it from the server. But your desired behaviour seems to be to just download the file and store it. In that case, don't use LoadFromCacheOrDownload. – Bart May 11 '16 at 20:30

1 Answers1

0

I was confused with the example above so I created my own script to download scene form an asset bundle.After creating asset bundle of a scene or scenes use below code to load scene:-

public class LoadScene : MonoBehaviour {
//public Variables
public string url; // url where your asset bundle is present, can be your hard disk or ftp server
public string AssetBundleName;
public string levelName;
public int version;

//private variables
private AssetBundle assetBundle; 

/*Corountines
By using this, the function will simply stop in that point until the WWW object is done downloading, 
but it will not block the execution of the rest of the code, it yields until it is done.*/
protected IEnumerator LoadTheScene()
{
    if (!Caching.IsVersionCached(url + "/" + AssetBundleName, version)){
        WWW www = WWW.LoadFromCacheOrDownload(url + "/" + AssetBundleName, version);
        yeild return www;
        assetBundle = www.assetBundle;
        www.Dispose();
        if (assetBundle != null)
        {
            string[] path = assetBundle.GetAllScenePaths();
            //below code is for finding the "scene name" from the bundle
            foreach (string temp in path)
            {
                Debug.Log(temp);
                string[] name = temp.Split('/');
                string[] sceneName = name[name.Length - 1].Split('.');
                string result = sceneName[0];
                if (result == levelName)
                {
                    yield return (SceneManager.LoadSceneAsync(result));
                }
            }
        }

    }

    else{
        Debug.Log("Asset Already Cached...");
        yield return Caching.CleanCache();
        //After using an asset bundle you should unload it otherwise an exception will be thrown saying asset bundle is already loaded.. if you use WWW.LoadFromCacheOrDownload again.
    }

}

}

Aryaman Gupta
  • 616
  • 1
  • 8
  • 20