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 *= fileInfo[RandMusicID].ToString();*
_ Debug.Log (MusicFilesFromExtSrc*);_
_ 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 == RandNumber){
return true;
}
}
}*_