Search Unity

How would one include a file in an Android build, and then access it later?

Discussion in 'Android' started by DanTaylor, Nov 22, 2018.

  1. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Hi there,
    I am trying to do something specific, and, after a lot of forum searching) I can't quite work out the details.
    I need to include a particular file in my Android build, and the point to it later in Unity.
    My questions are basically...

    A) Where in my build structure should I drop the file? (E.g. /Plugins/Android/res )
    B) What do I use in Unity to reference the file location? ( E.g. Application.persistentDataPath )

    To give you a little more info, the file I want to include in my build is a very specifically structured .jpg, which I then want to pass to a social sharing API.

    Thanks in advance for your help!

    Cheers,
    Dan
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    StreamingAssets perhaps?
     
  3. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Interesting tip, Aurimas-Cernius - thanks!
    I had a look at this, and it was almost exactly what I am looking for!
    Unfortunately it seems that, on Android, Streaming Assets are compressed and can only be accessed by WWW.

    Maybe I could load and uncompress the data into a byte array with WWW, then write it to disc for future use, but this seems a little extreme (especially as disc space is at a premium on mobile devices).

    Is there a simpler way to do this? Somehting that doesn't compress the assets, so I can access tehmn directly from the devices storage?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    StreamingAssets are not compressed. They are inside apk, so yes, you have to use UnityWebRequest or WWW to read them, but they are stored to apk without compression. So if you are on a fairly recent Unity version, reading them shouldn't be significantly slower compared to regular files on disk.
     
  5. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167
    Alternative: use Assets/Plugins/Android/assets folder to provide files which are packed unchanged to the APK. They can be accessed later using correspondent Java APIs.
     
  6. DanTaylor

    DanTaylor

    Joined:
    Jul 6, 2013
    Posts:
    119
    Thanks, Yury. That sounds perfect... but a little outside my area of expertise... do you have a link with more information I can investigate? :D
     
  7. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,167