mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-23 00:28:23 -05:00
47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace UnityAtoms.MonoHooks
|
|
{
|
|
/// <summary>
|
|
/// Generic base class for all Mono Hooks.
|
|
/// </summary>
|
|
/// <typeparam name="E1">Event of type `AtomEvent<EV>`</typeparam>
|
|
/// <typeparam name="E2">Event of type `AtomEvent<EV, GameObject>`</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<E1, E2, EV, F> : MonoBehaviour
|
|
where E1 : AtomEvent<EV> where E2 : AtomEvent<EV, GameObject>
|
|
where F : AtomFunction<GameObject, GameObject>
|
|
{
|
|
/// <summary>
|
|
/// The Event
|
|
/// </summary>
|
|
public E1 Event;
|
|
|
|
/// <summary>
|
|
/// Event including a GameObject reference.
|
|
/// </summary>
|
|
public E2 EventWithGameObjectReference;
|
|
|
|
/// <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)
|
|
{
|
|
EventWithGameObjectReference.Raise(value, _selectGameObjectReference != null ? _selectGameObjectReference.Call(gameObject) : gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|