- Home /
DirectoryInfo().GetFiles works incorrectly in Standalone
So, i'm making adding your own music possible in my game and i hit a problem which is: I'm using a function to look for a folder, search for files in it, save them in array and play them in random order. It works perfect in unity editor but when i compile it for standalone, it is not so cool... i debugged what does GetFiles() function return in both cases and i got:
full file path + filename - in editor && only filename in standalone
so I'm assuming that it is an internal unity bug as some other things stopped working for me after Unity 4 came to my PC... here's code I'm using:
private var IsAnyMusicInFolder : boolean = false;
private var CheckMusicFolder : boolean = false;
private var ReadyToPlay : boolean = false;
private var MusicPlaying : boolean = false;
var MusicTrackID : float = 0;
private var NoMusicTimer : float = 0;
private var RandMusicID : float = 0;
private var ExtMusicFile : WWW;
function LoadMusicFromMusicFolder (){
if (Directory.Exists(Application.dataPath+"/Music")){var info = new DirectoryInfo(Application.dataPath+"/Music");}
if (!info){CheckMusicFolder = true;}else{
if (!CheckMusicFolder){var fileInfo = info.GetFiles("*.ogg");}
if (!CheckMusicFolder && fileInfo.length > 0){
MusicAssignIDRandomizer.Clear();
IsAnyMusicInFolder = false; ReadyToPlay = false; MusicTrackID = 0;
MusicFilesFromExtSrc = new String[fileInfo.length];
for (i=0; i < fileInfo.length; i++){
RandMusicID = Random.Range(0, MusicFilesFromExtSrc.length);
if (ArrayUtilityInt(RandMusicID, MusicAssignIDRandomizer) == false){
MusicFilesFromExtSrc[i] = fileInfo[RandMusicID].ToString();
Debug.Log (MusicFilesFromExtSrc[i]);
MusicAssignIDRandomizer.Add (RandMusicID);
} else {i--;}
}
IsAnyMusicInFolder = true; CheckMusicFolder = true;
} else {CheckMusicFolder = true;}
if (CheckMusicFolder && IsAnyMusicInFolder){
if (!ReadyToPlay){
Destroy(audio.clip);
ExtMusicFile = new WWW ("file://"+MusicFilesFromExtSrc[MusicTrackID]);
//else{ExtMusicFile = new WWW ("file://"+Application.dataPath+"/Music/"+MusicFilesFromExtSrc[MusicTrackID]); Debug.Log("file://"+Application.dataPath+"/Music/"+MusicFilesFromExtSrc[MusicTrackID]);}
audio.clip = ExtMusicFile.GetAudioClip(false, false);
ReadyToPlay = true;
}
if (!audio.isPlaying){NoMusicTimer += Time.deltaTime;
if (NoMusicTimer > 0.1 && audio.clip.isReadyToPlay){Debug.Log("Fuck");audio.Play();}
} else {NoMusicTimer = 0;}
if (audio.clip.length-audio.time<0.5 && audio.isPlaying){
if (MusicTrackID < MusicFilesFromExtSrc.length-1){
MusicTrackID++;
} else {CheckMusicFolder = false;}
ReadyToPlay = false;
}
if (Input.GetKeyDown("/")){
if (MusicTrackID < MusicFilesFromExtSrc.length-1){
MusicTrackID++;
} else {CheckMusicFolder = false;}
ReadyToPlay = false;
}
}
}
}
function ArrayUtilityInt (RandNumber : float, ObjArray : Array){
for (i = 0; i < ObjArray.length; i++){
if (ObjArray[i] == RandNumber){
return true;
}
}
}
Answer by Landern · Dec 12, 2012 at 01:30 PM
DirectoryInfo().GetFiles() returns an array of FileInfo type... if you don't need all the extra weight in the type and you only want the *.ogg files as an array of string, just use:
....
var somePathStrings : String[] = Directory.GetFiles(someDirectoryPath, "*.ogg");
....
Answer by ArkaneX · Dec 06, 2013 at 02:20 PM
Please use this
fileInfo[RandMusicID].FullName
instead of
fileInfo[RandMusicID].ToString()
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity-workspace gets Black when starting game 0 Answers
Unity plays imported animations incorrectly 1 Answer
i get weird rotations on my animated objects using legacy, help? 0 Answers
More Unity bugs, sometimes it keeps restarting character position to (0,0,0) 0 Answers