Search Unity

Accessing File Stream at runtime (Android)

Discussion in 'Scripting' started by BenJB, Jun 26, 2019.

  1. BenJB

    BenJB

    Joined:
    Jul 16, 2013
    Posts:
    10
    The game I'm working on has a level generator that uses a ~30MB custom binary file to provide some data. The level generator itself cannot use any Unity libraries - its just a .dll I made that will be used in a non-unity project as well (the server application) so cannot contain unity specific code. The file will be loaded in it's entirety once every time the player starts a new save file (infrequent). Currently it just loads the file using a System.IO BinaryReader. This works fine in the editor as it can access the file at the project root.

    The issue is that the generator cannot access this file on builds as it is not included in the builds themselves. I'm targetting Android (iOS, Windows and webGL in the future) so that rules out StreamingAssets. I've also tried using C#'s embedded resources, but that was strangely slow, and seems like bad practice for such a large file especially since it will be used very infrequently.

    Is there any way I can include the file in builds and use it to provide the world generator with a Stream (or even better a filepath) from which it can access the data?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It looks like the limitation you have is that you need to use the WWW class to access StreamingAssets on Android. You could use WWW to copy the file to Application.persistentDataPath, maybe?
     
  3. BenJB

    BenJB

    Joined:
    Jul 16, 2013
    Posts:
    10
    Thanks for the quick reply. I got caught up in some other stuff. The WWW class seems to be marked as obsolete on the docs now, but it appears to have been superceded by UnityWebRequest. I'll check it out in the morning. Thanks!