查看: 535|回复: 0
打印 上一主题 下一主题

[其他] Unity对象池的实现

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53456
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-30 00:09:28 |只看该作者 |倒序浏览
网上介绍对象池的文章有很多,但是总感觉代码不太清晰,并不适合新手学习最近在一个工程里看到一段对象池的代码感觉不错,故分享一下
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;

  5. public class PoolManager : UnitySingleton<PoolManager> {

  6.         public static Dictionary<Type, Stack<IPoolable>> ObjectPoolDic = new Dictionary<Type, Stack<IPoolable>>();
  7.         public static Dictionary<Type, int> ObjectPoolSizeDic = new Dictionary<Type,int>();
  8.        
  9.         void Start () {

  10.         }
  11.        
  12.         public void RegistPoolableType(Type type, int poolSize)
  13.         {
  14.                 if (!ObjectPoolDic.ContainsKey(type))
  15.                 {
  16.                         ObjectPoolDic[type] = new Stack<IPoolable>();
  17.                         ObjectPoolSizeDic[type] = poolSize;
  18.                 }
  19.         }
  20.        
  21.         public bool HasPoolObject(Type type)
  22.         {
  23.                 return ObjectPoolDic.ContainsKey(type) && ObjectPoolDic[type].Count > 0;
  24.         }
  25.        
  26.         public bool IsPoolFull(Type type)
  27.         {
  28.                 if (!ObjectPoolDic.ContainsKey(type))
  29.                         return true;
  30.                 else if (ObjectPoolDic[type].Count >= ObjectPoolSizeDic[type])
  31.                         return true;
  32.                 return false;
  33.         }
  34.        
  35.         public IPoolable TakePoolObject(Type type)
  36.         {
  37.                 if (ObjectPoolDic.ContainsKey(type) && ObjectPoolDic[type].Count > 0)
  38.                 {
  39.                         return ObjectPoolDic[type].Pop();
  40.                 }
  41.                 else
  42.                 {
  43.                         return null;
  44.                 }
  45.         }
  46.        
  47.         public bool PutPoolObject(Type type, IPoolable obj)
  48.         {
  49.                 if (!ObjectPoolDic.ContainsKey(type) || ObjectPoolDic[type].Count >= ObjectPoolSizeDic[type])
  50.                 {
  51.                         GameObject.Destroy((obj as MonoBehaviour).gameObject);
  52.                         return false;
  53.                 }
  54.                 else
  55.                 {
  56.                         (obj as MonoBehaviour).gameObject.SetActive(false);
  57.                         //(obj as MonoBehaviour).transform.parent = GameManager.Instance.PoolRoot;
  58.                         ObjectPoolDic[type].Push(obj);
  59.                         return true;
  60.                 }
  61.         }
  62. }
复制代码
首先继承自一个单例类,就可以用PoolManager.Instance来获取这个类的单例了
定义了两个字典,一个用来对应存储对应的对象类型的栈,一个用来记录实例化的最大个数来控制内存
用的时候Pop,用完了Push
可存储在对象池的对象必须实现IPoolable接口
  1. using UnityEngine;
  2. using System.Collections;

  3. public interface IPoolable {

  4.         void Destroy();
  5. }
复制代码
destroy里可以这样写
  1. if (!PoolManager.Instance.IsPoolFull(GetType()))
  2.                 {
  3.                         PoolManager.Instance.PutPoolObject(GetType(), this);
  4.                 }
  5.                 else
  6.                 {
  7.                         GameObject.Destroy(this.gameObject);
  8.                 }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-29 11:29 , Processed in 0.078586 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部