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

60 lines
1.4 KiB
C#
Raw Normal View History

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