unity-atoms/Source/MonoHooks/OnButtonClickHook.cs
2019-04-05 14:37:48 +02:00

46 lines
986 B
C#

using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityAtoms.Logger;
#endif
namespace UnityAtoms
{
[RequireComponent(typeof(Button))]
public class OnButtonClickHook : VoidHook
{
private void Awake()
{
var button = GetComponent<Button>();
if (button == null)
{
#if UNITY_EDITOR
AtomsLogger.Warning("OnButtonClickHook needs a Button component");
#endif
return;
}
button.onClick.AddListener(OnClick);
}
private void OnDestroy()
{
var button = GetComponent<Button>();
if (button == null)
{
#if UNITY_EDITOR
AtomsLogger.Warning("OnButtonClickHook needs a Button component");
#endif
return;
}
button.onClick.RemoveListener(OnClick);
}
private void OnClick()
{
OnHook(new Void());
}
}
}