unity3d 如何从切换组中获取选中的切换?

kmbjn2e3  于 2022-12-19  发布在  其他
关注(0)|答案(2)|浏览(244)

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

与我分享您的建议...

**编辑:**这里有切换组详细信息:

我用这个代码来检查:

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);
}
}

目前,控制台中未显示任何内容,但一次一个切换始终保持活动状态。

1qczuiv0

1qczuiv01#

您可以使用ActiveToggles()函数获取所有激活的切换,该函数选择组中激活的切换集。通常,该集合由唯一的切换组成。

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 ) ;
}
vd2z7a6w

vd2z7a6w2#

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

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
}

相关问题