• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

137 - MonoController.cs - 可用UnityAPI

作者:

贺及楼

成为作者

更新日期:2023-09-17 11:01:04

MonoController.cs

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. public class MonoController : MonoBehaviour
  7. {
  8. private event UnityAction updateEvent;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. DontDestroyOnLoad(this.gameObject);//不会转场消失
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. if (updateEvent != null)
  18. updateEvent();//不为空执行
  19. }
  20. //给外部提供的 添加更新帧事件
  21. public void AddUpdateListener(UnityAction fun)
  22. {
  23. updateEvent += fun;
  24. }
  25. //给外部提供的 移除更新帧事件
  26. public void RemoveUpdateListener(UnityAction fun)
  27. {
  28. updateEvent -= fun;
  29. }
  30. }

MonoManager.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. public class MonoManager : BaseManager<MonoManager>
  6. {
  7. private MonoController controller;
  8. public MonoManager()
  9. {
  10. //保证MonoController的唯一
  11. GameObject obj = new GameObject("MonoController");
  12. controller = obj.AddComponent<MonoController>();
  13. }
  14. //给外部提供的 添加更新帧事件
  15. public void AddUpdateListener(UnityAction fun)
  16. {
  17. controller.AddUpdateListener(fun);
  18. }
  19. //给外部提供的 移除更新帧事件
  20. public void RemoveUpdateListener(UnityAction fun)
  21. {
  22. controller.RemoveUpdateListener(fun);
  23. }
  24. public Coroutine StartCoroutine(IEnumerator routine)
  25. {
  26. return controller.StartCoroutine(routine);
  27. }
  28. }