Packing and loading asset files (midi) in Unity

  Kiến thức lập trình

I considered if this would be more of a Unity or specific library question, but considering it’s tied to the loading of assets, it might come in handy to know of a workaround if there is any.

I’m using the DryWetMidi library with Unity, and have a number of midi files I can load and swap in runtime. DWM loads midi files from a file path, so I just keep them in a folder and build the file path from Application.dataPath + filename before loading. However, when the project is built, the files no longer are at the given path (unless you manually place them there afterwards).

Unity doesn’t have a variable type that can store midi files (unless you store it as .bytes file), but even so, DWM needs the filepath to load the midi.

Could there be a way to go around this that I’m not seeing? I’ve looked at using Resources, but that doesn’t really allow me to tie in with the library’s loading method.
I thought about using resources (or storing the files as byte files) to load the files during runtime, copy and create a temp file to load it with the library, and afterward delete it, but am unsure if that is overkill and there might be a simpler way to do it.

Many thanks!

5

As said what you want to go with would be the StreamingAssets folder and Application.streamingAssetsPath.

Those assets are either shipped alongside with the build or even packed into the resulting apk (depending on your target platform).


The Application.dataPath you are using is actually just the install folder / project root folder again depending on your target platform and mostly not accessible at all in a build.


Also not to confuse with Application.persistentDataPath which is an external path which allows the application to persistently store and access data. This you would e.g need to use if you want your user to be able to modify and save modified midi files later.

LEAVE A COMMENT