Shrikrishna Joisa Shrikrishna Joisa - 4 months ago 12
C# Question

How to combine pause and unpause game button in unity [C#] with GUI?

I am working on a Unity C# game and I want my pause button to be GUI [with the pause and unpause image displayed on click].

Any help would be great.Thank you so much!!!

Answer

The easiest way is to create new Button object, and add proper method for displaying correct image. Something like this:

public class ButtonStateHandler:MonoBehaviour
{
    public bool 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;
        }
}

And then remember to add Click method in the inspector to OnClick list on Play/Pause button.

enter image description here