OnButtonClickHook should require Button

This commit is contained in:
Adam Ramberg 2019-04-05 14:37:48 +02:00
parent ab02061f12
commit c97e541641

View File

@ -1,23 +1,45 @@
using UnityEngine;
using UnityEngine.UI;
namespace UnityAtoms
{
public class OnButtonClickHook : VoidHook
{
private void Awake()
{
GetComponent<Button>().onClick.AddListener(OnClick);
}
private void OnDestroy()
{
GetComponent<Button>().onClick.RemoveListener(OnClick);
}
private void OnClick()
{
OnHook(new Void());
}
}
}
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());
}
}
}