mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-24 00:58:59 -05:00
43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace UnityAtoms.MonoHooks
|
|
{
|
|
/// <summary>
|
|
/// Generic base class for all Mono Hooks.
|
|
/// </summary>
|
|
/// <typeparam name="E">Event of type `AtomEvent<EV>`</typeparam>
|
|
/// <typeparam name="EV">Event value type</typeparam>
|
|
/// <typeparam name="F">Function type `AtomFunction<GameObject, GameObject>`</typeparam>
|
|
[EditorIcon("atom-icon-delicate")]
|
|
public abstract class MonoHook<E, EV, F> : MonoBehaviour
|
|
where E : AtomEvent<EV>
|
|
where F : AtomFunction<GameObject, GameObject>
|
|
{
|
|
/// <summary>
|
|
/// The Event
|
|
/// </summary>
|
|
public E Event { get => _event; set => _event = value; }
|
|
|
|
[SerializeField]
|
|
private E _event;
|
|
|
|
/// <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);
|
|
}
|
|
RaiseWithGameObject(value, _selectGameObjectReference != null ? _selectGameObjectReference.Call(gameObject) : gameObject);
|
|
}
|
|
|
|
protected abstract void RaiseWithGameObject(EV value, GameObject gameObject);
|
|
}
|
|
}
|