unity-atoms/Packages/FSM/Runtime/FiniteStateMachine/FiniteStateMachineMonoHook.cs
Adam Ramberg e4c489c88e WIP
2020-03-11 21:11:27 +01:00

61 lines
1.4 KiB
C#

using System;
using UnityEngine;
namespace UnityAtoms.FSM
{
public class FiniteStateMachineMonoHook : MonoBehaviour
{
public static FiniteStateMachineMonoHook GetInstance(bool createIfNotExist = false)
{
if (_instance == null && createIfNotExist)
{
GameObject go = new GameObject("FiniteStateMachineUpdateHook");
_instance = go.AddComponent<FiniteStateMachineMonoHook>();
}
return _instance;
}
public event Action OnStart;
public event Action<float> OnUpdate;
public event Action<float> OnFixedUpdate;
private static FiniteStateMachineMonoHook _instance;
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
}
private void Start()
{
if (OnStart != null)
{
OnStart.Invoke();
}
}
private void Update()
{
if (OnUpdate != null)
{
OnUpdate.Invoke(Time.deltaTime);
}
}
private void FixedUpdate()
{
if (OnFixedUpdate != null)
{
OnFixedUpdate.Invoke(Time.deltaTime);
}
}
}
}