How to save the state of Canvases/Toggles?
First of all, I don't really have any experience in coding and just started a while ago with Unity. Anyway, I almost got my first little application for an android device done, with a lot of help from various tutorials. There is still one major part missing though and that is saving the settings. I've tried almost every possible solution/asset I could find on the internet/asset store, but I didn't manage to get them to work or didn't figure out how they work yet.
This is what I want to do here: Firstly, I want to choose between different themes by clicking a toggle. Each toggle opens a canvas with a theme and closes every other theme canvas. With my current "saving" code, the toggle doesn't even activate the canvas I want to setActive (Without it does). Secondly, I want to save a GameObject as a favorite with a toggle and this toggle should still be toggled in the next session. Because the toggle always switched back to untoggled as soon as I clicked somewhere else, I made 2 overlapping toggles and each of them sets itself inactive and the other one active (I mention this if that is a reason why it doesn't work and I know it is probably not the professional way to do it).
I'm sorry for this beginner question, but I really couldn't figure it out on my own until now. I would really appreciate any kind of suggestions and thanks a lot in advance!
I've read PlayerPrefs is not the most secure way to save game data, but in this case, it doesn't matter. This is the currently not working saving code for the themes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveTheme : MonoBehaviour
{
public Canvas canvas;
void Start()
{
if ((PlayerPrefs.GetInt("ThemeKey") == 1))
{
canvas.enabled = true;
}
else
{
canvas.enabled = false;
}
}
void Update()
{
if (canvas.enabled == true)
{
PlayerPrefs.SetInt("ThemeKey", 1);
}
else
{
PlayerPrefs.SetInt("ThemeKey", 0);
}
}
}
and this one for the favorite toggles:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveToggle : MonoBehaviour
{
public Toggle toggle;
void Start()
{
if ((PlayerPrefs.GetInt("ToggleKey") == 1))
{
toggle.isOn = true;
}
else
{
toggle.isOn = false;
}
}
public void ToggleState()
{
if (toggle.isOn == true)
{
PlayerPrefs.SetInt("ToggleKey", 1);
}
else
{
PlayerPrefs.SetInt("ToggleKey", 0);
}
}
}
Answer by Beebaa · Feb 09, 2021 at 07:48 AM
This is how I solved it now for the themes:
private int value;
public Canvas canvas;
public Canvas canvas1;
void Awake()
{
if (PlayerPrefs.HasKey("theme"))
{
value = PlayerPrefs.GetInt("theme");
if(value == 0)
{
canvas.enabled = true;
canvas1.enabled = false;
}
if (value == 1)
{
canvas.enabled = false;
canvas1.enabled = true;
}
else
{
canvas.enabled = true;
}
}
public void OnClickSave()
{
if(canvas.enabled == true)
{
PlayerPrefs.SetInt("theme", 0);
}
else if (canvas1.enabled == true)
{
PlayerPrefs.SetInt("theme", 1);
}
PlayerPrefs.Save();
}
And for the toggles:
private int value;
public Toggle toggle;
public Toggle toggle1;
void Awake()
{
if (PlayerPrefs.HasKey("toggle"))
{
value = PlayerPrefs.GetInt("toggle");
if(value == 1)
{
toggle.isOn = true;
}
}
if (PlayerPrefs.HasKey("toggle1"))
{
value = PlayerPrefs.GetInt("toggle1");
if (value == 1)
{
toggle1.isOn = true;
}
}
}
public void OnFavoriteSave()
{
if (toggle.isOn == true)
{
PlayerPrefs.SetInt("toggle", 1);
}
if (toggle1.isOn == true)
{
PlayerPrefs.SetInt("toggle1", 1);
}
PlayerPrefs.Save();
}
public void OnUnFavoriteSave()
{
if (toggle.isOn == false)
{
PlayerPrefs.SetInt("toggle", 0);
}
if (toggle1.isOn == false)
{
PlayerPrefs.SetInt("toggle1", 0);
}
PlayerPrefs.Save();
}