How to include files into the Build and copy them to persistenDataPath

Hello,
I’m producing a game for Android, iOS, WebGL and Windows, so I need a solution compatible with all these platforms.

I have some files that I need to include with the Build when I compile my project. When the game starts, I have to copy these files to the Application.persistenDataPath folder, where I expect to have all the assets my game will save, load, and use.

So, the questions are:

  1. Where should I put my files so that Unity includes them in the Build when it compiles? In a standard folder like Resources or StreamingAssets?

  2. How can I copy those files into the Application.persistenDataPath folder, considering the various platforms’ limitations as, for example, that on Android they are inside an APK, and in the StreamingAssets folder I cannot use the System.IO.File class?

Thanx!

If you want the files to be copied verbatim to the build folder and accessible via System.IO then use StreamingAssets.
Resources folder contents are also always included in a build but have to be loaded with the Resources class and end up being Unity assets (ie Texture2D) which require additional steps to write back in a source format (ie PNG).

Well, you use the FileUtil class helpers like CopyFileOrDirectory for the platforms that support it, and for Android and Web you use WebRequest as the manual states. There’s no way around branching code paths here.

Be sure to test any system you create foremost with WebGL because it is most likely to fail there, and it’ll be the slowest to copy the contents.

Ultimately the big question here is: How many files will you copy? How many megabytes will these files occupy?
It should be way less than 100 megabytes. With every additional megabyte, or if you have many small files, the time it takes to copy all that content is going through the roof rather quickly. And the amount of space available for persistentData path may be limited, and may be less than available disk space. Be sure to include behaviour for when disk space runs out.

It may be a lot more efficient to provide a single zip file with the contents and then unzipping those contents in memory, writing the unzipped files to the persistentData path. You can use .NET GZipStream class, it should work on WebGL too.

Hi @CodeSmile
thanx for your reply.

So, if I understand correctly, I put the files in the StreamingAssets folder and then use the FileUtil class or the WebRequest class to copy the files, depending on the platform.

About your question, the files are managed to have the lowest size for the quality I want to maintain. Generally speaking, I’m talking about 2-3 megabytes.

Exactly.

And a few MB should be fine.