Created
August 17, 2015 02:53
-
-
Save keiranlovett/f0d968657a8f25655707 to your computer and use it in GitHub Desktop.
Unity: Player Settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public static class Settings | |
{ | |
#region Constant Variables | |
/// <summary> | |
/// The Name of the Player | |
/// </summary> | |
/// <remarks>This defaults to null, so that if you wish to display something else, you may.</remarks> | |
public const string DefaultPlayerName = null; | |
/// <summary> | |
/// The Network Interpolation, measured in percentage. | |
/// Ex. 10 is equal to 10% | |
/// </summary> | |
public const float DefaultInterpolation = .1f; | |
/// <summary> | |
/// The Overall Volume of the Sound and Music. | |
/// </summary> | |
public const float DefaultVolume = .5f; | |
/// <summary> | |
/// The Percentage of the Mouse Sensitivity. | |
/// </summary> | |
public const float DefaultMouseSensitivity = .5f; | |
/// <summary> | |
/// The Field of View of the Camera. | |
/// </summary> | |
public const int DefaultFieldOfView = 70; | |
/// <summary> | |
/// Whether the Y-axis is inverted in-game. | |
/// </summary> | |
public const bool DefaultInvert = false; | |
/// <summary> | |
/// Whether we should have Vertical Sync enabled to remove screen tearing. | |
/// </summary> | |
public const bool DefaultVSync = true; | |
#endregion | |
#region Property Variables | |
private static string _playerName; | |
public static string PlayerName | |
{ | |
get | |
{ | |
if (_playerName == null) | |
{ | |
_playerName = DefaultPlayerName; | |
} | |
return _playerName; | |
} | |
set | |
{ | |
_playerName = value; | |
PlayerPrefs.SetString("Settings/PlayerName", _playerName); | |
} | |
} | |
private static float? _interpolation; | |
/// <summary> | |
/// The Networking Interpolation | |
/// </summary> | |
public static float Interpolation | |
{ | |
get | |
{ | |
if (!_interpolation.HasValue) | |
{ | |
_interpolation = DefaultInterpolation; | |
} | |
return _interpolation.Value; | |
} | |
set { _interpolation = value; } | |
} | |
private static float? _volume; | |
/// <summary> | |
/// The Volume Level | |
/// </summary> | |
public static float Volume | |
{ | |
get | |
{ | |
if (!_volume.HasValue) | |
{ | |
_volume = DefaultVolume; | |
} | |
return _volume.Value; | |
} | |
set { _volume = value; } | |
} | |
private static float? _mouseSensitivity; | |
/// <summary> | |
/// The Sensitivity of the Mouse | |
/// </summary> | |
public static float MouseSensitivity | |
{ | |
get | |
{ | |
if (!_mouseSensitivity.HasValue) | |
{ | |
_mouseSensitivity = DefaultMouseSensitivity; | |
} | |
return _mouseSensitivity.Value; | |
} | |
set { _mouseSensitivity = value; } | |
} | |
private static int? _fieldOfView; | |
/// <summary> | |
/// The Field of View of the Camera | |
/// </summary> | |
public static int FieldOfView | |
{ | |
get | |
{ | |
if (!_fieldOfView.HasValue) | |
{ | |
_fieldOfView = DefaultFieldOfView; | |
} | |
return _fieldOfView.Value; | |
} | |
set | |
{ | |
_fieldOfView = value; | |
} | |
} | |
private static bool? _invert; | |
/// <summary> | |
/// Y-Axis Look Inversion | |
/// </summary> | |
public static bool Invert | |
{ | |
get | |
{ | |
if (!_invert.HasValue) | |
{ | |
_invert = DefaultInvert; | |
} | |
return _invert.Value; | |
} | |
set { _invert = value; } | |
} | |
private static bool? _vsync; | |
/// <summary> | |
/// The Vertical Sync | |
/// </summary> | |
/// <para> | |
/// This causes the Frames Per Second to match the refresh rate of the monitor. | |
/// </para> | |
public static bool VSync | |
{ | |
get | |
{ | |
if (!_vsync.HasValue) | |
{ | |
_vsync = DefaultVSync; | |
} | |
return _vsync.Value; | |
} | |
set | |
{ | |
_vsync = value; | |
switch (VSync) | |
{ | |
case false: | |
QualitySettings.vSyncCount = 0; | |
break; | |
default: | |
QualitySettings.vSyncCount = 1; | |
break; | |
} | |
} | |
} | |
#endregion | |
#region Normal Variables | |
private static bool _hasLoaded; | |
#endregion | |
/// <summary> | |
/// This will Load the Options regardless of whether it's been called previously. | |
/// </summary> | |
public static void Load() | |
{ | |
_hasLoaded = true; | |
PlayerName = PlayerPrefs.GetString("Settings/PlayerName", DefaultPlayerName); | |
Interpolation = PlayerPrefs.GetFloat("Settings/Interpolation", DefaultInterpolation); | |
Volume = PlayerPrefs.GetFloat("Settings/Volume", DefaultVolume); | |
MouseSensitivity = PlayerPrefs.GetFloat("Settings/MouseSensitivity", DefaultMouseSensitivity); | |
FieldOfView = PlayerPrefs.GetInt("Settings/FieldOfView", DefaultFieldOfView); | |
VSync = PlayerPrefsUlt.GetBool("Settings/VSync", DefaultVSync); | |
Invert = PlayerPrefsUlt.GetBool("Settings/Invert", DefaultInvert); | |
} | |
/// <summary> | |
/// If Load has Previous been called then it won't load again. | |
/// </summary> | |
public static void SafeLoad() | |
{ | |
if (!_hasLoaded) | |
{ | |
Load(); | |
_hasLoaded = true; | |
} | |
} | |
public static void Save() | |
{ | |
PlayerPrefs.SetString("Settings/PlayerName", PlayerName); | |
PlayerPrefs.SetFloat("Settings/Interpolation", Interpolation); | |
PlayerPrefs.SetFloat("Settings/Volume", Volume); | |
PlayerPrefs.SetFloat("Settings/MouseSensitivity", MouseSensitivity); | |
PlayerPrefs.SetInt("Settings/FieldOfView", FieldOfView); | |
PlayerPrefsUlt.SetBool("Settings/Invert", Invert); | |
PlayerPrefsUlt.SetBool("Settings/VSync", VSync); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment