60 lines
1.2 KiB
C#
60 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
public class ButtonInteraction : MonoBehaviour
|
|
{
|
|
private Sprite normal;
|
|
private Sprite highlight;
|
|
private Sprite pressed;
|
|
|
|
void Start()
|
|
{
|
|
var btn = GetButton();
|
|
normal = GetImage().sprite;
|
|
highlight = btn.spriteState.highlightedSprite;
|
|
pressed = btn.spriteState.pressedSprite;
|
|
}
|
|
|
|
public void SetClickAction(UnityAction action)
|
|
{
|
|
var btn = GetButton();
|
|
btn.onClick.AddListener(action);
|
|
}
|
|
|
|
public void ClickTrigger()
|
|
{
|
|
var btn = GetButton();
|
|
btn.onClick.Invoke();
|
|
}
|
|
|
|
public void HighlightTrigger()
|
|
{
|
|
GetImage().sprite = highlight;
|
|
}
|
|
|
|
public void PressTrigger()
|
|
{
|
|
GetImage().sprite = pressed;
|
|
|
|
}
|
|
|
|
public void NormalTrigger()
|
|
{
|
|
GetImage().sprite = normal;
|
|
}
|
|
|
|
private Image GetImage()
|
|
{
|
|
return GetButton().gameObject.GetComponent<Image>();
|
|
}
|
|
|
|
private Button GetButton()
|
|
{
|
|
return GetComponent<Button>() ?? transform.GetChild(1).GetComponent<Button>();
|
|
}
|
|
}
|