天天看點

02.基礎架構Mono子產品

02.基礎架構Mono子產品
02.基礎架構Mono子產品
02.基礎架構Mono子產品
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonoController : MonoBehaviour
{
    private Action callBack = null;

    void Update()
    {
        this.callBack?.Invoke();
    }

    public void AddListener(Action callBack)
    {
        this.callBack += callBack;
    }
    public void RemoveListener(Action callBack)
    {
        this.callBack -= callBack;
    }
}      
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class MonoMgr : BaseManager<MonoMgr>
{
    private MonoController controller;
    public MonoMgr()
    {
        GameObject mono=new GameObject("Mono");
        mono.AddComponent<MonoController>();
        controller= mono.GetComponent<MonoController>();
    }
    public void AddListener(Action callBack)
    {
        controller.AddListener(callBack);
    }
    public void RemoveListener(Action callBack)
    {
        controller.RemoveListener(callBack);
    }
}      
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonoTest
{
    public void AddListen()
    {
        MonoMgr.GetInstance.AddListener(DebugInfo);
    }
    public void Remove()
    {
        MonoMgr.GetInstance.RemoveListener(DebugInfo);
    }
    private void DebugInfo()
    {
        Debug.Log("Test");
    }
}      
private MonoTest test;
    void Start()
    {
        MusicMgr.GetInstance.Init();
        PoolMgr.GetInstance.Init();
        test=new MonoTest();
        test.AddListen();
    }

   
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            test.Remove();
        }
    }