mirror of
https://github.com/unity-atoms/unity-atoms.git
synced 2025-01-23 08:38:23 -05:00
61 lines
1.4 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
} |