unity-atoms/Packages/MonoHooks/Runtime/Hooks/OnButtonClickHook.cs
2019-10-16 01:43:51 +02:00

32 lines
741 B
C#

using UnityEngine;
using UnityEngine.UI;
namespace UnityAtoms.MonoHooks
{
/// <summary>
/// Mono Hook for On Button Click
/// </summary>
[EditorIcon("atom-icon-delicate")]
[AddComponentMenu("Unity Atoms/Hooks/On Button Click")]
[RequireComponent(typeof(Button))]
public sealed class OnButtonClickHook : VoidHook
{
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()
{
OnHook(new Void());
}
}
}