Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndyF · Jan 17, 2011 at 04:20 PM · audioloadingresourcesmass

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Jan 17, 2011 at 04:35 PM 0
Share

lowercase i, Capital P.

avatar image Justin Warner · Jan 17, 2011 at 06:13 PM 0
Share

$$anonymous$$ake it an array...? Or does that not work?

avatar image jtbentley · Jun 05, 2012 at 02:53 AM 0
Share

I find it odd that you're telling it to play on every frame in an Update loop...

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1
Wiki

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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]);

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AndyF · Jan 18, 2011 at 04:05 PM 0
Share

$$anonymous$$any thanks for your help.

avatar image
-1

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]);
}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to prevent resources being unloaded unintentionally 0 Answers

Loading music for initial scene loading takes a very long time 1 Answer

Loading audio dynamically iOs 1 Answer

Accesing and Loading resources best practices 0 Answers

Loading/mounting alot of assetbundles 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges