unity-atoms/Examples/Assets/Intro/UI/RaiseEventOnButtonClick.cs

30 lines
636 B
C#
Raw Normal View History

2019-07-05 04:07:05 -04:00
using UnityEngine;
using UnityEngine.UI;
2019-07-05 04:07:05 -04:00
namespace UnityAtoms.Examples
{
[RequireComponent(typeof(Button))]
2019-07-05 04:07:05 -04:00
public sealed class RaiseEventOnButtonClick : MonoBehaviour
{
2019-07-05 04:07:05 -04:00
[SerializeField]
private VoidEvent _event;
private void Awake()
{
var button = GetComponent<Button>();
button.onClick.AddListener(OnClick);
}
private void OnDestroy()
{
var button = GetComponent<Button>();
button.onClick.RemoveListener(OnClick);
}
private void OnClick()
{
2019-07-05 04:07:05 -04:00
_event.Raise();
}
}
}