PatchQuest

Settings Save Data

Nov 12th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5.  
  6. public class SettingsSaveData {
  7.  
  8.     public static SettingsSaveData CurrentAppliedSettings {
  9.         get {
  10.             return new SettingsSaveData (AudioListener.volume, SoundManager.MusicVolume, SoundManager.IsMuted, QualitySettings.vSyncCount > 0,
  11.                 Screen.currentResolution.width, Screen.currentResolution.height, Screen.fullScreen);
  12.         }
  13.     }
  14.  
  15.     public static SettingsSaveData DefaultSettings {
  16.         get {
  17.             var resolutions = Screen.resolutions;
  18.             var topResolution = resolutions[resolutions.Length -1];
  19.  
  20.             return new SettingsSaveData (1, 1, false, true, topResolution.width, topResolution.height, true);
  21.         }
  22.     }
  23.  
  24.     // ============================= CONSTRUCTION =============================
  25.  
  26.     private readonly float masterVolume;
  27.     private readonly float musicVolume;
  28.     private readonly bool muted;
  29.  
  30.     private readonly bool vSync;
  31.     private readonly int resolutionWidth;
  32.     private readonly int resolutionHeight;
  33.     private readonly bool fullScreen;
  34.  
  35.     public SettingsSaveData (float masterVolume, float musicVolume, bool muted, bool vSync, int resolutionWidth, int resolutionHeight, bool fullScreen) {
  36.         this.masterVolume = Mathf.Clamp (masterVolume, 0, 1);
  37.         this.musicVolume = Mathf.Clamp (musicVolume, 0, 1);
  38.         this.muted = muted;
  39.         this.vSync = vSync;
  40.         this.resolutionWidth = resolutionWidth;
  41.         this.resolutionHeight = resolutionHeight;
  42.         this.fullScreen = fullScreen;
  43.     }
  44.  
  45.     // ============================= READ / WRITE =============================
  46.  
  47.     public static SettingsSaveData ReadFrom (BinaryReader reader) {
  48.         var masterVolume = reader.ReadSingle ();
  49.         var musicVolume = reader.ReadSingle ();
  50.         var muted = reader.ReadBoolean ();
  51.         var vSync = reader.ReadBoolean ();
  52.         var resolutionWidth = reader.ReadInt32 ();
  53.         var resolutionHeight = reader.ReadInt32 ();
  54.         var fullScreen = reader.ReadBoolean ();
  55.  
  56.         return new SettingsSaveData (masterVolume, musicVolume, muted, vSync, resolutionWidth, resolutionHeight, fullScreen);
  57.     }
  58.  
  59.     public void WriteTo (BinaryWriter writer) {
  60.         writer.Write (masterVolume);
  61.         writer.Write (musicVolume);
  62.         writer.Write (muted);
  63.  
  64.         writer.Write (vSync);
  65.         writer.Write (resolutionWidth);
  66.         writer.Write (resolutionHeight);
  67.         writer.Write (fullScreen);
  68.     }
  69.  
  70.     // ============================= APPLICATION =============================
  71.  
  72.     public void Apply () {
  73.         AudioListener.volume = masterVolume;
  74.         SoundManager.SetMusicVolume (musicVolume);
  75.         SoundManager.SetMuted (muted);      
  76.         QualitySettings.vSyncCount = vSync ? 1 : 0;
  77.  
  78.         Screen.SetResolution (resolutionWidth, resolutionHeight, fullScreen);
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment