I need to create a radio, so I think about creating an array with the music I have in a folder on my assets. Then play it randomly depending on the station. Changing the station will not be a problem but I can’t do the array properly. Can you help me?
AssetDatabase.LoadAllAssetsAtPath is what you’ll want to use. It returns an array of all Objects at a given path.
Note that this is an Editor only function. What that means is that you’ll have to create an editor script to populate your array, and use the populated array at runtime. Essentially you’ll be declaring a collection at edit time via script, much like you would drag assets from the project pane into the inspector.
If you want to do this at runtime (not recommended), you’ll have to go the resources path.
Resources.LoadAll( “pathname”) will give you what you want, essentially an AudioClip[ ] array of all files under a certain folder “pathname” (that must be under a Resources folder).
You can even break down the various pieces of music into separate “Radio Station” folders underneath that, and load each folder independently, or all at once.
I would NOT go the route of prepopulating an array with an editor script, unless you enjoy fragile toolchains where you can accidentally forget to refresh the folder before you build, and then the music is out of date. The only value that provides is that you can hand-edit the list before building, but if you’re including everything, you are only making extra bookkeeping work for yourself.
NOTE: all of this assumes the music files are part of your project at build time, and live under a folder named Resources/.