UIFramework之对象池的使用
来源:互联网 发布:黑龙江网络作家协会 编辑:程序博客网 时间:2024/06/15 20:13
UIFramework之对象池的使用
在游戏开发中,像子弹这类物体会被频繁的创建,然后被销毁,这样的操作对游戏性能也会造成严重的浪费。所以对象池的使用,可以减少需要频繁创建销毁的物体的操作,达到对性能的优化。
/** Name: ObjectPoolManager.cs* Function: Object Pool Manager Class. * * Ver Date Name Change Content* ────────────────────────────────────────────────────────────────────────────* V1.0.0 20170430 http://blog.csdn.net/husheng0 Creat ObjectPoolManager.cs* * Copyright (c). All rights reserved.* */using UnityEngine;using System.Collections;using System.Collections.Generic;using System;/// <summary>/// Object Pool Manager/// </summary>public class ObjectPoolManager : MonoSingletonManager<ObjectPoolManager>{ private Dictionary<Type, Stack<MonoBehaviour>> ObjectDic = new Dictionary<Type, Stack<MonoBehaviour>>(); /// <summary> /// Get or Great Object From Pool /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="objectName">object Name</param> /// <returns>Type</returns> public T GetObjectFromPool<T>(string objectName) where T :MonoBehaviour,IPoolObject,new() { Type type = typeof(T); object obj = null; T ctrl = null; if (ObjectDic.ContainsKey(type)) { if (ObjectDic[type].Count == 0) { obj = GameObject.Instantiate(Resources.Load(objectName)); ctrl = ((GameObject)obj).GetComponent<T>(); } if (ObjectDic[type].Count > 0) { obj = ObjectDic[type].Pop(); ctrl = (T)obj; } } else { obj = GameObject.Instantiate(Resources.Load(objectName)); ctrl = ((GameObject)obj).GetComponent<T>(); } ctrl.gameObject.SetActive(true); if(ctrl!=null)ctrl.Spawn(); return ctrl; } /// <summary> /// Disappear Object To Pool /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="ctrl">object</param> /// <param name="objCount">count</param> public void DisappearObjectToPool<T>(T ctrl,int objCount) where T : MonoBehaviour,IPoolObject, new() { Type type = typeof(T); if (!ObjectDic.ContainsKey(type)) { ObjectDic.Add(type, new Stack<MonoBehaviour>()); } ctrl.UnSpawn(); ctrl.gameObject.SetActive(false); if (ObjectDic[type].Count < objCount) { ObjectDic[type].Push((MonoBehaviour)ctrl); } else { GameObject.Destroy(ctrl.gameObject); } } /// <summary> /// clear pool /// </summary> public void ClearPool() { foreach (KeyValuePair<Type,Stack<MonoBehaviour>> tempStack in ObjectDic) { while(tempStack.Value.Count > 0) { GameObject.Destroy(tempStack.Value.Pop().gameObject); } } }}public interface IPoolObject{ /// <summary> /// init /// </summary> void Spawn(); /// <summary> /// Finallize /// </summary> void UnSpawn();}
using UnityEngine;using System.Collections;public class Bullet : MonoBehaviour, IPoolObject{ public Bullet() { } public void Spawn() { Debug.Log("----->Spawn"); } public void UnSpawn() { Debug.Log("----->UnSpawn"); }}
using UnityEngine;using System.Collections;using System.Collections.Generic;public class Test : MonoBehaviour{ private const string PerfabName = "Bullet"; //限定对象池缓存数目 private int count = 3; private List<Bullet> bulletList = new List<Bullet>(); void OnGUI() { if (GUILayout.Button("Great Bullet")) { for (int i = 0; i < 5; i++) { Bullet bullet = ObjectPoolManager.Instance.GetObjectFromPool<Bullet>(PerfabName); bulletList.Add(bullet); } } if (GUILayout.Button("Disapper Bullet")) { for (int i = 0; i < bulletList.Count; i++) { ObjectPoolManager.Instance.DisappearObjectToPool<Bullet>(bulletList[i], count); } bulletList.Clear(); } if (GUILayout.Button("Distroy All")) { ObjectPoolManager.Instance.ClearPool(); } }}
点击不同的Button,会发现,Bullet.cs的Spawn();UnSpawn();顺利执行。
0 0
- UIFramework之对象池的使用
- UIFramework之功能分析
- UIFramework之功能分析
- UIFramework之通用单例类Singleton
- UIFramework之数据表读取工具
- UIFramework之事件系统实现
- UIFramework之通用单例类Singleton
- UIFramework之UnityEngine.Debug封装
- UIFramework之数据表读取工具
- UIFramework之事件系统实现
- UIFramework之Unity中的Path详解
- UIFramework之Unity中的Path详解
- UIFramework之Unity4.x AssetBundle打包策略
- UIFramework之单例滥用解决策略
- UIFramework之Unity4.x 资源加载策略
- UIFramework之Unity4.x 场景管理策略
- UIFramework之Unity5.x AssetBundle打包策略
- 编写高效的C++程序方法之使用对象池
- 开启nginx和php-fpm的status状态监控
- gulp less编译css压缩和js压缩别名输出
- Spring学习笔记(一)
- sunday算法
- MySQL启程学习回忆录1
- UIFramework之对象池的使用
- 新手求助,C#窗口使用timer配合progressBar做循环进度条,为什么进度条不动
- Windows高效通信模型之IOCP
- bootstrap 模态框 删除确认
- RBF神经网络
- Sublime Text3 配置
- c语言中输出字符指针相关问题
- 拿来就用,基于jq的pc+移动端头像上传插件
- jquery中attr和prop的区别