unity-atoms/Packages/MonoHooks/Runtime/Hooks/MonoHook.cs

61 lines
2.0 KiB
C#
Raw Normal View History

2019-10-15 19:43:51 -04:00
using UnityEngine;
using UnityEngine.Serialization;
namespace UnityAtoms.MonoHooks
{
/// <summary>
/// Generic base class for all Mono Hooks.
/// </summary>
/// <typeparam name="E1">Event of type `AtomEvent&lt;EV&gt;`</typeparam>
/// <typeparam name="E2">Event of type `AtomEvent&lt;EV, GameObject&gt;`</typeparam>
/// <typeparam name="EV">Event value type</typeparam>
2020-03-01 15:32:52 -05:00
/// <typeparam name="EV">Event value type with GameObject</typeparam>
2019-10-15 19:43:51 -04:00
/// <typeparam name="F">Function type `AtomFunction&lt;GameObject, GameObject&gt;`</typeparam>
[EditorIcon("atom-icon-delicate")]
2020-03-01 15:32:52 -05:00
public abstract class MonoHook<E1, E2, EV, EVG, F> : MonoBehaviour
where E1 : AtomEvent<EV>
where E2 : AtomEvent<EVG>
2019-10-15 19:43:51 -04:00
where F : AtomFunction<GameObject, GameObject>
{
/// <summary>
/// The Event
/// </summary>
2020-03-01 15:32:52 -05:00
public E1 Event { get => _event; set => _event = value; }
2019-10-15 19:43:51 -04:00
/// <summary>
/// Event including a GameObject reference.
/// </summary>
2020-03-01 15:32:52 -05:00
public E2 EventWithGameObjectReference { get => _eventWithGameObjectReference; set => _eventWithGameObjectReference = value; }
[SerializeField]
private E1 _event;
[SerializeField]
private E2 _eventWithGameObjectReference;
2019-10-15 19:43:51 -04:00
/// <summary>
/// Selector function for the Event `EventWithGameObjectReference`. Makes it possible to for example select the parent GameObject and pass that a long to the `EventWithGameObjectReference`.
/// </summary>
[SerializeField]
protected F _selectGameObjectReference;
protected void OnHook(EV value)
{
if (Event != null)
{
Event.Raise(value);
}
if (EventWithGameObjectReference != null)
{
2020-03-01 15:32:52 -05:00
RaiseWithGameObject(value, _selectGameObjectReference != null ? _selectGameObjectReference.Call(gameObject) : gameObject);
2019-10-15 19:43:51 -04:00
}
}
2020-03-01 15:32:52 -05:00
protected abstract void RaiseWithGameObject(EV value, GameObject gameObject);
2019-10-15 19:43:51 -04:00
}
}