- Home /
How to load multiple wav files
I'm currently developing an app in Unity 3 that will eventually go onto the Iphone. I have managed to get a single sound playing from a WAV. I put the WAV file into my resources folder so that it appeared in the Unity editor. I dragged the resultant audio clip onto a game object which then appeared as an audio source in the inspector for that object. To play the sound I use something like the following code:
//Please add in the code tags with the 101010 next time please =) Thanks! static var mySound : AudioClip;
function Start () { mySound = Resources.Load("sound"); }
function Update () { audio.clip = mySound; audio.Play(); }
Problem is I have over 5000 of these WAV files and I do not want to add each file by hand. What is the best way of adding these files into Unity and easily being able to play them?
$$anonymous$$ake it an array...? Or does that not work?
I find it odd that you're telling it to play on every frame in an Update loop...
Answer by Drum · Mar 25, 2011 at 07:41 AM
I've just had to do something similar, and I did it like this which loads all the audio files from the folder 'audio/foo' within the Resources folder:
// // play a random sound from a folder //
var isPlaying : boolean = false; var thisSound : AudioSource; var myClips : Object[];
function Start(){ myClips = Resources.LoadAll("audio/foo",AudioClip); }
function OnTriggerEnter(){ if(isPlaying == false){ isPlaying = true; audio.clip = myClips[Random.Range(0, myClips.Length)]; audio.Play(); } }
function OnTriggerExit(){ if(isPlaying == true){ isPlaying = false; audio.Stop(); } }
I wanted to choose a sound randomly, but you can use another way of specifying which file to play in the audio.clip = myClips[] line.
Hope this helps.
Answer by RFAronson · May 16, 2012 at 11:30 PM
I had to manage thousands of sound files for two World Series of poker games (most of them dialog). They weren't coded in Unity but this approach should work.
1) Get a list of all the sound files. Assuming you are working in Windows you can do this truly old school by going to the Command Prompt, using CD to go to the folder where those sound files are (they'll need to be in one folder or you'll need to repeat this for each folder), and using the dir command redirected to an output file, e.g.
dir *.wav >output.txt
In this example, output.txt will hold the names of all the sound files, formatted slightly off (a few deletable lines before and after all the sound files, some extra data on each line, and so forth). dir /? will tell you of potentially useful options.
If Microsoft really fixed things, you could probably just open output.txt using notepad, go to the folder where the sound files are, Ctrl A to select them all, Ctrl C to copy them, and then Ctrl V to paste their names into output.txt or an excel spreadsheet, but again, I haven't tried this and don't know what Windows version you use.
1b) There are shareware utilities for under $100 which will speed up that process (we bought one for WSOP back in 2006) or you can write your own text manipulator (as I did back in the 1980's) to help with transforming the list of names into a list of code.
2) Clean up output.txt (or whatever you called it) which can be done quickly if you have an editor that allows macro creation. You'll want to get the file format into something you can cut and paste into Unityscript or C#, whatever you're using. I suggest an array, e.g.
soundList.Add("Sound1.wav"); soundList.Add("Sound2.wav");
and so forth where you were starting with just a file holding:
Sound1.wav Sound2.wav
or more likely
Sound1 wav Size Date
and macroed/massaged it to
Sound1.wav
3) Copy that code into your code. Create the soundList array using whatever kind of array suits you.
3a) I've read that you can create an empty game object and treat it as an array yourself by dropping the 5,000 sound files into it. I've never done that, but I saw a nice blurb on Waypoints here to control vehicle motion. That might be easier; trust the other comments because I'm really old and my way might work but be far less than optimal. Working with the MSDOS, er, Command prompt is not as dangerous as editing the Registry, but it's not something Microsoft likes us to do.
4) Now you can populate a given clip indexed at n from soundList[n].
5) Assuming you have reasons for those thousands of sound files, I urge you to start first by coming up with an intelligent naming convention to manage them. You probably already have done so. For example, for World Series of Poker, Phil Hellmuth might have had sound files named:
PH_FOLD_00 PH_FOLD_01 PH_RAISE_00
etc. to handle what game (poker) situation was being covered. All of Phil's dialog was prefixed with PH (everybody's had a prefix) and the suffixes were to avoid having him say the same thing over and over again (in this example, there are two fold lines). Excel is a fine tool for managing thousands of sound files; you can open a new worksheet for each voice, have the files transcribed (in case you need to translate to another language or display text onscreen), and you can copy and paste the list of sound files right into Excel.
Good luck.
P.S. I see a spurious http:// in front of file names like output dot text. I don't know why. I don't see it in the preview. I don't know why it's there. If you see it, ignore it. And no matter what my wife says, I'm not crazy.
Answer by pepworks · Jan 17, 2011 at 07:21 PM
// YOU CAN LOAD THE SOUNDFILES FROM THE RESOURCES FOLDER BY A LIST:
private var i: int=0;
private var var soundfiles: String[];
soundfiles=["0pickcrystal","1intowater", .........];
for (i=0; i<2; i++) {
audios[i] = Resources.Load(soundfiles[i]);
}
Answer by pepworks · Jan 17, 2011 at 07:12 PM
// PUT THE SOUNDFILES WITH DRAG AND DROP INTO YOUR ARRAY
public var audios: AudioClip[];
// I USE 3 EMPTY GAME OBJECTS WITH ONLY AUDIO SOURCES ATTACHED,
// sx & sy can be used to move them to get 2D/ 3D (add sz ;) stereo fx later
var source1: GameObject;
var source2: GameObject;
var source3: GameObject;
private var so: int=0;
function sfx(sound: int, sx: float, sy: float) {
so++; if (so>3) { so=1; }
if (so==1) { source1.audio.PlayOneShot(audios[sound]);
}
if (so==2) { source2.audio.PlayOneShot(audios[sound]);
}
if (so==3) { source3.audio.PlayOneShot(audios[sound]);
}
}
Answer by jtbentley · Jun 05, 2012 at 02:55 AM
Well, you can do it easily with..
var soundIndex : int = 0;
var mySound : AudioClip;
function PlaySound()
{
mySound = Resources.Load( ("soundFile_"+soundIndex), AudioClip);
audio.PlayOneShot(mySound);
}
All you need to do is change soundIndex to a different number. It will load the files "soundFile_1", "soundFile_2", etc and so on.