Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Unity not saving to Json file anymore?

$
0
0
Hey Community, I have a question about C# and Unity. For my options menu I wrote a script with C#, that worked fine for a while. My problem is to let unity write a .Json file into the LocalLow folder. At first everything went fine and there occurred no errors, but now the file is only generated completely empty. I didn't change anything in the script and Visual Studio doesn't show any errors to me, so I have no clue what is wrong. As I'm only a beginner, I hope anyone of you guys can find what I messed up and give me some advices. Code for the GameSettings using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameSettings { //all variables assigned to their class. public bool fullscreen; public float musicvolume; public float audiovolume; public int difficulty; public bool subtitles; public int Language; public int vsync; public int texturequality; public int resolution; public float gamma; public int antialiasing; public bool autosave; } Code for the SettingManager using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; public class SettingManager : MonoBehaviour { //all variables assigned to their class. public Slider musicvolumeslider; public Toggle fullscreenToggle; public Slider audiovolumeSlider; public Dropdown difficultyDropdown; public Toggle subtitlesToggle; public Dropdown languageDropdown; public Dropdown vsyncDropdown; public Dropdown texturequalityDropdown; public Slider gammaSlider; public Dropdown antialiasingDropdown; public Toggle autosaveToggle; public Dropdown resolutionDropdown; public Button closeButton; public AudioSource musicSource; public Resolution[] resolutions; public GameSettings gameSettings; void OnEnable() { DontDestroyOnLoad(gameObject); musicvolumeslider.onValueChanged.AddListener(delegate { OnMusicVolumeChange(); }); fullscreenToggle.onValueChanged.AddListener(delegate { OnFullscreenToggle(); }); audiovolumeSlider.onValueChanged.AddListener(delegate { OnAudioVolumeChange(); }); difficultyDropdown.onValueChanged.AddListener(delegate { OnDifficultyChange(); }); subtitlesToggle.onValueChanged.AddListener(delegate { OnSubtitlesChange(); }); languageDropdown.onValueChanged.AddListener(delegate { OnLanguageChange(); }); vsyncDropdown.onValueChanged.AddListener(delegate { OnVSyncChange(); }); texturequalityDropdown.onValueChanged.AddListener(delegate { OnTextureQualityChange(); }); gammaSlider.onValueChanged.AddListener(delegate { OnGammaChange(); }); autosaveToggle.onValueChanged.AddListener(delegate { OnAutosaveToggle(); }); antialiasingDropdown.onValueChanged.AddListener(delegate { OnAAChange(); }); resolutionDropdown.onValueChanged.AddListener(delegate { OnResolutionChange(); }); closeButton.onClick.AddListener(delegate { OnCloseButtonClick(); }); musicSource = GameObject.FindGameObjectWithTag("music").GetComponent<AudioSource>(); gameSettings = new GameSettings(); resolutions = Screen.resolutions; foreach (Resolution resolution in resolutions) { resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString())); } LoadSettings(); } public void OnMusicVolumeChange() { musicSource.volume = gameSettings.musicvolume = musicvolumeslider.value; } public void OnResolutionChange() { Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen); gameSettings.resolution = resolutionDropdown.value; } public void OnFullscreenToggle() { gameSettings.fullscreen = Screen.fullScreen = fullscreenToggle.isOn; } public void OnAudioVolumeChange() { AudioListener.volume = gameSettings.audiovolume = audiovolumeSlider.value; } public void OnDifficultyChange() { //Not Implemented Yet } public void OnSubtitlesChange() { //Not Implemented Yet } public void OnLanguageChange() { if (languageDropdown.value == 0) LocalizationService.Instance.Localization = "English"; if (languageDropdown.value == 1) LocalizationService.Instance.Localization = "German"; if (languageDropdown.value == 2) LocalizationService.Instance.Localization = "French"; if (languageDropdown.value == 3) LocalizationService.Instance.Localization = "Russian"; if (languageDropdown.value == 4) LocalizationService.Instance.Localization = "Italian"; if (languageDropdown.value == 5) LocalizationService.Instance.Localization = "Chinese"; } public void OnVSyncChange() { QualitySettings.vSyncCount = gameSettings.vsync = vsyncDropdown.value; } public void OnTextureQualityChange() { QualitySettings.masterTextureLimit = gameSettings.texturequality = texturequalityDropdown.value; } public void OnGammaChange() { //Not Implemented Yet } public void OnAAChange() { QualitySettings.antiAliasing = (int)Mathf.Pow(2, antialiasingDropdown.value); gameSettings.antialiasing = antialiasingDropdown.value; } public void OnAutosaveToggle() { //Not Implemented Yet } public void OnCloseButtonClick() { SaveSettings(); } public void SaveSettings() { string jsonData = JsonUtility.ToJson(gameSettings, true); File.WriteAllText(Application.persistentDataPath + "/gamesettings.json", jsonData); } public void LoadSettings() { gameSettings = JsonUtility.FromJson<GameSettings>(File.ReadAllText(Application.persistentDataPath + "/gamesettings.json")); audiovolumeSlider.value = gameSettings.audiovolume; antialiasingDropdown.value = gameSettings.antialiasing; gammaSlider.value = gameSettings.gamma; texturequalityDropdown.value = gameSettings.texturequality; vsyncDropdown.value = gameSettings.vsync; languageDropdown.value = gameSettings.Language; subtitlesToggle.isOn = gameSettings.subtitles; Screen.fullScreen = gameSettings.fullscreen; difficultyDropdown.value = gameSettings.difficulty; resolutionDropdown.value = gameSettings.resolution; autosaveToggle.isOn = gameSettings.autosave; musicvolumeslider.value = gameSettings.musicvolume; resolutionDropdown.RefreshShownValue(); } } Hoping for fast help, SanguineTunic

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>