Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 dryheat70 · May 01, 2012 at 05:12 PM · assetbundlewww

Pausing Until AssetBundle Downloads

We currently have a system where assets are loaded via ResourceController.INSTANCE.Load(assetName). There is a FileSystemResourceController, an AssetBundleResourceLoader and a UnityResourceLoader (derived from ResourceController)

These handle editor , standalone , web(scorm) asset loading differently. We are moving towards having all our lesson specific assets in their own bundles which means calls like ResourceController.INSTANCE.Load("level1config.xml") need to "wait" until the bundle that contains the asset is downloaded (in web player case). The code immediately following needs to have that file loaded to continue. Has anyone else solved something similar? (ie an AssetLoader whose purpose it is to return assets from bundles ...that might need to wait for bundles to be downloaded before proceeding?) thanks, --matt

Comment
Add comment · Show 1
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
avatar image dannyskim · May 01, 2012 at 07:00 PM 0
Share

do you $$anonymous$$d posting your code for your derived class so that we can deter$$anonymous$$e a good place to pause at?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by cregox · Jun 11, 2013 at 10:54 PM

Kinda late reply, and I'm not sure what you're asking, but I do a lot of work with bundles and yours seems like a pretty straight-forward question. If I'm right, this should have helped ya if posted early enough:

Usage

 static public IEnumerator CoDownload () {
     FileHandler fileHandler = new FileHandler();
     
     IEnumerator e = fileHandler.Download("http://www.example.com/index.html");
     while ( e.MoveNext() )
         yield return e.Current;
     
     if ( fileHandler.DownloadOk() )
         fileHandler.Save("somewhere/somefile.html");
     else
         Debug.LogError("error on download: " + fileHandler.www.error);
 }


FileHandler.cs

 using System.IO;
 using UnityEngine;
 using System.Collections;
 
 /// 
 /// File handler, wait for file download and do the saving process.
 /// 
 public class FileHandler {
     public WWW www { get; protected internal set; }
     
     public IEnumerator Download (string url) {
         www = new WWW(url); // initiate download
         yield return www;
     }
     
     public bool Save (string into) {
         bool savedOk = false;
         if (www != null && www.isDone && www.error == null) {
             FileStream stream = null;
             try {
                 stream = new FileStream(
                     into
                     , FileMode.Create);
             } catch (System.SystemException exception) {
                 Debug.LogError("[FileHandler] FileStream error, which should never happen: "+ exception.Message);
             }
             if (stream != null) {
                 stream.Write(www.bytes, 0, www.bytes.Length);
                 stream.Close();
                 savedOk = true;
             } else {
                 Debug.LogError("[FileHandler] Couldn't write to file because stream is null: "+ into);
                 if (www == null || www.error == null) {
                     Debug.LogWarning("[FileHandler] www or error is null  -  *most likely* the file to download was changed and this is not being loaded any more: "+ into);
                 } else {
                     Debug.LogError("[FileHandler] Couldn't save file!! "+ into +" error: "+ www.error +"  error length? "+ www.error.Length);
                 }
             }
         }
         Dispose();
         return savedOk;
     }
     
     public void Dispose () {
         www.Dispose();
         www = null;
     }
     
     public bool DownloadOk () {
         if (www == null) {
             return false;
         } else if (www.error != null) {
             return false;
         }
         return true;
     }
 }
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
avatar image
0

Answer by dryheat70 · May 02, 2012 at 04:46 PM

I'm afraid that would be a lot of code. The key hangup, however, is that we have code throughout similar to

 levelConfigXML =  ResourceController.INSTANCE.Load("level1config.xml");
 // parse levelConfigXML and assume it's already been loaded
 

// Below is pseudo code

 class AssetBundleResourceLoader : IResourceController
 {
    WWW bundleLoader; 
    String CurrBundleName;
    Object Load(string path)
    {
       String bundleName  = GetBundleFromPath(path);
       if(bundleName != CurrBundleName)
       {
          bundleLoader = RequestController.RequestAndWaitForLoad(CurrBundleName);
       }
       Object result = null;
       if(bundleLoader != null && bundleLoader.error == null && bundleLoader.isDone)
       {
           if(bundleLoader.assetBundle != null)
           {
               result = bundleLoader.assetBundle.Load(path);
           }
        }
        if(result == null)
        {
           // fallback to "parent" loader
           result = base.Load(path);
        }
     return result;
   }

   class WWWRequestController
   {  
      WWW RequestAndWaitForLoad(string url)
      {
         // have made many attempts here to essentially start a d/l of bundle and "wait" for it to complete. How have others solved this?
      }
    }
       

 
 

{

}

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
avatar image
0

Answer by dryheat70 · May 02, 2012 at 09:46 PM

found a good link to a page explaining walking through an IEnumerable yourself Unity3D coroutines in detail

that I thought might help, but haven't had much luck... still loops through with WWW.isDone = false and no error in WWW.error.

but, wasn't able to get it to effectively block until download was complete with the following code...

     public WWW RequestAndWaitForLoad (string url)
     {        
     isLoadingBundle = true;
     Debug.Log(String.Format("##WWW requesting {0}", url));        
     

     int test = 0;
     IEnumerator e = FinishDownload(url);
     while(e.MoveNext()) 
     { 
         Debug.Log(String.Format("##count:{0}", test++));
     }        
                                         
     Debug.Log(String.Format("##RequestAndWaitForLoad after startcorouting "));        
     Debug.Log(String.Format("##isLoadingBundle is {0}", isLoadingBundle));        
     Debug.Log(String.Format("##wwwResults is {0} null", wwwResults == null ? "" : "NOT"));
     return wwwResults;        
 }
 
 
 protected IEnumerator FinishDownload(string url)
 {
     WWW theWWW = new WWW(url);        
     if(theWWW.error != null)
     {
         Debug.Log(String.Format("##FinishDownload breaking with error : {0}", theWWW.error));
         yield break;                    
     }
     yield return theWWW;        
     
     Debug.Log(String.Format("##FinishDownload Ater first yield isDone is  : {0}", theWWW.isDone));
     if(theWWW.error != null)        
     {
         Debug.Log(String.Format("##FinishDownload Ater first yield isDone is  : {0}", theWWW.isDone));                    
     }
     
     wwwResults = theWWW;
     yield break;        
 }
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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

3D model is not loading from url 1 Answer

Unload asset bundle make problem 0 Answers

asset bundles download problem 0 Answers

WWW::Dispose doesn't work 1 Answer

How to Load all textures with WWW class? But not with Assets Bundles 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges