I am making a game where i save the player’s settings in a json file. But i cant figure out how to access the variables in the classes i save. The variables cant be static because then i cant save them.
The save script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public static class SaveManager
{
public static void save(Total total)
{
string dir = Application.persistentDataPath + "/SaveData/";
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string json = JsonUtility.ToJson(total, true);
File.WriteAllText(dir + "Saves.json", json);
}
public static Total load()
{
string fullPath = Application.persistentDataPath + "/SaveData/" + "Saves.json";
Total total = new Total();
if(File.Exists(fullPath))
{
string json = File.ReadAllText(fullPath);
total = JsonUtility.FromJson<Total>(json);
}else
{
Debug.Log(fullPath + "Doesnt exist");
}
return total;
}
}
The script with save and load functions and the classes to save:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveScript : MonoBehaviour
{
public Total total;
public void save()
{
SaveManager.save(total);
Debug.Log("saved");
}
public void load()
{
total = SaveManager.load();
Debug.Log("loaded");
}
}
[System.Serializable]
public class Settings
{
public int quality = 2;
public int fullscreenMode = 0;
public float masterVolume = 1f;
public float musicVolume = 0.5f;
}
[System.Serializable]
public class SystemVars
{
public int yes;
}
[System.Serializable]
public class Info
{
public string yes = "nothing yet";
}
[System.Serializable]
public class Total
{
public Settings Settings;
public SystemVars System;
public Info Info;
}
And the settings script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
public class SetSettings : MonoBehaviour
{
public AudioMixer audioMixer;
public SaveScript saveScript;
void Start()
{
saveScript = GameObject.Find("Save").GetComponent<SaveScript>();
saveScript.load();
setAll();
}
public void setQuality(int qualityIndex)
{
QualitySettings.SetQualityLevel(qualityIndex, true);
}
public void setFullscreenMode(int fullscreenMode)
{
var nothing = fullscreenMode switch
{
0 => Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen,
1 => Screen.fullScreenMode = FullScreenMode.FullScreenWindow,
2 => Screen.fullScreenMode = FullScreenMode.MaximizedWindow,
3 => Screen.fullScreenMode = FullScreenMode.Windowed,
_ => Screen.fullScreenMode = FullScreenMode.FullScreenWindow
};
}
public void setMasterVolume(float masterVolume)
{
GameObject.Find("Canvas").GetComponent<SetText>().setMasterVolumeText();
audioMixer.SetFloat("masterVolume", Mathf.Log10 (masterVolume) * 20);
}
public void setMusicVolume(float musicVolume)
{
GameObject.Find("Canvas").GetComponent<SetText>().setMusicVolumeText();
audioMixer.SetFloat("musicVolume", Mathf.Log10 (musicVolume) * 20);
}
public void restoreDefaults()
{
setQuality(2);
setFullscreenMode(0);
setMasterVolume(1f);
setMusicVolume(0.5f);
}
public void setAll()
{ //Take the values from settings class and assign them in functions
setQuality(s.quality);
// Debug.Log(s.quality);
setFullscreenMode(s.fullscreenMode);
// Debug.Log(s.fullscreenMode);
setMasterVolume(s.masterVolume);
// Debug.Log(s.masterVolume);
setMusicVolume(s.musicVolume);
// Debug.Log(s.musicVolume);
// GameObject.Find("Canvas").GetComponent<SetText>().quality.value = s.quality;
// GameObject.Find("Canvas").GetComponent<SetText>().fullscreenMode.value = s.fullscreenMode;
// GameObject.Find("Canvas").GetComponent<SetText>().masterVolumeSlider.value = s.masterVolume;
// GameObject.Find("Canvas").GetComponent<SetText>().musicVolumeSlider.value = s.musicVolume;
}
}
Can someone please tell me how i can change and read the files in the Savescript class from the SetSettings class?