- Home /
How can unity read a directory?
Hallo
I am making a sound list where the player of the game can put his/her own music in.
But for that unity must read a directory outside the _data folder.
How can unity do that?
Does anybody know the answer?
I have something like it but it only reads the string.
var path = Application.dataPath+"/music/";
function Update ()
{
var files = (path+"*.mp3");
print(files);
}
Answer by Ony · Sep 05, 2010 at 12:46 AM
Try this:
import System.IO;
var filesList = new Array();
function ReadDirectory() {
var path = Application.dataPath+"\\music\\";
var info = new DirectoryInfo(path);
var fileInfo = info.GetFiles();
for (file in fileInfo)
{
var filename = file.ToString();
filename = filename.Replace(Application.dataPath+"\\music\\", "");
filesList.Push(filename);
}
print(filesList);
}
i get this error: Assets/music player!.js(14,9): BCE0005: $$anonymous$$ identifier: 'filesList'. do you mean with filelist file name?
filesList is an array. I figure you have some array you want to add the filenames to and I just chose filesList as the name out of the blue. I've edited my code to add the declaration for the array.
you helpt me a lot but how do i play the music?? i know how to do that if the .mp3 files are in unity but how to do that with this script?
That's a totally different question you'll want to ask in a different post as you're more likely to get an answer that way. $$anonymous$$y code is simply to read a directory, but playing the music files in it is a whole other thing. If my answer helped it would be great if you checked it as the correct answer to this particular question.
Answer by Cyclops · Sep 04, 2010 at 06:00 PM
Here's another example, Desktop game?, showing how to read a file. Note that only standalone executables can read files on the player's machine. The web-browser version can not, for security reasons.
Unity will not automatically expand wildcards for you, like "*.mp3", that is something you need to using .Net directory functions.
Also - the Update() function runs every frame - you probably want to move your file-reading to a function that runs once, called either in Start(), or via button click or something.
On second reading - it's not clear from your question, if you mean to read files on the user's machine, or if you're allowing the user to upload files to your server. Can you clarify what you're asking for?
i just wanna use the .mp3 files as music :) nothing else
Your answer
![](https://koobas.hobune.stream/wayback/20220613072709im_/https://answers.unity.com/themes/thub/images/avi.jpg)