159 lines
3.8 KiB
C#
159 lines
3.8 KiB
C#
using UnityEngine;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine.UI;
|
||
using UnityEngine.Events;
|
||
|
||
public class ActionCenter : MonoBehaviour
|
||
{
|
||
private static ActionCenter _instance;
|
||
public static ActionCenter Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
string name = "ActionCenter";
|
||
GameObject obj = GameObject.Find(name);
|
||
if (obj == null)
|
||
{
|
||
obj = new GameObject(name);
|
||
_instance = obj.AddComponent<ActionCenter>();
|
||
}
|
||
else
|
||
{
|
||
_instance = obj.GetComponent<ActionCenter>();
|
||
if (_instance == null)
|
||
{
|
||
_instance = obj.AddComponent<ActionCenter>();
|
||
}
|
||
}
|
||
}
|
||
return _instance;
|
||
}
|
||
}
|
||
//场景中所有ActionBase对象列表
|
||
private List<ActionBase> actionList = new List<ActionBase>();
|
||
|
||
//初始化,场景加载完成后调用
|
||
public void Init()
|
||
{
|
||
ActionBase[] arr = Resources.FindObjectsOfTypeAll<ActionBase>();
|
||
for (int i = 0; i < arr.Length; i++)
|
||
{
|
||
if (arr[i].gameObject.scene.buildIndex >= 0)
|
||
{
|
||
arr[i].Init();
|
||
arr[i].RegisterAction();
|
||
actionList.Add(arr[i]);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void AddAction(ActionBase action)
|
||
{
|
||
actionList.Add(action);
|
||
action.Init();
|
||
action.RegisterAction();
|
||
}
|
||
|
||
public void RemoveAction(ActionBase action)
|
||
{
|
||
if (actionList.Contains(action))
|
||
{
|
||
actionList.Remove(action);
|
||
}
|
||
}
|
||
|
||
void OnDestroy()
|
||
{
|
||
for (int i = 0; i < actionList.Count; i++)
|
||
{
|
||
actionList[i].RemoveAction();
|
||
}
|
||
}
|
||
|
||
#region 执行事件
|
||
|
||
//执行事件
|
||
public void DoAction(UnityAction action)
|
||
{
|
||
action?.Invoke();
|
||
}
|
||
|
||
//执行事件
|
||
public void DoAction<T>(UnityAction<T> action, T t)
|
||
{
|
||
action?.Invoke(t);
|
||
}
|
||
|
||
//执行事件
|
||
public void DoAction<T, U>(UnityAction<T, U> action, T t, U u)
|
||
{
|
||
action?.Invoke(t, u);
|
||
}
|
||
|
||
//执行事件
|
||
public void DoAction<T, U, V>(UnityAction<T, U, V> action, T t, U u, V v)
|
||
{
|
||
action?.Invoke(t, u, v);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
//delay秒后,执行事件
|
||
public void DoActionDelay(UnityAction action, float delay)
|
||
{
|
||
StartCoroutine(DoActionDelayCor(action, delay));
|
||
}
|
||
|
||
IEnumerator DoActionDelayCor(UnityAction action, float delay)
|
||
{
|
||
yield return new WaitForSeconds(delay);
|
||
action?.Invoke();
|
||
}
|
||
|
||
|
||
//delay秒后,执行事件
|
||
public void DoActionDelay<T>(UnityAction<T> action, float delay, T t)
|
||
{
|
||
StartCoroutine(DoActionDelayCor(action, delay, t));
|
||
}
|
||
|
||
IEnumerator DoActionDelayCor<T>(UnityAction<T> action, float delay, T t)
|
||
{
|
||
yield return new WaitForSeconds(delay);
|
||
action?.Invoke(t);
|
||
}
|
||
|
||
|
||
//delay秒后,执行事件
|
||
public void DoActionDelay<T, U>(UnityAction<T, U> action, float delay, T t, U u)
|
||
{
|
||
StartCoroutine(DoActionDelayCor(action, delay, t, u));
|
||
}
|
||
|
||
IEnumerator DoActionDelayCor<T, U>(UnityAction<T, U> action, float delay, T t, U u)
|
||
{
|
||
yield return new WaitForSeconds(delay);
|
||
action?.Invoke(t, u);
|
||
}
|
||
|
||
|
||
//delay秒后,执行事件
|
||
public void DoActionDelay<T, U, V>(UnityAction<T, U, V> action, float delay, T t, U u, V v)
|
||
{
|
||
StartCoroutine(DoActionDelayCor(action, delay, t, u, v));
|
||
}
|
||
|
||
IEnumerator DoActionDelayCor<T, U, V>(UnityAction<T, U, V> action, float delay, T t, U u, V v)
|
||
{
|
||
yield return new WaitForSeconds(delay);
|
||
action?.Invoke(t, u, v);
|
||
}
|
||
|
||
#endregion
|
||
}
|