How to create buttons in Unity? Let’s have a look. We will create and script buttons in this Unity tutorial for beginners.


♥ Subscribe to Oxmond Tutorials:
https://bit.ly/SubscribeOxmondTutorials

● Check Out the Free Asset Packages at the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k


The Button Script:

/* .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © OXMOND / www.oxmond.com */

using UnityEngine;
using UnityEngine.Events;

public class Button : MonoBehaviour
{

    public Sprite btnUp;
    public Sprite btnDown;
    public UnityEvent buttonClick;

    void Awake()
    {
        if (buttonClick == null) { buttonClick = new UnityEvent(); }
    }

    void OnMouseDown()
    {
        GetComponent().sprite = btnDown;
    }

    void OnMouseUp()
    {
        print("Click!");
        buttonClick.Invoke();
        GetComponent().sprite = btnUp;
    }
}