Assets/Scripts/Utility/SettingsManager.cs (view raw)
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Runtime.Serialization.Formatters.Binary;
6using UnityEngine;
7using UnityEngine.Audio;
8using UnityEngine.SceneManagement;
9using UnityEngine.UI;
10
11public class SettingsManager : MonoBehaviour
12{
13 GameMaster gm;
14 public OverworldUIManager ui;
15 public Slider musicSlider, sfxSlider;
16 public Slider textSlider, battleSlider;
17 public AudioMixer mixer;
18
19 public void SetMusicVolume(float sliderValue)
20 {
21 mixer.SetFloat("MusicVolume", Mathf.Log10(sliderValue) * 20);
22 PlayerPrefs.SetFloat("MusicVolume", sliderValue);
23 }
24
25 public void SetSfxVolume(float sliderValue)
26 {
27 mixer.SetFloat("SfxVolume", Mathf.Log10(sliderValue) * 20);
28 PlayerPrefs.SetFloat("SfxVolume", sliderValue);
29 }
30
31 public void SetTextSpeed(float sliderValue)
32 {
33 UI.TextSpeed = sliderValue;
34 PlayerPrefs.SetFloat("TextSpeed", sliderValue);
35 }
36
37 public void SetBattleSpeed(float sliderValue)
38 {
39 UI.BattleSpeed = sliderValue;
40 PlayerPrefs.SetFloat("BattleSpeed", sliderValue);
41 }
42
43 public void Start()
44 {
45 gm = GameMaster.Instance;
46
47 //restore playerprefs
48 musicSlider.value = PlayerPrefs.GetFloat("MusicVolume", 0.75f);
49 sfxSlider.value = PlayerPrefs.GetFloat("SfxVolume", 0.75f);
50 textSlider.value = PlayerPrefs.GetFloat("TextSpeed", 0.015f);
51 battleSlider.value = PlayerPrefs.GetFloat("BattleSpeed", 1f);
52 }
53
54 #region Resolutions
55 public void GoHD()
56 {
57 Screen.SetResolution(1280, 720, Screen.fullScreen);
58 }
59 public void GoFullHD()
60 {
61 Screen.SetResolution(1920, 1080, Screen.fullScreen);
62 }
63 public void Go2K()
64 {
65 Screen.SetResolution(2560, 1440, Screen.fullScreen);
66 }
67 #endregion
68
69 #region Save and load
70 public void SaveGame(int slot)
71 {
72 gm = GameMaster.Instance;
73 Save save = gm.CreateSaveGameObject();
74
75 BinaryFormatter bf = new BinaryFormatter();
76 FileStream file = File.Create(Application.persistentDataPath + "/game" + slot + ".save");
77 bf.Serialize(file, save);
78 file.Close();
79
80 //Debug.Log("Game saved in " + Application.persistentDataPath + "/game" + slot + ".save");
81
82 ui.hideAllPanels();
83 ui.currentPanel = 0;
84 }
85 public void LoadGame(int slot)
86 {
87 if (File.Exists(Application.persistentDataPath + "/game" + slot + ".save"))
88 {
89 gm = GameMaster.Instance;
90 BinaryFormatter bf = new BinaryFormatter();
91 FileStream file = File.Open(Application.persistentDataPath + "/game" + slot + ".save", FileMode.Open);
92 Save save = (Save)bf.Deserialize(file);
93 file.Close();
94
95 Vector3 lastPosition = new Vector3(save.posx, save.posy, save.posz);
96 Quaternion lastRotation = new Quaternion(save.rot0, save.rot1, save.rot2, save.rot3);
97 gm.LoadManager(save.currentLevel, new List<string>(save.enemiesKilled), save.gold,
98 new Dictionary<string, Stats>(save.party), new Inventory(save.inventory),
99 save.fighting, lastPosition, lastRotation, save.timerfloat);
100
101 Time.timeScale = 1f;
102 SceneManager.LoadScene("Level" + save.currentLevel);
103 } else
104 {
105 Debug.Log("No file found.");
106 }
107 }
108 #endregion
109}