2020-03-09 18:51:14 -04:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace UnityAtoms.FSM
|
|
|
|
{
|
2020-03-21 17:45:39 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Needed By FiniteStateMachine in order to gain access to some of the Unity life cycle methods.
|
|
|
|
/// </summary>
|
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
|
|
|
}
|
|
|
|
}
|