Graphics Reference
In-Depth Information
There is one preference file per Web player URL and the file size is limited to
1 MB. If this limit is exceeded, SetInt, SetFloat, and SetString will not store the
value and throw a PlayerPrefsException.
The filename for the preference files this script uses is made up of a combination of the
contents of the string named gamePrefsName and a suffix of _SFXVol. The gamePrefs-
Name string should be set in the Unity editor so that each game has a unique preferences
file.
PlayerPrefs may be formatted in three different ways: floats, integers, or strings, in
this case, a float between 0 and 1 to represent sound volume:
// we will grab the volume from PlayerPrefs when this script
// first starts
volume= PlayerPrefs.GetFloat(gamePrefsName+"_SFXVol");
To store SoundObject instances, an ArrayList is used. The ArrayList needs to be ini-
tialized before any attempts to access it:
soundObjectList=new ArrayList();
Next, the script iterates through the array of AudioClips set in the Unity editor and builds
the SoundObject instances to hold information about each sound. The soundObjectList
ArrayList is populated by the new SoundObject instances.
A foreach loop iterates through each AudioClip in the GameSounds array:
foreach(AudioClip theSound in GameSounds)
{
The constructor function of the SoundObject takes three parameters: the AudioClip,
the name of the sound, and its intended volume (based on the volume value read earlier
from the PlayerPrefs and stored in the variable named volume):
tempSoundObj= new SoundObject(theSound,
theSound.name, volume);
soundObjectList.Add(tempSoundObj);
The integer variable totalSounds counts how many sounds go into the ArrayList to
save having to recount the array each time we need it:
totalSounds++;
}
}
To play a sound, the function PlaySoundByIndex takes two parameters: an index
number (integer) and a position. The index number refers to an AudioClip's position in
the original AudioClip array:
public void PlaySoundByIndex(int anIndexNumber, Vector3 aPosition)
{
Search WWH ::




Custom Search