文章目录
- 泛型单例类
- 泛型单例类(不带组件版)
- 对象池管理器
- 数据管理器
- 场景管理器
泛型单例类
using System.Collections;
using System.Collections.Generic;public abstract class ManagersSingle<T> where T : new()
{private static T instance;public static T Instance{get{if (instance == null){instance = new T();}return instance;}}
}
泛型单例类(不带组件版)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MyrSingletonBase<T> : MonoBehaviour where T : MonoBehaviour
{private static T instance;public static T Instance {get{return instance;}}protected virtual void Awake() {instance = this as T;}protected virtual void OnDestroy() {instance = null;}
}
对象池管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoolStack{public Stack <UnityEngine.Object> stack = new Stack<Object>();public int MaxCount = 100;public void Push(UnityEngine.Object go){if (stack.Count < MaxCount) stack.Push(go);else GameObject.Destroy(go);}public UnityEngine.Object Pop() {if (stack.Count > 0) return stack.Pop();return null; }public void Clear(){foreach (UnityEngine.Object go in stack) GameObject.Destroy(go);stack.Clear();}}public class PoolManager :ManagersSingle<PoolManager>
{Dictionary<string, PoolStack> poolDic = new Dictionary<string, PoolStack>();public UnityEngine.Object Spawn(string poolName, UnityEngine.Object prefab){if (!poolDic.ContainsKey(poolName)) poolDic.Add(poolName, new PoolStack());UnityEngine.Object go = poolDic[poolName].Pop();if (go == null) go = GameObject.Instantiate(prefab);return go;}public void UnSpawn(string poolName){if (poolDic.ContainsKey(poolName)){poolDic[poolName].Clear();poolDic.Remove(poolName);}}}
数据管理器
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uWsTk7ac-1690360831404)(QQ%E6%88%AA%E5%9B%BE20230726091635.png)]](https://img-blog.csdnimg.cn/5b95eef7864a4938a402ff1632fc7335.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s6XIAsND-1690360831406)(QQ%E6%88%AA%E5%9B%BE20230726093855.png)]](https://img-blog.csdnimg.cn/11682743beb740df8f3ab317aa1a9a9b.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jzymn27z-1690360831407)(QQ%E6%88%AA%E5%9B%BE20230726094052.png)]](https://img-blog.csdnimg.cn/abbc038edf7a4c63af290d7a5774fb19.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HeGkPdaw-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094141.png)]](https://img-blog.csdnimg.cn/ec068f4ca7174779a3956eacd95a883c.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ed6LFKt-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094604.png)]](https://img-blog.csdnimg.cn/0c8fc11547ae440a9e03751e55cdfcc1.png)
场景管理器
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class SceneManager : MyrSingletonBase<SceneManager>
{public List<string> sceneList = new List<string>();public int CurrentIndex = 0;private System.Action<float> currentAction;private AsyncOperation operation;public void LoadScene(string sceneName, System.Action<float> action){currentAction = action;if (sceneList.Contains(sceneName)){CurrentIndex = sceneList.IndexOf(sceneName);operation = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, UnityEngine.SceneManagement.LoadSceneMode.Single);}}void Update(){if (operation != null){currentAction(operation.progress);if (operation.progress >= 1) operation = null;}}public void LoadPre(System.Action<float> action){CurrentIndex--;LoadScene(sceneList[CurrentIndex], action);}public void LoadNext(System.Action<float> action){CurrentIndex++;LoadScene(sceneList[CurrentIndex], action);}
}