很多时候我们对于物体(比如弓箭)大量的生成与销毁,这个时候可以把弓箭放在内存池中进行管理,加快体验。自己Copy了一个简易版的。

一、代码

GameObjectPoolManager.cs

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;// A general pool object for reusable game objects.
//
// It supports spawning and unspawning game objects that are
// instantiated from a common prefab. Can be used preallocate
// objects to avoid calls to Instantiate during gameplay. Can
// also create objects on demand (which it does if no objects
// are available in the pool).
public class GameObjectPoolManager {public GameObject parent;

    // The prefab that the game objects will be instantiated from.private GameObject prefab;
   // The list of available game objects (initially empty by default).private Stack<GameObject> available;// The list of all game objects created thus far (used for efficiently// unspawning all of them at once, see UnspawnAll).private List<GameObject> all;// An optional function that will be called whenever a new object is instantiated.// The newly instantiated object is passed to it, which allows users of the pool// to do custom initialization.private Callback<GameObject> initializeFunction;private Callback<GameObject> destroyFunction;Indicates whether the pool's game objects should be activated/deactivatedrecursively (i.e. the game object and all its children) or non-recursively (just thegame object).//private var setActiveRecursively : boolean;// Creates a pool.// The initialCapacity is used to initialize the .NET collections, and determines// how much space they pre-allocate behind the scenes. It does not pre-populate the// collection with game objects. For that, see the PrePopulate function.// If an initialCapacity that is <= to zero is provided, the pool uses the default// initial capacities of its internal .NET collections.public GameObjectPoolManager(GameObject prefab, Callback<GameObject> initializeFunction, Callback<GameObject> destroyFunction){this.prefab = prefab;this.parent = new GameObject(prefab.name + "Pool");this.available = new Stack<GameObject>();this.all = new List<GameObject>();this.initializeFunction = initializeFunction;this.destroyFunction = destroyFunction;}// Spawn a game object with the specified position/rotation.public GameObject Spawn(Vector3 position, Quaternion rotation) {GameObject result = null;if (available.Count == 0){// Create an object and initialize it.result = GameObject.Instantiate(prefab, position, rotation) as GameObject;result.transform.parent = parent.transform;// Keep track of it.all.Add(result);}else{result = available.Pop() as GameObject;// Get the result's transform and reuse for efficiency.//Calling gameObject.transform is expensive.var resultTrans = result.transform;resultTrans.position = position;resultTrans.rotation = rotation;result.SetActive(true);}if (initializeFunction != null) initializeFunction(result);return result;}// Unspawn the provided game object.// The function is idempotent. Calling it more than once for the same game object is// safe, since it first checks to see if the provided object is already unspawned.// Returns true if the unspawn succeeded, false if the object was already unspawned.public bool Unspawn(GameObject obj){if (!available.Contains(obj)){ // Make sure we don't insert it twice.available.Push(obj);obj.SetActive(false);if (destroyFunction != null) destroyFunction(obj);return true; // Object inserted back in stack.}return false; // Object already in stack.}// Pre-populates the pool with the provided number of game objects.void PrePopulate(int count){GameObject[] array = new GameObject[count];for(var i = 0; i < count; i++){array[i] = Spawn(Vector3.zero, Quaternion.identity);//this.SetActive(array[i], false);}for(var j = 0; j < count; j++){Unspawn(array[j]);}}
       // Unspawns all the game objects created by the pool.void UnspawnAll(){foreach (var item in available){Unspawn(item);}}// Returns the number of active objects.int GetActiveCount(){return all.Count - available.Count;}// Returns the number of available objects.int GetAvailableCount(){return available.Count;}
}

二、应用

还是用之前的BezierTest.cs的例子

    void Start(){arrowPool = new GameObjectPoolManager(arrowPrefab, null, null);//controlPoints = ControlPoints(transform, right);}#endregionvoid Test(bool fireRight){Transform end = fireRight ? right : left;///在中心点生成弓箭GameObject curArrow = arrowPool.Spawn(transform.position, Quaternion.Euler(Vector3.zero));///计算LookTarget的点 与 贝塞尔曲线的第三个控制点Vector3[] points = Re_LookTarget_MiddlePerpendicularPoint(curArrow.transform, end);///初始化发射ArrowControl arrowControl = curArrow.GetComponent<ArrowControl>();arrowControl.Init(points[0],points[1],end.position,3.0f,delegate(){arrowPool.Unspawn(curArrow);});}

  

转载于:https://www.cnblogs.com/chongxin/p/4340125.html

山寨小小军团开发笔记 之 GamePool相关推荐

  1. Android开发笔记(七十三)代码混淆与反破解

    代码混淆 ProGuard是ADT自带的apk混淆器,它的用途有: 1.压缩apk包的大小,能删除无用的代码,并简化部分类名和方法名. 2.加大破解源码的难度,因为部分类名和方法名被重命名,使得程序逻 ...

  2. Android开发笔记(七十)反编译初步

    查看平台源码 查看内核源码 Android的内核源码很大,有几个G,仔细找找网上有许多下载的地方.作为普通开发者,一般不需要阅读内核源码,但一点都不了解好像也不行,因为实际开发中有时候就得会那么一点点 ...

  3. 运维开发笔记整理-前后端分离

    运维开发笔记整理-前后端分离 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么要进行前后端分离 1>.pc, app, pad多端适应 2>.SPA开发式的流 ...

  4. iOS开发笔记-两种单例模式的写法

    iOS开发笔记-两种单例模式的写法 单例模式是开发中最常用的写法之一,iOS的单例模式有两种官方写法,如下: 不使用GCD #import "ServiceManager.h"st ...

  5. 【Visual C++】游戏开发笔记十三 游戏输入消息处理(二) 鼠标消息处理

    本系列文章由zhmxy555编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7405479 作者:毛星云    邮箱: happyl ...

  6. 【Visual C++】游戏开发笔记二十七 Direct3D 11入门级知识介绍

    游戏开发笔记二十七 Direct3D 11入门级知识介绍 作者:毛星云    邮箱: happylifemxy@163.com    期待着与志同道合的朋友们相互交流 上一节里我们介绍了在迈入Dire ...

  7. Android移动APP开发笔记——最新版Cordova 5.3.1(PhoneGap)搭建开发环境

    引言 简单介绍一下Cordova的来历,Cordova的前身叫PhoneGap,自被Adobe收购后交由Apache管理,并将其核心功能开源改名为Cordova.它能让你使用HTML5轻松调用本地AP ...

  8. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  9. os-cocos2d游戏开发基础-进度条-开发笔记

     os-cocos2d游戏开发基础-进度条-开发笔记(十)   ios-cocos2d游戏开发基础-游戏音效-开发笔记(九)       ios-cocos2d游戏开发基础-CCLayer和Touch ...

  10. 【Visual C++】游戏开发笔记四十一 浅墨DirectX教程之九 为三维世界添彩:纹理映射技术(一)...

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhmxy555/article/details/8523341 作者:毛星云(浅墨 ...

最新文章

  1. 我的R之路:参数假设检验
  2. ios设置中性黑体_ios 解决自定义字体无法显示问题
  3. 哈希表添加哈希表(Hash Table,也叫散列表),是根据键(Key)而直接访问在内存存储位置的数据结构。typedef enum{ HASH_OK, -icoding
  4. spring体系结构_了解Spring Web应用程序体系结构:经典方法
  5. 51NOD 1088 最长回文子串1089 最长回文子串 V2(Manacher算法)
  6. 三大超算军团加速布局 中科曙光E级超算预研项目正式启动
  7. android imageview 上蒙版,在iOS中为UIImageView的圆形蒙版设置动画
  8. 从文档流角度理解浏览器页面渲染引擎对元素定位的解析
  9. 01-vue项目之滚动加载数据
  10. [转载] Python: ljust()|rjust()|center()字符串对齐
  11. 悄悄告诉你Java面试必备技能是什么?
  12. matlab绘制不同线性的直方图,matlab绘制直方图
  13. python3实例(一)平方根
  14. win7网络里计算机登录失败,Win7访问网上邻居提示“登陆失败”原因及解决方法...
  15. AWS EMR 上 Spark 任务 Container killed Exit code 137 错误
  16. gyb优化事项(4)
  17. 个人用 Qt + ffmpeg + D3D9/D3D11 开发的播放器
  18. navicat随手记
  19. COBOL 知识点集锦(下)
  20. KICAD设计——原理图层次及标签

热门文章

  1. 2022李宏毅第14讲---机器终身学习(Life Long Learning)
  2. oss图片无法在网站中显示
  3. Silverlight游戏设计(Game Design):(六)场景编辑器之开源畅想
  4. 服务器系统2008恢复,win2003升级为win2008、win2012保留数据重装恢复数据说明
  5. 大学python课本_Python大学实用教程(大学计算机规划教材)/数据工程师系列
  6. 进公司不会用 Git 拉项目!第二天被开除?
  7. LZY的CQU水下机器人视觉学习笔记(一)
  8. 如何用python把pdf转为word_如何使用python将双栏pdf转换成word?
  9. python实际应用2-拆分PDF
  10. 刷题笔记——青蛙跳台阶问题汇总