all repos — RPG @ d7044262357209ad359fad4b2664315aea7096d0

Fully functional 3D turn based role playing game coded in C# and base Unity.

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    private new AudioSource audio;
 19    [SerializeField] AudioClip sfxTest = null;
 20
 21    public void SetMusicVolume(float sliderValue)
 22    {
 23        mixer.SetFloat("MusicVolume", Mathf.Log10(sliderValue) * 20);
 24        PlayerPrefs.SetFloat("MusicVolume", sliderValue);
 25    }
 26
 27    public void SetSfxVolume(float sliderValue)
 28    {
 29        if(sfxTest && !audio.isPlaying)
 30            audio.PlayOneShot(sfxTest);
 31        mixer.SetFloat("SfxVolume", Mathf.Log10(sliderValue) * 20);
 32        PlayerPrefs.SetFloat("SfxVolume", sliderValue);
 33    }
 34
 35    public void SetTextSpeed(float sliderValue)
 36    {
 37        UI.TextSpeed = sliderValue;
 38        PlayerPrefs.SetFloat("TextSpeed", sliderValue);
 39    }
 40
 41    public void SetBattleSpeed(float sliderValue)
 42    {
 43        UI.BattleSpeed = sliderValue;
 44        PlayerPrefs.SetFloat("BattleSpeed", sliderValue);
 45    }
 46
 47    public void Start()
 48    {
 49        gm = GameMaster.Instance;
 50        audio = GetComponent<AudioSource>();
 51        //restore playerprefs
 52        musicSlider.value = PlayerPrefs.GetFloat("MusicVolume", 0.75f);
 53        sfxSlider.value = PlayerPrefs.GetFloat("SfxVolume", 0.75f);
 54        textSlider.value = PlayerPrefs.GetFloat("TextSpeed", 0.015f);
 55        battleSlider.value = PlayerPrefs.GetFloat("BattleSpeed", 1f);
 56    }
 57
 58    #region Resolutions
 59    public void GoHD()
 60    {
 61        Screen.SetResolution(1280, 720, Screen.fullScreen);
 62    }
 63    public void GoFullHD()
 64    {
 65        Screen.SetResolution(1920, 1080, Screen.fullScreen);
 66    }
 67    public void Go2K()
 68    {
 69        Screen.SetResolution(2560, 1440, Screen.fullScreen);
 70    }
 71    #endregion
 72
 73    #region Save and load
 74    public void SaveGame(int slot)
 75    {
 76        gm = GameMaster.Instance;
 77        Save save = gm.CreateSaveGameObject();
 78
 79        BinaryFormatter bf = new BinaryFormatter();
 80        FileStream file = File.Create(Application.persistentDataPath + "/game" + slot + ".save");
 81        bf.Serialize(file, save);
 82        file.Close();
 83
 84        //Debug.Log("Game saved in " + Application.persistentDataPath + "/game" + slot + ".save");
 85
 86        ui.hideAllPanels();
 87        ui.currentPanel = 0;
 88    }
 89    public void LoadGame(int slot)
 90    {
 91        if (File.Exists(Application.persistentDataPath + "/game" + slot + ".save"))
 92        {
 93            gm = GameMaster.Instance;
 94            BinaryFormatter bf = new BinaryFormatter();
 95            FileStream file = File.Open(Application.persistentDataPath + "/game" + slot + ".save", FileMode.Open);
 96            Save save = (Save)bf.Deserialize(file);
 97            file.Close();
 98
 99            Vector3 lastPosition = new Vector3(save.posx, save.posy, save.posz);
100            Quaternion lastRotation = new Quaternion(save.rot0, save.rot1, save.rot2, save.rot3);
101            gm.LoadManager(save.currentLevel, new List<string>(save.enemiesKilled), save.gold,
102                new Dictionary<string, Stats>(save.party), new Inventory(save.inventory),
103                save.fighting, lastPosition, lastRotation, save.timerfloat);
104
105            Time.timeScale = 1f;
106            SceneManager.LoadScene("Level" + save.currentLevel);
107        } else
108        {
109            Debug.Log("No file found.");
110        }
111    }
112    #endregion
113}