Search Unity

Resources vs StreamingAssets for mobile.

Discussion in 'Editor & General Support' started by fronzu, Sep 11, 2017.

  1. fronzu

    fronzu

    Joined:
    Nov 1, 2016
    Posts:
    24
    Hi all,
    I never really understood the difference in placing and loading assets at runtime in the Resources folder or in the StreamingAssets folder.

    Is it the same? If not, which one should I use for mobile development?

    Thanks in advance and have a nice day!
     
    akuno likes this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Pikkukala likes this.
  3. fronzu

    fronzu

    Joined:
    Nov 1, 2016
    Posts:
    24
    I missed the second article, thanks!
    So it is recommended to use the StreamingAssets solution because Resources is too much memory intense?
    If so, I wonder why it is still an available option when StreamingAssets is better and serve the same purpose... or did I miss something?
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Resources and StreamingAssets are conceptually different things and one isn't recommended over the other, because they have different purposes.

    Whatever you throw in StreamingAssets, is by-passing Unity's import pipeline, means: Unity does not know what the file is. For example, if you throw a texture in StreamingAssets, you can't reference that texture in the project.
     
  5. fronzu

    fronzu

    Joined:
    Nov 1, 2016
    Posts:
    24
    Uhm okay, but I still can't understand what are the advantages of using one instead of the other.

    I understood that Resources is good for prototyping and to be used in the editor, and that StreamingAssets holds the files as they are without the import modifications.

    So why can't I always use Resources? Or why should I use StreamingAssets? What are the advantages?
    I mean, you quoted an example from the official Unity guides, but why couldn't I load the video directly from Resources?


    Sorry for the stream of questions but I can't really figure it out by myself even after reading the guides.
     
    arielfel likes this.
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,717
    They have completely different purposes. Your question is like asking the advantages of using a knife instead of a spoon.

    Resources folder imports assets normally, like it does for all assets and converts them to internal formats that make sense for the target platform. So png in the resources folder, get turned into a ETC compressed texture when you build.

    A png in the streaming assets folder is still a png when you're building. SteamingAssets is saying to Unity, "just include those files as is, you don't need to know what they are, I'll deal with them".
     
  7. UDN_8513fa23-131a-4126-9928-c274044208d7

    UDN_8513fa23-131a-4126-9928-c274044208d7

    Joined:
    Nov 20, 2016
    Posts:
    1
    How can i load SQLite database on android using this StreamingAssets Folder?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    You can use UnityWebRequest to load the file, but you'll have the raw bytes of the database.
    On Android StreamingAssets are inside APK, which is a zip file.
     
  9. Tom-Mensink

    Tom-Mensink

    Joined:
    Jul 27, 2017
    Posts:
    17
    There is truth in most answers, but I think no real clear comparison is provided. So far I understand it like this:
    • Resources: A way of referencing assets by filename instead of GUID rerefences (the normal way).
      • Direct GUID references are made by refering to an asset in an Inspector property. This tells the builder to include the asset in the application during build time.
      • The assets in the Resources folder(s) on the other hand, can be loaded via script through their file names during runtime. One drawback is that they use additional memory in the app, even when the assets are not used. Also when there are many (1000s) assets in the Resources folder, building up a reference tree will take additional time at startup. Finally using file paths is more error prone. There are a few use cases for Resources folders, e.g. when there is no GUID because you created an asset (like a mesh) yourself during run time. For a better explanation see The Knights of Unity.
    • Streaming Assets: A way of storing files to be included in the built app.
      • Any files placed in a folder called StreamingAssets (case-sensitive) in a Unity project will be copied without change to a particular folder on the target machine. Unity will not know these files as assets of a certain type. These files can be referenced there also from other apps. The file address for external apps is machine dependent, as discussed in the link. When referencing the assets from your own code, you use unity's methods. This is different for Android. Drawback of StreamingAssets is that it is readonly
     
    Last edited: Nov 11, 2018
  10. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    Extra experience in real use case for Android app

    • Resources:
      • I am pretty sure it not load all assets during start up like document say. I am not sure why but it only pre-load stuff that was reference GUID in first scene.
      • For heavy texture that not yet used in scene like image file, It will not be load directly to RAM not until something reference to it
    • Streaming Assets:
      • It did not save any more space on Android. Depend on compress type, it actually took more space than normal resource file
      • It actually load more slower than Resources because it have to unpack everything each time game start up. And yes, Android did not cache unpack file from asset streaming. So the game will use heavy CPU on game start up to unzip asset file => heated phone. You can avoid this by divide asset into many smaller streaming asset file
    Edit: Ok Streaming assets did not worsen performance in anyway. I was mistaken it with Asset Bundle Compression.
     
    Last edited: Nov 14, 2018
  11. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,731
    No. StreamingAssets are not compressed. They are inside zip, but they are stored without being compressed, so reading them will not heavily use the CPU.
     
  12. southrop

    southrop

    Joined:
    May 14, 2018
    Posts:
    4
    I'm pretty sure the docs don't say all assets are loaded during start up. It says that it computes an index for all the assets, so they are all serialised, but that doesn't mean they are loaded.
     
  13. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    Sorry for bringing this thread back up. My question is,

    I am making a platformer game with 100+ levels, each level is serialized and stored in a binary file.
    After going through this thread it seems like StreamingAssets is the right place to store the levels? Any suggestions? Thank you
     
  14. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    When I build to the Android platform (Oculus Go), and I place 4 video files into the StreamingAssets folder with a total size of around 3GB (the total size of the .apk is pretty much the same), the application crashes as soon as I start it. If the .apk file stays under 2GB (when using 2 videos for example), it runs fine. My guess is that RAM gets filled with the video files for some reason. But that shouldn't happen when they are in StreamingAssets?
     
  15. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    This depends on your video player and its memory behavior.
    I tested Ocolus go with over 15 GB of video and if i only play one or two at the same time its working fine. (Using AVPRO Video)
     
  16. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    The app does not crash when I start the video. The app crashes when I start the app. However, it seems to be fixed now. I think rebuilding the Library folder and restarting the project helped for some reason. I guess some old references, which when on prefabs were still there and the videos were loaded into memory on app startup. That's probably why it crashed.
     
  17. Wattosan

    Wattosan

    Joined:
    Mar 22, 2013
    Posts:
    460
    What actually fixed this is using IL2CPP instead of mono and setting the Target Architecture to ARM64.