Creating a Button

Checked with version: 2017.3

-

Difficulty: Intermediate

In this session Online Evangelist Matt Gambell will take us through building a system that allows for a flexible and customisable way to work with UI in a project by taking advantage of the power of ScriptableObjects.

Creating a Button

Intermediate User Interface (UI)

FlexibleUIButton

Code snippet

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Image))]
[RequireComponent(typeof(Button))]
public class FlexibleUIButton : FlexibleUI
{

    Button button;
    Image image;

    public ButtonType buttonType;

    protected override void OnSkinUI()
    {

        base.OnSkinUI();
        image = GetComponent();
        button = GetComponent();

        button.transition = Selectable.Transition.SpriteSwap;
        button.targetGraphic = image;

        image.sprite = skinData.buttonSprite;
        image.type = Image.Type.Sliced;
        button.spriteState = skinData.buttonSpriteState;
}

Related tutorials