Data persistence Flashcards
1
Q
public static MainManager Instance;
public Color TeamColor;
A
private void Awake() { if (Instance !=null) { Destroy(gameObject); } Instance=this; DontDestroyOnload(gameObject); }
2
Q
Data persistent between session
A
(MainManager script) [System.Serializable] class SaveData { public Color TeamColor; } public void SaveColor() { SaveData data = new SaveData(); data.TeamColor= TeamColor; string json=JsonUtility.ToJson(data); File.WriteAllText(Application.persistenteDataPath+"/savefile.json",json); } public void LoadColor() { string path = Application.persistentDataPath+"/savefile.json"; SaveData data = JsonUtility.FromJson < SaveData >(json); TeamColor = data.TeamColor; } (Awake method) LoadColor();
3
Q
Data persistent between sessions
MenuUiHandler script
A
(Start method)
ColorPicker.SelectColor(MainManager.Instance.TeamColor);
(Exit method)
MainManager.Instance.SaveColor();
public void SaveColorClicked()
{
MainManager.Instance.SaveColor();
}
public void LoadColorClicked() { MainManager.Instance.LoadColor(); Color picked.SelectColor(MainManager.Instance.TeamColor); }
4
Q
Data persistent between scenes
A
(MenuUiHandler script) public void NewColorSelected(Color color) { MainManager.Instance.TeamColor= color; } (Unit script) private void Start() { if (MainManager.Instance!=null) { SetColor(Main manager.Instance.TeamColor); } }