作用:让没有继承Mono的类可以开启协程,可以统一管理Update的更新。
-------------------------------MonoManager ------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class MonoManager : BaseManager<MonoManager>
{
private MonoController monoController;
public MonoManager()
{
GameObject obj = new GameObject("#MonoController#");
monoController = obj.AddComponent<MonoController>();
}
/// <summary>
/// 添加桢更新事件
/// </summary>
/// <param name="action"></param>
public void AddUpdateListerer(UnityAction action)
{
monoController.AddUpdateListerer(action);
}
/// <summary>
/// 删除桢更新事件
/// </summary>
/// <param name="action"></param>
public void RemoveUpdateListerer(UnityAction action)
{
monoController.RemoveUpdateListerer(action);
}
/// <summary>
/// 开启协程
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Coroutine StartCoroutine(string name)
{
return monoController.StartCoroutine(name);
}
}
-------------------------------MoneController------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class MonoController : MonoBehaviour
{
private event UnityAction updateEvent;
private void Start()
{
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (updateEvent != null)
updateEvent.Invoke();
}
/// <summary>
/// 添加桢更新事件
/// </summary>
/// <param name="action"></param>
public void AddUpdateListerer(UnityAction action)
{
updateEvent += action;
}
/// <summary>
/// 删除桢更新事件
/// </summary>
/// <param name="action"></param>
public void RemoveUpdateListerer(UnityAction action)
{
updateEvent -= action;
}
}