泛型单例类
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);
}
}
}
数据管理器
文章来源:https://www.toymoban.com/news/detail-617767.html
文章来源地址https://www.toymoban.com/news/detail-617767.html
场景管理器
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);
}
}
到了这里,关于Unity进阶--对象池数据场景管理器笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!