Well I created a Panel with a toggle group and 3 toggles. Then created a Tester script which i referenced my panel:
Script testing your code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Tester : MonoBehaviour {
public GameObject myPanel;
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
IEnumerator<Toggle> toggleEnumCallsign = myPanel.GetComponent<ToggleGroup>().ActiveToggles().GetEnumerator();
toggleEnumCallsign.MoveNext();
Toggle callsignToggle = toggleEnumCallsign.Current;
Text callsignText = callsignToggle.GetComponentInChildren<Text>();
Debug.Log("Set to " + callsignText.text);
}
}
}
I was able to to get the correct Text of the toggle. Your code seems fine for getting the the correct text.
I looked into the ToggleGroup code here: https://bitbucket.org/Unity-Technologies/ui/src/a3f89d5f7d145e4b6fa11cf9f2de768fea2c500f/UnityEngine.UI/UI/Core/?at=2017.3
It looks like ToggleGroup doesn’t have an easy way to just get all the Toggles in a list. It stores them in a private List: m_toggles. I think the easiest way to save and store these toggle values is with PlayerPrefs. This is just a flat text file unity uses to load/store data. Its insecure as players can access it, but perfect for UI things.
We have two options:
- Copy ToggleGroup.cs from the above link and then modify it so the list is public
- Use Reflection to get at the list.
I’m going to use option 2. The solution will involve 3 major topics you might want to research to fully understand it. You can google all 3 of these to find out more information
- PlayerPrefs – easy topic
- C# Class Extensions – easy topic
- C# Reflection – medium/hard topic
First you need to make an extension script that uses reflection to get at the Toggle List:
(This code was originally written by lordofduct)
ToggleGroupExtensions.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public static class ToggleGroupExtensions
{
private static System.Reflection.FieldInfo _toggleListMember;
public static IList<Toggle> GetToggles(this ToggleGroup grp)
{
if (_toggleListMember == null)
{
_toggleListMember = typeof(ToggleGroup).GetField("m_Toggles", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (_toggleListMember == null)
throw new System.Exception("UnityEngine.UI.ToggleGroup source code must have changed in latest version and is no longer compatible with this version of code.");
}
return _toggleListMember.GetValue(grp) as IList<Toggle>;
}
public static int Count(this ToggleGroup grp)
{
return GetToggles(grp).Count;
}
public static Toggle Get(this ToggleGroup grp, int index)
{
return GetToggles(grp)[index];
}
}
This has 3 methods. Get a List of all your toggles, Get a Count of toggles, and Get a specific toggle by index. You can just make this script and leave it in your project it doesn’t need to be attached to any Game Object.
Here is an example of how to use this to store and load the Toggle Setting. I just use the “A” key to store my toggle value, and it loads toggles value on Start(). You can place these functions in whatever appropriate spot you need:
Tester Script to Load/Save Toggles
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Tester : MonoBehaviour {
public GameObject myPanel;
void Start()
{
int x = PlayerPrefs.GetInt("ToggleGroupName",0);
ToggleGroup tg = myPanel.GetComponent<ToggleGroup>();
IList<Toggle> toggles = tg.GetToggles();
tg.SetAllTogglesOff();
toggles[x].isOn = true;
}
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
ToggleGroup tg = myPanel.GetComponent<ToggleGroup>();
IList<Toggle> toggles = tg.GetToggles();
bool found = false;
for (int i = 0; !found && i < tg.Count();++i)
{
if (toggles[i].isOn)
{
found = true;
PlayerPrefs.SetInt("ToggleGroupName", i);
}
}
}
}
}