Graphics Reference
In-Depth Information
10.1.1 Script Breakdown
After all of the variable declarations, the first part of this script does the initial setup within
a Start() function. Remember that, in Unity, the Start function is automatically called by the
engine after a script is initialized and before the first update. Here, the script checks to see
whether or not PlayerPrefs data have already been saved to disk. If it finds PlayerPrefs keys
that exist, it loads them in and applies the saved values to our local variables. To store slider
values from the options menu, there are three variables used. These are:
1. audioSFXSliderValue, a float, with a value between 0 and 1, for the sound effects
volume level.
2. audioMusicSliderValue, a float, with a value between 0 and 1, for the music vol-
ume level.
3. graphicsSliderValue, an integer to store the level of graphics detail. The num-
ber acts as an index used to set Unity's QualitySettings quality level with the
SetQualityLevel function.
void Start()
{
// set up default options, if they have been saved out to
// prefs already
if(PlayerPrefs.HasKey(gamePrefsName+"_SFXVol"))
{
audioSFXSliderValue= PlayerPrefs.
GetFloat(gamePrefsName+"_SFXVol");
} else {
audioSFXSliderValue= 1;
}
if(PlayerPrefs.HasKey(gamePrefsName+"_MusicVol"))
{
audioMusicSliderValue= PlayerPrefs.
GetFloat(gamePrefsName+"_MusicVol");
} else {
audioMusicSliderValue= 1;
}
if(PlayerPrefs.HasKey(gamePrefsName+"_GraphicsDetail"))
{
graphicsSliderValue= PlayerPrefs.
GetFloat(gamePrefsName+"_GraphicsDetail");
} else {
string[] names = QualitySettings.names;
detailLevels= names.Length;
graphicsSliderValue= detailLevels;
}
// set the quality setting
QualitySettings.SetQualityLevel( (int)graphicsSliderValue,
true);
}
Search WWH ::




Custom Search