0
\$\begingroup\$

I am working on a game where the user has the possibility to import custom audio tracks that can be listened to during the game.

For this I am making use of System.IO.File.Copy method. This works, there are no issues or flaws here. When I inspect the target and destination folder the file(s) get copied properly and show up in the folder.

The issue however is that the Unity Editor doesn't show the file despite it actually being there. During play mode in the Unity editor the new file isn't shown in the resources folder (which I use as destination for now). When I use windows file explorer I can clearly see the file in the corresponding folder. Using debugs (in Unity) also state that the folder is empty, however when I use debugs outside of Unity it shows that the folder contains the copied item. Keep in mind that these both debugs are called roughly at the same time. I have tested this in many ways to be 100% certain that Unity doesn't see the file, while it is there.

So once I stop playmode it starts loading/importing/processing the new file and then it shows up in the folder in unity as well. When I launch playmode after this the file is indeed loaded and included in the game as well.

I however need this to happen without having to stop playmode. So there is probably some type of processing that I haven't called for Unity to process the addition of the new file and for it to be visible.

So in a nutshell basically copying a file from folder to folder isn't enough for Unity to directly utilized in the same play session (and all after). How can I make the audio-file directly showable and accessable?

There aren't any errors or warnings. The file plays correctly after it has been processed. I am at a loss with this issue/bug.

\$\endgroup\$
3
  • \$\begingroup\$ Does this feature need to work in the built version of the game? If so, copying it to the resources folder is not an appropriate solution: players with just the built version of the game and not the game's source to open in the Unity Editor don't have a folder like this. Those Resource assets are packaged at build time, not while the shipped game is running. \$\endgroup\$
    – DMGregory
    Commented Sep 7, 2018 at 11:12
  • \$\begingroup\$ I was not aware of this. And yes it needs to work during run time in the build version in the game. What would be the appropriate way of achieving this? \$\endgroup\$
    – ploopploop
    Commented Sep 7, 2018 at 14:15
  • \$\begingroup\$ Your comment pushed me towards the right direction, I managed to get it working with the StreamingAssets folder in combination with www in a coroutine. \$\endgroup\$
    – ploopploop
    Commented Sep 7, 2018 at 15:45

1 Answer 1

1
\$\begingroup\$

The comment from DMGregory made me realize that it was the compiler that was processing the new added file. This however only happens outside of playmode and only in the editor. With this came the other knowledge of the Resources folder not being editable in a build. And for me this would be a huge issue since players need to be able to add their own custom soundtracks.

So I actually had 2 issues ongoing without being aware of the second one.

The solution to this was fairly simple, but I wasn't aware of its existence; StreamingAssets. The StreamingAssets folder stays intact and remains editable. This is in a build and in the editor. After making a build you can see the StreamingAssets folder in the [gamename]_Data folder. Using a www form in a coroutine I managed to get the newly added soundtrack to the library of soundtracks and made it directly usable for the player. Mainly this website gave me a lot information about how to utilize StreamingAssets.

I hope this will help future programmers that are going through the pain of making an option for the player to add custom audioclips.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .