代码之家  ›  专栏  ›  技术社区  ›  Siddharth

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

  •  -3
  • Siddharth  · 技术社区  · 6 年前

    enter image description here

    和我分享你的建议。。。

    下面是切换组的详细信息: 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 回复  |  直到 6 年前
        1
  •  1
  •   Hellium    6 年前

    您可以使用 ActiveToggles() function

    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 ) ;
    }
    
        2
  •  0
  •   zambari    6 年前

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

    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
    }