unity-atoms/Packages/FSM/Runtime/FiniteStateMachine/FiniteStateMachineMonoHook.cs

63 lines
1.5 KiB
C#
Raw Normal View History

2020-03-09 23:51:14 +01:00
using System;
using UnityEngine;
namespace UnityAtoms.FSM
{
2020-03-21 22:45:39 +01:00
/// <summary>
/// Needed By FiniteStateMachine in order to gain access to some of the Unity life cycle methods.
/// </summary>
2020-03-11 21:11:27 +01:00
public class FiniteStateMachineMonoHook : MonoBehaviour
2020-03-09 23:51:14 +01:00
{
2020-03-11 21:11:27 +01:00
public static FiniteStateMachineMonoHook GetInstance(bool createIfNotExist = false)
2020-03-09 23:51:14 +01:00
{
if (_instance == null && createIfNotExist)
{
GameObject go = new GameObject("FiniteStateMachineUpdateHook");
2020-03-11 21:11:27 +01:00
_instance = go.AddComponent<FiniteStateMachineMonoHook>();
2020-03-09 23:51:14 +01:00
}
return _instance;
}
2020-03-11 21:11:27 +01:00
public event Action OnStart;
2020-03-09 23:51:14 +01:00
public event Action<float> OnUpdate;
2020-03-11 21:11:27 +01:00
public event Action<float> OnFixedUpdate;
2020-03-09 23:51:14 +01:00
2020-03-11 21:11:27 +01:00
private static FiniteStateMachineMonoHook _instance;
2020-03-09 23:51:14 +01:00
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
}
2020-03-11 21:11:27 +01:00
private void Start()
{
if (OnStart != null)
{
OnStart.Invoke();
}
}
2020-03-09 23:51:14 +01:00
private void Update()
{
if (OnUpdate != null)
{
OnUpdate.Invoke(Time.deltaTime);
}
}
2020-03-11 21:11:27 +01:00
private void FixedUpdate()
{
if (OnFixedUpdate != null)
{
OnFixedUpdate.Invoke(Time.deltaTime);
}
}
2020-03-09 23:51:14 +01:00
}
}