对象池是一种Unity常常用到的内存管理服务,它的做用在于能够减小建立每一个对象的系统开销。ide
在Unity游戏开发的过程当中常常会建立一些新的对象,若是数量较少还能够接受,若是建立的新对象数量庞大,那么对内存而言是一个极大的隐患。例如射击游戏当中,每发射一颗子弹,都要建立一个新的子弹对象,那么子弹是数量庞大,可想而知一场游戏当中会建立多少这样的新对象,那么若是这些子弹建立以后都对游戏起着关键且持续性的做用也无可厚非,问题是子弹发射完成以后,几秒以后就再也不拥有任何的意义,通常会将它自动的隐藏,也就是咱们所说的SetActive(false),所以大量的非活跃对象出如今游戏场景当中。性能
为了解决大量建立重复对象形成的内存损耗,咱们采用对象池的方式来解决。优化
下面用一个例子来演示 。this
用代码在plane上自动生成一堵方块墙,经过点击屏幕来发射子弹,击塌这堵墙。spa
生成墙以及发射子弹的代码 (挂在摄像机上):code
using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { public int row = 6; public Vector2 offset = new Vector2(); public GameObject cubPrefab; public GameObject bulletPrefab; private RaycastHit hit; public float speed=3; void Start () {
//生成墙 for (int i = 0; i < row; i++) { for (int j= 0; j < row; j++) { Instantiate(cubPrefab, new Vector3(i, j, 0)+new Vector3(offset.x,offset.y,0), Quaternion.identity); } } } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray,out hit)) { Vector3 dir = hit.point - Camera.main.transform.position; //从对象池中获取对象 GameObject bullet = ObjectPool.GetInstance().GetObj("Bullet"); bullet.transform.position = Camera.main.transform.position; bullet.GetComponent<Rigidbody>().velocity = dir.normalized * speed; } } } }
对象池代码:
(不须要挂载)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectPool { #region 单例 private static ObjectPool instance; private ObjectPool() { pool = new Dictionary<string, List<GameObject>>(); prefabs = new Dictionary<string, GameObject>(); } public static ObjectPool GetInstance() { if (instance==null) { instance = new ObjectPool(); } return instance; } #endregion /// <summary> /// 对象池 /// </summary> private Dictionary<string,List<GameObject>> pool; /// <summary> /// 预设体 /// </summary> private Dictionary<string, GameObject> prefabs; /// <summary> /// 从对象池中获取对象 /// </summary> /// <param name="objName"></param> /// <returns></returns> public GameObject GetObj(string objName) { //结果对象 GameObject result=null; //判断是否有该名字的对象池 if (pool.ContainsKey(objName)) { //对象池里有对象 if (pool[objName].Count>0) { //获取结果 result = pool[objName][0]; //激活对象 result.SetActive(true); //从池中移除该对象 pool[objName].Remove(result); //返回结果 return result; } } //若是没有该名字的对象池或者该名字对象池没有对象 GameObject prefab = null; //若是已经加载过该预设体 if (prefabs.ContainsKey(objName)) { prefab = prefabs[objName]; } else //若是没有加载过该预设体 { //加载预设体 prefab = Resources.Load<GameObject>("Prefabs/"+objName); //更新字典 prefabs.Add(objName, prefab); } //生成 result = UnityEngine.Object.Instantiate(prefab); //更名(去除 Clone) result.name = objName; //返回 return result; } /// <summary> /// 回收对象到对象池 /// </summary> /// <param name="objName"></param> public void RecycleObj(GameObject obj) { //设置为非激活 obj.SetActive(false); //判断是否有该对象的对象池 if (pool.ContainsKey(obj.name)) { //放置到该对象池 pool[obj.name].Add(obj); } else { //建立该类型的池子,并将对象放入 pool.Add(obj.name, new List<GameObject>() { obj }); } } void Start () { } // Update is called once per frame void Update () { } }
挂载在子弹上,自动回收到对象池的代码:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } /// <summary> /// 3秒后自动回收到对象池 /// </summary> /// <returns></returns> IEnumerator AutoRecycle() { yield return new WaitForSeconds(3f); ObjectPool.GetInstance().RecycleObj(gameObject); } private void OnEnable() { StartCoroutine(AutoRecycle()); } }
这样就减小了重复的生成和销毁子弹产生的内存消耗,优化了性能。