Dynamic Loading from File System

Hi all,

Been tearing out my hair over this. We are building a game that dynamically loads some files (via System.IO and checks the directory via a call to Directory.GetFiles) from the file system…mainly a few textures and a few music files. The reason for this is so a player can add his/her own music tracks for example to use via the ingame music manager…and also perhaps change the textures (modding).

This is working all nice and fine, right up to version 4.5 of Unity. Upon upgrading to version 5, the system still works in the editor. If I choose “Build and Run”…still works too. But, if we choose “Build”, then run the generated game .exe, we get an error that for example Assets/Resources/Sound/BGM" does not exist.

Was something changed with how Unity handles the file system in version 5? Is there any way around this or we can no longer get files from the system dynamically? The test system is Windows 7 and Windows 8.

Thank you.

Can you show us the entire error?

You give us a fragment of:

Well… if the folder you’re attempting to access is ‘Assets/Resources/Sound/BGM’. Well… if that’s a windows machine, that folder is not an existing folder on any windows machine… unless it’s a relative folder (mac/linux machines could have that as a folder as it does follow unix naming conventions).

If it’s a relative folder though, well… does the built version in its installed directory contain such a folder hierarchy relative to the where the game is ran from? If not, there’s your problem.

Hi,

That’s the error “Assets/Resources/Sound/BGM does not exist”. The thing that I’m confused is that it used to work previously. Obviously, that directory doesn’t exist in the “_data” folder that Unity generates…but I thought Unity will preserve the directory hierarchy when it bundles/compresses all the stuff under Resources/GameData.

If Unity doesn’t preserve the directory structure, I guess I’ll have to do a #if of some sort to know if the game is being run in the Editor or as a standalone itself.

Thank you.

Assets placed inside Resources get built into resources.assets and are loaded via Resources.Load().
Assets placed inside StreamingAssets are copied when built, Application.streamingAssetsPath gives you the path to this folder.

As a side note - using System.IO to load stuff in Assets has never worked outside the Editor (because the files don’t exist in the built version of the executable) so something was different between your processes to make it work.

As others have mentioned, you can either use Resources.Load or manually place your files post-build somewhere it would have access to and use WWW or System.IO

Thank you, this worked like a charm :slight_smile: