Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Why the asset bundle slow in StreamingAssets on android device?

Discussion in 'Editor & General Support' started by watsonsong, Apr 29, 2016.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    143
    I am loading the resource in Editor using AssetDatabase is fast, but when I export the assetbundle, copy them into StreamingAssets, and load them using WWW on android device, it really very slow make my game can not play.
    I see many people copy them asset bundle from StreamingAssets to persistentData path at the first time when the application launch, is it useful to speed up the load time, and why is it?
    Thank you~
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    2,717
    Hey,

    What platform are you building for? Only Android?
    Also, did you measure your game's performance using the profiler? what takes most of the time?

    On Android, your game is deployed to the device in an .apk file. This is essentially a zipped file with some internal structure. While deploying your game with StreamingAssets, these files are actually stored inside the .apk (e.g: zipped).

    In order to access them, Unity allows you to easily access the bundles under streaming assets using the WWW class. Internally, it probably uses some Android APIs for accessing these files (AssetManager?) which probably have some overhead.

    What we do, in order to directly access our asset bundles under StreamingAssets, is to add native android code that copies the bundles from streaming assets to another folder that is directly accessible from code (using System.IO.File), and then we can load them directly. This actually runs pretty quick (although our bundles are very small in size, so YMMV).
     
  3. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    143
    Hi, @liortal
    I want to check this method, copy the AssetBundle from StreamingAssets to other folder.
    It takes time when the user first launch, and take twice space than normal. I know many game do this way, but I want to find is there any good API to process it to avoid this copy?
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    2,717
    As i said, you can still use the WWW class (WWW.LoadFromCacheOrDownload). It knows how to handle files in StreamingAssets. Internally, it may do the same thing (copy the files out) though i am not sure.
     
  5. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    143
    I am using WWW.Load data from StreamingAssets, but it very slow.
     
  6. ForceMagic

    ForceMagic

    Joined:
    Feb 27, 2015
    Posts:
    35
    It is better to copy it outside of the StreamingAssets because on Android it's inside a Jar file, so there is an additionnal it to uncompress it.

    But even there, overall file access on Android seems pretty slow, cause us to have slow loading time when loading data from files.
     
  7. Vincent-Zhang

    Vincent-Zhang

    Unity Technologies

    Joined:
    Jan 23, 2013
    Posts:
    83
    If you go with WWW.LoadFromCacheOrDownload(), it will uncompress the file and store it in the cache folder on the device first, then load the cached file instead.
     
  8. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    72
    Just to clarify - files in StreamingAssets folder are not compressed with zip and packaged as raw data to apk. You can see them in a transient file raw.ap_ in Temp folder while building.

    You can also use AssetBundle.LoadFromFile to load bundle directly from StreamingAssets folder.
    Depending on compression method it will be either decompressed to memory (LZMA compressed bundle with default options) or decompressed/read when loading data from it (LZ4 or uncompressed bundle).
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    2,717
    @alexeyzakharov can you explain what do you mean by "files in StreamingAssets folder are not compressed with zip" ?

    When the APK is installed, does it get uncompressed? or do the files under StreamingAssets are stored in a compressed format (inside the APK) ?
     
  10. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    72
    Hi,

    StreamingAssets are packaged into APK with -0 flag for AAPT tool (or -0 -Z for fastzip). Meaning that correspondent segment in APK is uncompressed. APK is a zip container which consists of blocks of potentially different compression types. You can use e.g. APK Analyzer to check what's compressed and what's not.
    We don not extract and store any data from APK during installation or any other time. Player's data is streamed and decompressed when requested by LoadScene or LoadAsset API. Android-specific resources are decompressed by OS.
    StreamingAssets file are considered as "foreign" files which can be used by any external code (including direct access by file descriptor) and be already compressed (video, audio).
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    2,717
    So, can i directly access files from streaming assets using Syste.IO APIs, like reafing files, etc?

    Also, on android, can i directly load an asset bundle via AssetBundle.LoadFromFile from streaming assets?
     
  12. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    72
    No. StreamingAssets are not supported for System.IO API. You can access them only with Java code.

    Yes, since 5.3, see e.g. example for AssetBundle.LoadFromFile.
     
  13. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    121
    Is WWW.LoadFromCacheOrDownload the best option on Android phones?
    In our game it makes our first load 3-10x slow than subsequent runs. (20-50seconds vs 5 seconds, it is dependent on LZMA vs LZ4, which is understandable)