C#Unity脚本不起作用。无法创建Player Prefs密钥,使用代码无法使用Unity Game-in设置

时间:2018-08-06 18:29:31

标签: c# unity3d settings

我有一个Unity C#脚本,用于游戏中的设置菜单。有几个参数需要更改:音量(音乐和声音不同),屏幕分辨率,全屏和语言。

语言效果很好(我还有另一个用于语言和本地化的脚本,在这个脚本中,我只更改PlayerPrefs Key)。音量也可以正常使用,但是重启后不会保存设置。和全屏完全不起作用。不用担心空方法,我以后会写它们。

我的问题:全屏和音量无法正常工作;我不知道如何创建分辨率设置。有人可以帮我吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using System.IO;

public class Settings : MonoBehaviour {
    string[] PossibleLanguages = { "English", "Русский" };
    public AudioMixer SoundMix;
    public AudioMixer MusicMix;
    Resolution[] rsl;
    List<string> resolutions;

    string SoundKey = "Sound_Level";
    string MusicKey = "Music_Level";

    public AudioClip buttonSound;
    public AudioSource ClickSound;
    //public float sound_level = 1f;
    //public float music_level = 1f;
    //public string rezolution = "1920x1080";
    //public bool fullscreen = true;
    //public string language = "English";
    //
    //All keys to initiate
    //
    //Language_Settings
    //Resolution_Settings
    //Sound_Settings
    //Music_Settings
    //Fullscreen_Settings

    public Slider sliderMusic;
    public Slider sliderSound;

    private void Awake()
    {
        resolutions = new List<string>();
        rsl = Screen.resolutions;
        foreach (var i in rsl)
        {
            resolutions.Add(i.width + "x" + i.height);
        }

        for (int i = 0; i < resolutions.Count; i++)
        {
            Debug.Log(resolutions[i]);
        }

        if (!PlayerPrefs.HasKey("Music_Level"))
        {
            PlayerPrefs.SetFloat("Music_Level", 0.0f);
        }
        SoundPowerStart(MusicKey, sliderMusic, MusicMix);


        if (!PlayerPrefs.HasKey("Sound_Level"))
        {
            PlayerPrefs.SetFloat("Sound_Level", 0.0f);
        }

        SoundPowerStart(SoundKey, sliderSound, SoundMix);
    }

    public void SoundPowerStart(string name, Slider slider, AudioMixer mixer)
    {
        slider.value = PlayerPrefs.GetFloat(name);
        mixer.SetFloat(name, PlayerPrefs.GetFloat(name));
    }

    public void PlayClickSettingsStngs()
    {
        ClickSound.PlayOneShot(buttonSound);
    }

    public void MusicLevel(Slider Slider)
    {
        MusicMix.SetFloat("Music_Level", Slider.value);
        PlayerPrefs.SetFloat("Music_Level", Slider.value);
    }

    public void SoundLevel(Slider Slider)
    {
        SoundMix.SetFloat("Sound_Level", Slider.value);
        PlayerPrefs.SetFloat("Sound_Level", Slider.value);
    }

    public void FullScreenTogle()
    {
        Screen.fullScreen = !Screen.fullScreen;
    }

    public void LanguageChangeLeft(Text CurrentLanguage)
    {
        CurrentLanguage.text = GetNextEllement(1, PossibleLanguages);
        if (CurrentLanguage.text == "English")
            PlayerPrefs.SetString("Language_Settings", "en_UK");
        else if (CurrentLanguage.text == "Русский")
            PlayerPrefs.SetString("Language_Settings", "ru_RU");
        PlayClickSettingsStngs();
    }
    public void LanguageChangeRight(Text CurrentLanguage)
    {
        CurrentLanguage.text = GetNextEllement(0, PossibleLanguages);
        if (CurrentLanguage.text == "English")
            PlayerPrefs.SetString("Language_Settings", "en_UK");
        else if (CurrentLanguage.text == "Русский")
            PlayerPrefs.SetString("Language_Settings", "ru_RU");
        PlayClickSettingsStngs();
    }

    public string GetNextEllement(int index, string[] Array)
    {

        if ((index > Array.Length - 1) || (index < 0))
            Debug.Log("Error with languge chenging");
        else if (index == Array.Length - 1)
            index = 0;
        else
            index++;
        return Array[index];
    }

    public void ResolutionChange()
    {

    }

}

0 个答案:

没有答案