• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jasminetay · Dec 01, 2015 at 02:38 PM · assetbundledownloadload sceneurl

Loading scene from assetbundle (url)

I want to load the scene using www but it fails to download and shows a null. I'm not sure why it doesn't download.

 using UnityEngine;
 using System.Collections;
 using AssetBundles;
 
 
 public class loadingscene : MonoBehaviour
 {
     public string sceneAssetBundle;
     public string sceneName;
     public string url;
     
     // Use this for initialization
     IEnumerator Start ()
     {    
         yield return StartCoroutine(Initialize() );
         
         // Load level.
         yield return StartCoroutine(InitializeLevelAsync (sceneName, true) );
     }
     
     // Initialize the downloading url and AssetBundleManifest object.
     protected 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.SetSourceAssetBundleURL(url);
         //    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);
     }
     
     protected IEnumerator InitializeLevelAsync (string levelName, bool isAdditive)
     {
         WWW www = WWW.LoadFromCacheOrDownload (url, 1);
         yield return www;
         if (www.error != null) {
             print ("WWW donwload had an error : " + url + " " + www.error);
                 //Debug.Log("");
             }
         AssetBundle bundle = www.assetBundle;
         Debug.Log(www.assetBundle.mainAsset);
 
         // 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" );
         bundle.Unload(false);
         // Frees the memory from the web stream
         www.Dispose();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by David-Berger · Dec 02, 2015 at 10:04 AM

If you want to "download" the asset Bundle from the StreamingAssets folder you need to change

 AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/");

to something like

 AssetBundleManager.SetSourceAssetBundleURL(Application.streamingAssetsPath+"/");

More infos are here, here and here.

However if you want to download the Asset Bundles from the web you need to remove the / from line 36 of your posted code.

 /AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles");

to

 AssetBundleManager.SetSourceAssetBundleURL("http://www.MyWebsite/MyAssetBundles");

Adapt the link and comment out the local call

 AssetBundleManager.SetSourceAssetBundleURL(Application.dataPath + "/");

before, otherwise it looks up the local path instead of the url.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Float to Integer 1 Answer

Error when downloading AssetBundle from external server during runtime 0 Answers

Unity 5 Download AssetBundles from server via URL. 2 Answers

Problems get OBB File form GooglePlay Store 2 Answers

Download assetbundles with Webapp 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges