当前位置: 首页 > news >正文

Unity进阶--对象池数据场景管理器笔记

文章目录

  • 泛型单例类
  • 泛型单例类(不带组件版)
  • 对象池管理器
  • 数据管理器
  • 场景管理器

泛型单例类

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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s6XIAsND-1690360831406)(QQ%E6%88%AA%E5%9B%BE20230726093855.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jzymn27z-1690360831407)(QQ%E6%88%AA%E5%9B%BE20230726094052.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HeGkPdaw-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094141.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ed6LFKt-1690360831408)(QQ%E6%88%AA%E5%9B%BE20230726094604.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);}
}
http://www.lryc.cn/news/96662.html

相关文章:

  • 【Seata】微服务集成seata
  • 解决react,<img>src使用require方法引入图片不显示问题
  • 从小白到大神之路之学习运维第67天-------Tomcat应用服务 WEB服务
  • 图解SQL基础知识,小白也能看懂的SQL文章
  • 自动驾驶感知系统-毫米波雷达
  • Esp32_Arduino接入腾讯云笔记
  • python简单入门
  • 如何快速从csv文件搭建一个简单的神经网络模型(回归)
  • Pytorch深度学习-----DataLoader的用法
  • macOS Ventura 13.5 (22G74) Boot ISO 原版可引导镜像下载
  • 【机器学习】 奇异值分解 (SVD) 和主成分分析 (PCA)
  • 如何用logging记录python实验结果?
  • C语言假期作业 DAY 03
  • 使用serverless实现从oss下载文件并压缩
  • 从上到下打印二叉树
  • 【推荐】排序模型的调优
  • 负载均衡安装配置详解
  • Java-逻辑控制
  • UE 透明渲染次序
  • 【C++】多态原理剖析,Visual Studio开发人员工具使用查看类结构cl /d1 reportSingleClassLayout
  • vue实现flv格式视频播放
  • iptables安全技术和防火墙
  • 微信小程序开发5
  • 【算法题】2681. 英雄的力量
  • fastutil简单测试下性能
  • 【FAQ】关于无法判断和区分用户与地图交互手势类型的解决办法
  • 腾讯云裸金属服务器CPU型号处理器主频说明
  • 工程安全监测无线振弦采集仪在建筑物中的应用
  • 【iOS】isKindOfClass和isMemberOfClass方法
  • 李飞飞「具身智能」VoxPoser:0预训练完成复杂指令