using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using System; using System.IO; using System.Text; public enum Frame { 低配置 = 0, 中配置 = 1, 高配置 = 2, 完美画质 = 3 } [Serializable] public class System_Setting { public int resolutionX; public int resolutionY; public bool isFullScreen; public float bgmValue; public float seValue; public bool isMuto; public Frame frameSelect; } public class SystemSettig : MonoBehaviour { /// <summary> /// 分辨率width /// </summary> public static int ResolutionX; /// <summary> /// 分辨率height /// </summary> public static int ResolutionY; /// <summary> /// 是否全屏 /// </summary> public static bool IsFullScreen; /// <summary> /// 背景音乐 /// </summary> public static float BgmValue; /// <summary> /// 特效音乐 /// </summary> public static float SEValue; /// <summary> /// 是否静音 /// </summary> public static bool IsMute; /// <summary> /// 画质设置 /// </summary> public static Frame FrameSelect; public const int DefultResolutionX = 1366; public const int DefultResolutionY = 768; public const bool DefulteIsFullScreen = false; public const float DefultBgmValue = 0.5f; public const float DefultSEValue = 0.5f; public const bool DefultIsMute = false; public const Frame DefultFrameSelect = Frame.完美画质; public static SystemSettig instance = null; public static string Hinttext=""; string path; void Awake() { System_Setting load = new System_Setting(); path = Application.streamingAssetsPath + "/System.json"; // Debug.Log(Application.streamingAssetsPath); //Debug.Log(path); load = LoadFromFile(); if (load==null) { load = DefuletSetting(); } //设置画质与分辨率 SystemSettig.ShowAndFrameSetfunction(load); //设置声音 SystemSettig.ResolutionX = load.resolutionX; ResolutionY = load.resolutionY; IsFullScreen = load.isFullScreen; IsMute = load.isMuto; BgmValue = load.bgmValue; SEValue = load.seValue; FrameSelect = load.frameSelect; SystemSettig.SoundSetFunction(); } public static void RessetToDefult() { ResolutionX = DefultResolutionX; ResolutionY = DefultResolutionY; IsFullScreen = DefulteIsFullScreen; BgmValue = DefultBgmValue; SEValue = DefultSEValue; IsMute = DefultIsMute; FrameSelect = DefultFrameSelect; Hinttext = ""; } // Use this for initialization void Start() { if (instance == null) { instance = this; } //DontDestroyOnLoad(gameObject); } /// <summary> /// 设置显示和画质 /// </summary> /// <param name="sys"></param> public static void ShowAndFrameSetfunction(System_Setting sys) { //显示设置 if (sys.isFullScreen) { Screen.SetResolution(sys.resolutionX, sys.resolutionY, true); } else { Screen.SetResolution(sys.resolutionX, sys.resolutionY, false); } //声音设置//切场景单独调用 //画面设置 if (sys.frameSelect == Frame.低配置) { QualitySettings.SetQualityLevel(1); } if (sys.frameSelect == Frame.中配置) { QualitySettings.SetQualityLevel(2); } if (sys.frameSelect == Frame.高配置) { QualitySettings.SetQualityLevel(4); } if (sys.frameSelect == Frame.完美画质) { QualitySettings.SetQualityLevel(5); } } /// <summary> /// 设置声音,每个场景都要调一次 /// </summary> public static void SoundSetFunction() { ThisVoiceType[] voiceall = FindObjectsOfType<ThisVoiceType>(); if (voiceall != null && voiceall.Length > 0) { for (int i = 0; i < voiceall.Length; i++) { if (voiceall[i].Voice_Type == VoiceType.背景音) { voiceall[i].GetComponent<AudioSource>().volume = BgmValue; voiceall[i].GetComponent<AudioSource>().loop = true; } else if (voiceall[i].Voice_Type == VoiceType.特效音) { voiceall[i].GetComponent<AudioSource>().volume = SEValue; voiceall[i].GetComponent<AudioSource>().playOnAwake = false; } else { } if (IsMute) { voiceall[i].GetComponent<AudioSource>().enabled = false; } else { voiceall[i].GetComponent<AudioSource>().enabled = true; } } } } public static void DefultSoundSetFunction() { ThisVoiceType[] voiceall = FindObjectsOfType<ThisVoiceType>(); if (voiceall != null && voiceall.Length > 0) { for (int i = 0; i < voiceall.Length; i++) { if (voiceall[i].Voice_Type == VoiceType.背景音) { voiceall[i].GetComponent<AudioSource>().volume = DefultBgmValue; } else if (voiceall[i].Voice_Type == VoiceType.特效音) { voiceall[i].GetComponent<AudioSource>().volume = DefultSEValue; } else { } if (IsMute) { voiceall[i].GetComponent<AudioSource>().enabled = DefultIsMute; } } } } /// <summary> /// 读取json文件,没有返回默认; /// </summary> /// <returns></returns> public System_Setting LoadFromFile() { if (!File.Exists(path)) { return null; } StreamReader sr = new StreamReader(path); if (sr == null) { Hinttext="文件流为空"; return null; } string json = sr.ReadToEnd(); if (json.Length > 0) { Hinttext = "配置成功"; return JsonUtility.FromJson<System_Setting>(json); } Hinttext = "配置失败"; return null; } public void SaveToJson(System_Setting sys) { string json = JsonUtility.ToJson(sys, true); File.WriteAllText(path, json, Encoding.UTF8); } public System_Setting DefuletSetting() { System_Setting sys = new System_Setting(); sys.resolutionX = DefultResolutionX; sys.resolutionY = DefultResolutionY; sys.isFullScreen = DefulteIsFullScreen; sys.bgmValue = DefultBgmValue; sys.seValue = DefultSEValue; sys.isMuto = DefultIsMute; sys.frameSelect = DefultFrameSelect; return sys; } }