The Sound System is a general purpose sound handler written in C# for game development projects in the Unity environment. It is designed to allow developers and/or sound designers to centralise control and handling of all sound effects and music.
Soundscapes are defined in XML and loaded from within Unity. The system itself is comprised of a single gameObject with a set of public static functions that can be called from any other script in the same scene. This mitigates the need for Unity AudioSources to be attached to specific gameObjects, allowing them to be instantiated dynamically at runtime using simple function calls.
Setting Up the System
1. Import the SoundSystem unity package into your project
By default, the system and all attached scripts will be placed in the Standard Assets folder.
If your project uses C#, you may choose to move the SoundSystem to a different location according to the specific requirements of the project.
If your project primarily uses JavaScript or Boo, it is recommended that you leave the Sound System in the Standard Assets folder due to potential compile order issues with non-C# scripts accessing the Sound System.
2. If you have not already done so, import the audio files for use in your project.
These will need to be placed in a Resources folder. An empty Resources folder is provided in the unity package. However, according to the Unity documentation, Resources folders can exist anywhere in the project hierarchy as long as they are correctly named.
3. Setup the XML resource file
The XML template is called SoundSystemXML. You can edit this file directly, or create a copy so that you have a template to refer to.
The XML file should be included in the same Resources folder as your Audio files. This ensures that it will be included when you build the scene. If it is not, you may encounter problems with the audio not playing when exporting your project as a standalone or web player build.
There are three kinds of objects that can be defined in the XML file (I can't display the XML correctly on the blog because the tags look like html and the text doesn't show up).
SoundEffects, which represent a single sound that is played once or looped (eg. Character noises, environment sounds, looping ambient tracks. Can also be used for music). This is basically a single sound clip.
SoundSets, which represent a set of related sounds that can be played randomly once or at set intervals (eg various similar gunshots or explosions, bird calls, lines of character dialogue that can be selected randomly for NPCs). This can have as many clip elements as you like. The numclips property must match the number of clips elements you have included.
SoundPairs, which represent sounds that have an 'initialisation' followed by an indefinite loop (eg. A car starting and then the engine idling for a random period of time). This has two clip elements – the first one listed will be the initialisation sound, and the second the looping sound.
For each object, name is the name you call the object by using sound system functions, and clip refers to the import name of the clip in the Unity Inspector. Keeping these things distinct can be useful if you are using effects from various sources and need to keep track of attribution. All of the name properties you assign must be unique, or the system will not be able to load the sounds correctly.
There can be as many SoundEffects, SoundPairs and SoundSets as you like within the XML file, and they can be placed in any order within the SoundScape.
Using the SoundSystem methods
In order to use the SoundSystem methods, you will first need to place the SoundSystem prefab somewhere in your scene.
In the inspector, you will need to change the public variable XMLFilePath on the prefab to reflect the location in the project of your XML file. The default example is Assets/Standard Assets/Resources/SoundSystemXML.
SoundSystem methods are called from any script in the same scene as follows:
SoundSystem.methodName(“soundName”, objToPlayAt);
where soundName is a string and objToPlayAt is a gameObject (in most cases this will be the gameObject attached to the script you are calling the sound system from.
Some methods are overloaded to allow the user to specify volume as well. In later iterations, these will be further extended to provide finer control over how the sounds play.
The functions available in the current build of the SoundSystem are
playOnce(String Name, GameObject Caller)
This plays a one shot sound using Unity's playClipAtPoint() functionality and can be called on any clip loaded into the sound system, even ones contained within a SoundSet or SoundPair.
play(String Name, GameObject Caller)
play(String Name, GameObject Caller, float volume)
These play the sound object with the specified name once and parent it to the Caller object. Can be called for any kind of the three sound objects.
startLooping(String Name, GameObject Caller)
startLooping(String Name, GameObject Caller, float volume)
These start the sound object with the specified name looping and parent it to the Caller object. Can be called for any kind of sound object.
stopLooping(String Name, GameObject Caller)
This will stop the named clip from playing, and turn off looping. Can be used to cut a playing sound short if required. Can be called for any kind of sound object.
playAtInterval(String Name, GameObject Caller, float interval)
playAtInterval(String Name, GameObject Caller, float minInterval, float maxInterval)
playAtInterval(String Name, GameObject Caller, float minInterval, float maxInterval, float volume)
This will start the named SoundSet playing a random clip at the specified interval, or at a random interval between the specified max and min intervals. Can currently only be called for a SoundSet object, a SoundEffect or SoundPair will simply do nothing and log a SoundSystem warning.
I will upload and post a link to the current version of the Unity Package, so if anyone wants to play with it they can. I will also be updating this documentation as I further develop the system - it's still very much in the early stages.
No comments:
Post a Comment