GameObjectPool
来源:互联网 发布:疯狂淘宝李涛收费 编辑:程序博客网 时间:2024/06/02 15:02
using UnityEngine;using System;using System.Collections;/** * 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). * * Converted JScript version to C#, original here: * http://vonlehecreative.com/video-games/unity-resource-gameobjectpool/ * * C# version by Bart Wttewaall, www.Mediamonkey.nl */public class GameObjectPool{ private GameObject _prefab; private Stack available; private ArrayList all; private Action<GameObject> initAction; private bool setActiveRecursively; // ---- getters & setters ---- #region getters & setters // returns the prefab being used by the pool. public GameObject prefab { get { return _prefab; } } // returns the number of active objects. public int numActive { get { return all.Count - available.Count; } } // returns the number of available objects. public int numAvailable { get { return available.Count; } } #endregion // ---- constructor ---- #region constructor public GameObjectPool(GameObject prefab, uint initialCapacity, Action<GameObject> initAction, bool setActiveRecursively) { this._prefab = prefab; this.initAction = initAction; this.setActiveRecursively = setActiveRecursively; available = (initialCapacity > 0) ? new Stack((int)initialCapacity) : new Stack(); all = (initialCapacity > 0) ? new ArrayList((int)initialCapacity) : new ArrayList(); } #endregion // ---- public methods ---- #region public methods public GameObject Spawn(Vector3 position, Quaternion rotation) { GameObject result; if (available.Count == 0) { // create an object and initialize it. result = GameObject.Instantiate(prefab, position, rotation) as GameObject; // run optional initialization method on the object if (initAction != null) initAction(result); all.Add(result); } else { result = available.Pop() as GameObject; // get the result's transform and reuse for efficiency. // calling gameObject.transform is expensive. Transform resultTrans = result.transform; resultTrans.position = position; resultTrans.rotation = rotation; result.SetActive(true); } return result; } public bool Destroy(GameObject target) { if (!available.Contains(target)) { available.Push(target); SetActive(target, false); return true; } return false; } // Unspawns all the game objects created by the pool. public void DestroyAll() { for (int i = 0; i < all.Count; i++) { GameObject target = all[i] as GameObject; if (target.activeSelf) Destroy(target); } } // Unspawns all the game objects and clears the pool. public void Clear() { DestroyAll(); available.Clear(); all.Clear(); } // Applies the provided function to some or all of the pool's game objects. public void ForEach(Action<GameObject> action, bool activeOnly) { for (int i = 0; i < all.Count; i++) { GameObject target = all[i] as GameObject; if (!activeOnly || target.activeSelf) action(target); } } #endregion // ---- protected methods ---- #region protected methods // Activates or deactivates the provided game object using the method // specified by the setActiveRecursively flag. protected void SetActive(GameObject target, bool value) { if (setActiveRecursively) { target.SetActive(value); } else { target.SetActive(value); } } #endregion}原地址点击这里
0 0
- GameObjectPool
- Unity ----- 对象池GameObjectPool
- Unity3D-对象池GameObjectPool
- 对象池GameObjectPool
- url-pattern详解
- java 并发编程学习笔记之volatile意外问题的正确分析解答
- 引领技术潮流的传智播客广州.Net培训课程
- jQuery事件之鼠标事件(转)
- 剖析安卓软件的安装位置
- GameObjectPool
- 非root帐号不能打开1024以下的端口号,切忌!!!
- winform拖拽:实现外部文件拖拽到程序内以及程序内部间的拖拽
- 线上应用故障排查
- OSG 如何像HOOPS一样 设置前后左右上下视图
- 转向ARC的说明
- c#颠倒字符串序列
- android.content.res.Resources$NotFoundException: String resource ID #0x1
- dbca创建数据库