一、.基础缓存池实现

继承的Singleton脚本为
- public class Singleton<T> where T : new()
- {
- private static T _instance;
- public static T GetIstance()
- {
- if (_instance == null)
- _instance = new T();
- return _instance;
- }
- }
复制代码 1.PoolManager
2.Test
挂载到摄像机上
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Test : MonoBehaviour
- {
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- //路径名
- PoolMgr.GetInstance().GetObj("Test/Cube", (o) => { });
- }
- }
- }
复制代码 3.DlaySave
挂载到预制体Cube上
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DelaySave : MonoBehaviour
- {
- //当对象激活时,进入生命周期函数
- void OnEnable()
- {
- Invoke("Save", 1);
- }
- void Save()
- {
- PoolManager.GetIstance().SaveObj(this.gameObject.name, this.gameObject);
- }
- }
复制代码
二、缓存池优化
在天生物体时设置为Pool的子物体


来源:https://blog.csdn.net/weixin_53163894/article/details/132037520
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |