Jump to content
  • Advertisement
SanguineTunic

Unity not saving to Json file anymore?

This topic is 434 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

Generally, it's best to only post the code that you think is relevant. In this case, that's probably only these lines:

        string jsonData = JsonUtility.ToJson(gameSettings, true);
        File.WriteAllText(Application.persistentDataPath + "/gamesettings.json", jsonData);

First, you should use the Visual Studio debugger, or maybe a Debug.Log call, to see if anything is going into that jsonData string. If not, it implies that your GameSettings class isn't serialisable. The docs forToJson say that it "it must be a MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute applied. " I don't see that as being true in your case. You probably need to add that Serializable attribute.

 

Share this post


Link to post
Share on other sites

Hmm, I tested the save section with a debug.Log and it clearly is called when clicking on the apply button.

I don't get what is wrong. It worked like a charm at first! So, thanks for your fast reply. I hope there is a solution to my problem anywhere, as what you mentioned about the missing Serializable attribute didn't help.

 

Share this post


Link to post
Share on other sites

The point of my logging suggest line isn't to see whether it's being called - the point is to log the content of jsonData and see what is being stored in there.

And if you've added the Serializable property, show us what you have now, so that someone might be able to test it themselves.

Share this post


Link to post
Share on other sites

Oh, Sorry. Misunderstood that. This is what comes out when I log the content:

<string jsonData = JsonUtility.ToJson(gameSettings, true)>
UnityEngine.Debug:Log(Object)
SettingManager:SaveSettings() (at Assets/Menu/Main Scripts/SettingManager.cs:130)
SettingManager:OnCloseButtonClick() (at Assets/Menu/Main Scripts/SettingManager.cs:126)
SettingManager:<OnEnable>m__C() (at Assets/Menu/Main Scripts/SettingManager.cs:43)
UnityEngine.EventSystems.EventSystem:Update()

 

Share this post


Link to post
Share on other sites

I now found one possible cause, after starting the game in built mode with development mode active.

When saving it says:

Quote

IsolatedStorageException: Could not find part of the path "C:\users\Sanguine\AppData\LocalLow\SanguineTunic\ProjectName\gamesettings.json"

I don't get why... The path is exactly the same as it should be.

Maybe one of you can find the mistake I made.

Share this post


Link to post
Share on other sites

I don't know what you've done with that log line, but you should be able to print the actual content of the string (whereas what you've got there is the line of code, strangely) and see if there's actually any JSON in it. Better still, put a breakpoint in the debugger on that line, and use the debugger to see what is in there.

No idea about the IsolatedStorageException - that might be something worth asking on the Unity forums about.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!