Add Your Own Music to Daggerfall Unity

Show off your mod creations or just a work in progress.
User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

I meant load it at run time from an external directory, so it doesn't have to be pre-added into the Unity build. So for example you could set it up so your script would look for the music folders at the root location of where DaggerfallUnity is being executed from, and load songs from there.

Here's an example function I whipped up. StartCoroutine("LoadFiles", "DirectoryWithMusicFiles"); - one problem with this method is that Unity apparently disallows MP3s for windows (and linux I'm guessing) standalone builds. .wav & .ogg both work though.

It uses the application data path as root directory right now (which is Assets in editor mode). So you if you add a MUSIC folder to Assets for testing, just pass "MUSIC" in to the location paramater.

One issue left to solve is actually saving the playlists, they're currently just being discarded in the function.

Code: Select all


        IEnumerator LoadFiles(string location)
        {
            string path = Path.Combine(Application.dataPath, location);
            if(!Directory.Exists(path))
            {
                Debug.LogWarning("Couldn't locate external music files at " + path);
                yield break;
            }
            var files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);


            //filter out unplayable files
            files = files.Where(file => file.ToLower().EndsWith(".wav") || file.ToLower().EndsWith(".ogg")).ToArray();

            //no music files to play
            if (files == null || files.Length < 1)
                yield break;

            AudioClip[] songList = new AudioClip[files.Length];	//need solution to save playlists
            AudioClip clip;
            WWW www;

            int count = 0;
            foreach(string filePath in files)
            {
                www = new WWW("file://" + filePath);
                while (!www.isDone)
                    yield return new WaitForEndOfFrame();

                clip = www.GetAudioClip(false,false);
                while (clip.loadState == AudioDataLoadState.Loading)
                    yield return new WaitForEndOfFrame();

                if(clip != null && clip.loadState == AudioDataLoadState.Loaded)
                    songList[count++] = clip;

                www.Dispose();

            }
        }



User avatar
EpicSpellsword404
Posts: 50
Joined: Mon Sep 21, 2015 12:26 am

Re: Add Your Own Music to Daggerfall Unity

Post by EpicSpellsword404 »

Sorry for the noob question, but where exactly would I put that code? There seems to be an error wherever I put it.

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

Not a problem - I'm guessing you were missing some using statements that were required to make it work. Sorry, I shouldn't have assumed you would know about that :)

I put it together in a more comprehensive example for you here.

https://drive.google.com/file/d/0B8_oQw ... sp=sharing
https://drive.google.com/file/d/0B8_oQw ... sp=sharing

There are 2 files, one goes on a gameObject, the other is just a simple editor script that adds some buttons and such to the inspector. It's best to put it into an "Editor" folder.

User avatar
EpicSpellsword404
Posts: 50
Joined: Mon Sep 21, 2015 12:26 am

Re: Add Your Own Music to Daggerfall Unity

Post by EpicSpellsword404 »

Update - You can now add files at runtime!
https://drive.google.com/file/d/0Bx8--6 ... sp=sharing
I'll keep working on it and clean things up. Thanks for the help.
Last edited by EpicSpellsword404 on Sun May 08, 2016 11:02 pm, edited 2 times in total.

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

It's great to see you're still working on this, it's a great idea! ;) I see you're using resources to load the music - are you sure that's the way you want to do it? Resources requires people to add the files into a unity project then build it (the files have to be converted to unity audio clips), which not many people can or are willing to do.

An alternative is the WWW class, which will load the files from outside the compiled project, and it will convert them to audio clips.

User avatar
EpicSpellsword404
Posts: 50
Joined: Mon Sep 21, 2015 12:26 am

Re: Add Your Own Music to Daggerfall Unity

Post by EpicSpellsword404 »

Sorry. I did use www, just posted the wrong link :oops:

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

EpicSpellsword404 wrote:Sorry. I did use www, just posted the wrong link :oops:
Ah, that explains it! :D Cool, I can't wait to test it out ;)

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

OK, I just had a chance to try it, and it works, very cool. :)

Some general comments (I hope you don't mind!) - the LoadFiles() Enumerator can be simplified by a lot and reduce the amount of repeated code.

I didn't think of it before, but it's probably a better idea to load the files as needed, rather than all of them. You'd basically just build up a list of valid file names, then use those strings to load files on demand. When I have the time I'll try and put together an example.

User avatar
EpicSpellsword404
Posts: 50
Joined: Mon Sep 21, 2015 12:26 am

Re: Add Your Own Music to Daggerfall Unity

Post by EpicSpellsword404 »

Don't worry, I appreciate the comments. I am trying to get better at this after all :D
I'm going to clean up the code and make it less repetitive this weekend hopefully.
About the other thing, that's probably a good idea. Do you mean make a new list of files every time, or just have a list in the ini file or something that can be changed when necessary?

User avatar
LypyL
Posts: 512
Joined: Sun Mar 22, 2015 3:48 am

Re: Add Your Own Music to Daggerfall Unity

Post by LypyL »

EpicSpellsword404 wrote: About the other thing, that's probably a good idea. Do you mean make a new list of files every time, or just have a list in the ini file or something that can be changed when necessary?

It would work pretty similarly as it does now, except after this part:

Code: Select all

			
			var files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
			files = files.Where(file => file.ToLower().EndsWith(".wav") || file.ToLower().EndsWith(".ogg")).ToArray();
rather than building up List<AudioClip> for each playlist, use List<string>, and save the valid strings from the files variable to each list. Then when it comes to play a song, you just grab one of those strings (which is the full path to the song) and load it with the WWW class pretty much like you're doing now, and play it.

It would be nice to be able to save / load play lists, but that's getting a good deal more complicated.

Post Reply