Unity——public Mono module, you can call coroutines without inheriting Mono, and manage Update uniformly

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// Mono的管理者
/// 1.生命周期函数
/// 2.事件
/// 3.协程
/// </summary>
public class MonoController : MonoBehaviour
{
    private event UnityAction updateEvent;
    private void Start()
    {
        DontDestroyOnLoad(this.gameObject);
    }
    private void Update()
    {
        if (updateEvent != null)
            updateEvent();
    }

    /// <summary>
    /// 给外部提供的 添加帧更新事件的函数
    /// </summary>
    /// <param name="fun"></param>
    public void AddUpdateListener(UnityAction fun) 
    {
        updateEvent += fun;
    }

    /// <summary>
    /// 提供给外部用于移除帧更新事件函数
    /// </summary>
    /// <param name="fun"></param>
    public void RemoveUpdateListener(UnityAction fun) 
    {
        updateEvent -= fun;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// 1.可以提供给外部添加帧更新事件的方法
/// 2.可以提供给外部添加 协程 的方法
/// </summary>
public class MonoMgr : BaseManager<MonoMgr>
{
    private MonoController controller;

    public MonoMgr() 
    {
        //保证了MonoController对象的唯一性
        GameObject obj = new GameObject("MonoController");
        controller = obj.AddComponent<MonoController>();
    }

    /// <summary>
    /// 给外部提供的 添加帧更新事件的函数
    /// </summary>
    /// <param name="fun"></param>
    public void AddUpdateListener(UnityAction fun)
    {
        controller.AddUpdateListener(fun);
    }

    /// <summary>
    /// 提供给外部用于移除帧更新事件函数
    /// </summary>
    /// <param name="fun"></param>
    public void RemoveUpdateListener(UnityAction fun)
    {
        controller.RemoveUpdateListener(fun);
    }

    /// <summary>
    /// 重新封装一下协程 可以封装很多协程的方法,这里省略了
    /// </summary>
    /// <param name="routine"></param>
    /// <returns></returns>
    public Coroutine StartCoroutine(IEnumerator routine) 
    {
        return controller.StartCoroutine(routine);
    }
}
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class TestTest 
{
    public TestTest() 
    {
        MonoMgr.GetInstance().StartCoroutine(Test123());
    }
    IEnumerator Test123() 
    {
        yield return new WaitForSeconds(1f);
        Debug.Log("123412341234");
    }
    public void Update() 
    {
        Debug.Log("TestTest");
    }
}
public class TestTest02 
{
    
}

public class Test : MonoBehaviour
{
    void Start()
    {
        TestTest t = new TestTest();
        MonoMgr.GetInstance().AddUpdateListener(t.Update);
        MonoMgr.GetInstance().AddUpdateListener(test);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) 
        {
            PoolMgr.GetInstance().GetObj("Test/Cube");
        }
        if (Input.GetKeyDown(KeyCode.KeypadEnter)) 
        {
            PoolMgr.GetInstance().GetObj("Test/Sphere");
        }
    }

    public void test() 
    {
        print("测试");
    }
}

 We can call the Update of each class in the Start method

 

おすすめ

転載: blog.csdn.net/weixin_46711336/article/details/129187215