如何组合两个按钮

How to combine two buttons

button_playbutton_pause。我想将它们组合成一个按钮。第一次按下歌曲时,歌曲开始播放。第二次按下 - 暂停。第三次按下时,播放继续。我做不到。

请告诉我如何组合它们。

private void button_play_Click(object sender, EventArgs e)
{
    if ((list_catalog.Items.Count != 0) && (list_catalog.SelectedIndex != -1))
    {
        string current = Vars.Files[list_catalog.SelectedIndex];
        Vars.CurrentTrackNumber = list_catalog.SelectedIndex;
        BassLike.Play(current, BassLike.Volume);
        label_time1.Text = TimeSpan.FromSeconds(BassLike.GetPosOfStream(BassLike.Stream)).ToString();
        label_time2.Text = TimeSpan.FromSeconds(BassLike.GetTimeOfStream(BassLike.Stream)).ToString();
        xrewind.Maximum = BassLike.GetTimeOfStream(BassLike.Stream);
        xrewind.Value = BassLike.GetPosOfStream(BassLike.Stream);
        timer1.Enabled = true;
    }
}

private void button_pause_Click(object sender, EventArgs e)
{
    BassLike.Pause();
}

类似于:

private bool _isPlaying;

private void button_Click(object sender, EventArgs e)
{
    if(!_isPlaying)
    {
        mediaThing.Play();
        button1.Text = "Pause";
    }
    else
    {
        mediaThing.Pause();
        button1.Text = "Play";
    }

    _isPlaying = !_isPlaying;
}

最简单的方法是创建一个新的 Button 对象,并添加正确的方法来显示正确的图像。像这样:

public class ButtonStateHandler:MonoBehaviour {
public boolean isClicked;
public Button myBtn;
public Sprite Play;
public Sprite Pause;

public void Click(){
    changeState();
}
private void changeState(){
    isClicked = !isClicked;
    if(isClicked)       myBtn.image.sprite = Play;
    else myBtn.image.sprite = Pause;
    }
}

希望对您有所帮助!