如何从切换组中选择切换?

时间:2018-10-10 11:59:39

标签: c# unity3d

我已经创建了一个切换组,现在我想以最简单的方式来检索选定的切换项。

enter image description here

与我分享您的建议...

编辑: 在这里,您具有ToggleGroup详细信息: enter image description here

我正在使用此代码进行检查:

public class RandomMatchMakerPanelController : MonoBehaviour
{


public Sprite defaultPlayerPhoto;
//
[Header ("Player-1")]
public Image player1Photo;
public Text player1NameText;
public Text player1CountryText;
public ToggleGroup player1BallGroup;

[Header ("Player-2")]
public Image player2Photo;
public Text player2NameText;
public Text player2CountryText;
public ToggleGroup player2BallGroup;

void OnEnable ()
{
//      StartCoroutine (CreateRandomMatches ());
        GetSelectedToggle ();
    ShowLocalPlayerDetails ();
}

private void ShowLocalPlayerDetails ()
{
    if (DataCollection.localPlayer.ProfilePhoto == null)
        player1Photo.sprite = defaultPlayerPhoto;
    else
        player1Photo.sprite = DataCollection.localPlayer.ProfilePhoto;
    player1NameText.text = DataCollection.localPlayer.PlayerName;
    player1CountryText.text = DataCollection.localPlayer.Country;
}

private void GetSelectedToggle ()
{
//      Toggle[] ballToggles = player1BallGroup.GetComponentsInChildren<Toggle> ();
    foreach (Toggle toggle in player1BallGroup.ActiveToggles()) {
        Debug.Log ("toggle: "+ toggle.isOn);
    }
    // May have several selected toggles
//      foreach (Toggle toggle in ballToggles) {
//          if(toggle.isOn)
//      }
}

IEnumerator CreateRandomMatches ()
{
    yield return new WaitForSeconds (0.25f);
    RandomMatchMaker matchMaker = GameObject.FindGameObjectWithTag (GameConstants.TAG_NETWORK_MANAGER).GetComponent<RandomMatchMaker> ();
    matchMaker.FindInternetMatch ("Dodgels-");
}

public void OnBackButtonClick ()
{
    SoundManager.Instance.PlayButtonClickSound ();
    Camera.main.SendMessage (GameConstants.ACTIVATE_RANDOM_MATCH_MAKER_PANEL, false, SendMessageOptions.DontRequireReceiver);
    Camera.main.SendMessage (GameConstants.ACTIVATE_RANDOM_PLAYER_GAMEPLAY, false, SendMessageOptions.DontRequireReceiver);
    Camera.main.SendMessage (GameConstants.ACTIVATE_MAIN_MENU_PANEL, true, SendMessageOptions.DontRequireReceiver);
}
}

尽管每次一次切换始终保持活动状态,但目前控制台中什么也没有显示。

2 个答案:

答案 0 :(得分:1)

您可以使用ActiveToggles() function获得所有活动切换,该切换选择组中的活动切换集。通常,此集合由唯一的Toggle组成。

using System.Linq;

// ...

public UnityEngine.UI.ToggleGroup ToggleGroup ; // Drag & drop the desired ToggleGroup in the inspector

private void Start()
{
    if( ToggleGroup == null ) ToggleGroup = GetComponent<ToggleGroup>();
}

public void LogSelectedToggle()
{
    // May have several selected toggles
    foreach( Toggle toggle in ToggleGroup.ActiveToggles() )
    {
         Debug.Log( toggle, toggle ) ;
    }

    // OR

    Toggle selectedToggle = ToggleGroup.ActiveToggles().FirstOrDefault();
    if( selectedToggle != null )
        Debug.Log( selectedToggle, selectedToggle ) ;
}

答案 1 :(得分:0)

从切换的父级执行以下方法

Toggle GetSelectedToggle()
{
    Toggle[] toggles=GetComponentsInChildren<Toggle>();
    foreach (var t in toggles)
    if (t.isOn) return t;  //returns selected toggle
    return null;           // if nothing is selected return null
}