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
{
///
/// 分辨率width
///
public static int ResolutionX;
///
/// 分辨率height
///
public static int ResolutionY;
///
/// 是否全屏
///
public static bool IsFullScreen;
///
/// 背景音乐
///
public static float BgmValue;
///
/// 特效音乐
///
public static float SEValue;
///
/// 是否静音
///
public static bool IsMute;
///
/// 画质设置
///
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);
}
///
/// 设置显示和画质
///
///
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);
}
}
///
/// 设置声音,每个场景都要调一次
///
public static void SoundSetFunction()
{
ThisVoiceType[] voiceall = FindObjectsOfType();
if (voiceall != null && voiceall.Length > 0)
{
for (int i = 0; i < voiceall.Length; i++)
{
if (voiceall[i].Voice_Type == VoiceType.背景音)
{
voiceall[i].GetComponent().volume = BgmValue;
voiceall[i].GetComponent().loop = true;
}
else if (voiceall[i].Voice_Type == VoiceType.特效音)
{
voiceall[i].GetComponent().volume = SEValue;
voiceall[i].GetComponent().playOnAwake = false;
}
else
{
}
if (IsMute)
{
voiceall[i].GetComponent().enabled = false;
}
else
{
voiceall[i].GetComponent().enabled = true;
}
}
}
}
public static void DefultSoundSetFunction()
{
ThisVoiceType[] voiceall = FindObjectsOfType();
if (voiceall != null && voiceall.Length > 0)
{
for (int i = 0; i < voiceall.Length; i++)
{
if (voiceall[i].Voice_Type == VoiceType.背景音)
{
voiceall[i].GetComponent().volume = DefultBgmValue;
}
else if (voiceall[i].Voice_Type == VoiceType.特效音)
{
voiceall[i].GetComponent().volume = DefultSEValue;
}
else
{
}
if (IsMute)
{
voiceall[i].GetComponent().enabled = DefultIsMute;
}
}
}
}
///
/// 读取json文件,没有返回默认;
///
///
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(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;
}
}