- Home /
Save a Toggle Group state?
Unity 5, using the New UI
My android app is almost done, but I am seeking answer to a question that would make it much better. My programming/scripting skills are close to non existent, so bear with me. I have a dual controller setup (buttons and joysticks) which I can select via my settingspanel and a togglegroup
ToggleGroupControls
[x] ToggleJoysticks
[ ] ToggleButtons
The toggles works by simply deactivating one controller and activating the other controller via GameObject.SetActive. This works perfectly, but the toggle selection resets itself when I restarts the app. So my question for you is; do any of you have a script that will save a toggle group state? I guess it will have to be done via PlayerPrefs? I have spent a couple of days and hours trying to search for an answer or a script that I can reverse engineer, but no luck so far, so I am close to giving up
Thanks
Thank you for the reply, but as said I have spent days googling and searching for something to build from, and I have been reading everything that I have come by so far. All the examples I have found have been for single toggle and usually not using the new UI. I find it incredibly hard to write the code from scratch, which most of us probably do in the beginning, since I am a non programmer. However I do have written a few scripts already but they all have been based on examples I have found, and I do learn a lot by doing so. babysteps. I come from a 3D designer background. I know it might be frustrating to get such noob questions, and believe me when I say that I hate asking for help, but I was only asking if someone had something similar already that they would love to share. This is not a crucial part of my app, since it is almost done, but it would make the user experience a bit better. Of course I will continue my search and read tutorials and hopefully this question will be approved by the moderator.
Thanks
I have finally solved it. This is the script I ended up with:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SaveControllerSelection : $$anonymous$$onoBehaviour {
public GameObject buttons;
void Start()
{
int state = PlayerPrefs.GetInt("Controller", 0);
if (state == 1)
{
buttons.SetActive(true);
transform.GetChild(1).GetComponent<Toggle>().isOn = true;
}
}
public void SaveJoysticks()
{
PlayerPrefs.SetInt("Controller", 0);
}
public void SaveButtons()
{
PlayerPrefs.SetInt("Controller", 1);
}
}