Is there anyway I can get an audioclip by the name it has in the assets folder? Or will I always have to make a reference to it by dragging and dropping the clip on a SoundClip in the inspector?
\$\begingroup\$
\$\endgroup\$
2
-
\$\begingroup\$ If you put your sounds into the Resources folder they can be loaded by name via the Resources.Load() command. \$\endgroup\$– CalvinCommented Oct 19, 2013 at 20:02
-
\$\begingroup\$ That is very interesting. Thanks. However i meant that if i have audioClip called "DeathScream05" id like to play it with something like: audio.PlayOneShot("DeathScream05"); \$\endgroup\$– DaarwinCommented Oct 19, 2013 at 20:51
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
3
There is no way to achieve this out of the box. Unity requires an AudioClip reference for the PlayOneShot method.
You'll need to write a simple audio manager to achieve this, or simply include a reference to the clip in your script.
-
\$\begingroup\$ Any tips on how to get started with a audiomanager? \$\endgroup\$– DaarwinCommented Oct 20, 2013 at 9:21
-
\$\begingroup\$ Create an AudioManager prefab and instantiate it when you load your scene. This prefab will contain your script component. You'll want to create a public Array of audio clips to expose to the editor (so that you can easily drop them in in the inspector). in Awake(), run through your array/list and add them to a Dictionary<string, AudioClip> Lastly, create a static method PlayByName(string clipName) or whatever you want to call it. Then this method can get the AudioClip out of the Dictionary using it's name as the key. Hope this helps. \$\endgroup\$ Commented Oct 20, 2013 at 9:44
-