又是新的一年啊!今天准备给大家讲解一下一个很常见的优化技术—使用对象池来优化项目
废话不多说了,这篇文章可以和我前面的博客中所讲的内存优化一起来看哦!
首先来解释一下什么叫“对象池”,我们都知道在unity中所有的游戏对象都可以统称为对象。那么顾名思义对象池就是能够存放很多对象的一个容器呗!
在我们实际开发项目中可能会遇到这样的一个问题,我们在不断的创建新的游戏对象,然后在不使用这个对象时,我们就要使用Destroy()方法来销毁这个没有用处的对象。很多新手都会采用这样的做法吧!想要一个对象就实例化或者new出来,不想要的时候就去销毁他们。殊不知这样的话,会很吃我们的内存的!严重的话,会影响我们开发产品的质量的。
那么对于这样的重复使用资源的问题,我们该怎么办呢?大家想一想啊,我们如果不删除这些对象,而是把他们的gameobject属性设置为false,那么我们这些对象不也看不见了吗?当我们需要的时候再把属性设置为true,然后让他们的位置等其他的属性设置一下不也可以解决问题嘛!正因为如此,对象池优化技术很好的帮助我们解决了这个问题。
下面以第一人称角色射击中子弹的使用和销毁来演示对象池技术的使用吧!
首先我们来创建一个对象池类:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class pool : MonoBehaviour
{
//声明一个单例对象
public static pool instance;
//创建一个字典dic
public static Dictionary<string, ArrayList> dic = new Dictionary<string, ArrayList>();
// Use this for initialization
void Start()
{
//单例的初始化
instance = this;
}
//提供一个取得物体的方法
public static GameObject Get(string key, Vector3 position, Quaternion rotation)
{
//创建返回值对象
GameObject go;
//定义dic的key名,因为instantiate出的gameobject都会自动命名为gameobject(Clone),这里是为了通下面return方法里给key的命名匹配
string GameObjectName = key + "(Clone)";
//如果字典里有gameobjectname这个key 并且key对应的数组不为空(有该种类子弹,且该种类子弹中有<已经创建过的>(未激活)的子弹gameobject)
if (dic.ContainsKey(GameObjectName) && dic[GameObjectName].Count > 0)
{
//从gameobjectname这个key位置取出数组
ArrayList list = dic[GameObjectName];
//取出一号位的子弹
//强制转换将object类型转换为gameobject类型;
go = (GameObject)list[0];
//从列表中去除这个子弹(拿出来用)
//取出之后然后移除列表中的元素,方便下次再次取元素。
list.RemoveAt(0);
//将子弹gameobject属性激活
go.SetActive(true);//使用时激活
//设置位置
go.transform.position = position;
//设置旋转
go.transform.rotation = rotation;
}
//如果没有是第一次(池子中没有对象,那么我们就实例化出来对象)
else
{
go = Instantiate(Resources.Load(key), position, rotation) as GameObject;
}
//返回创建的对象
return go;
}
//功能:实现将需要取消激活的对象取消激活。
public static GameObject Return(GameObject g)
{
string key = g.name;
//如果字典里有这个key
if (dic.ContainsKey(key))
{
dic[key].Add(g);
}
//如果没有这个key
else
{
//建立一个这个key的arraylist 并把g加进去
dic[key] = new ArrayList() { g };
}
//不销毁而是取消激活
g.SetActive(false);
//返回g
return g;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//这个脚本挂到任意一个geamobject上
public class CreatBullet : MonoBehaviour {
//拖入子弹预设体
public GameObject Bullet;
//拖入子弹开始位置,给一个空物体位置,然后拖个空物体进来
public Transform startPlace;
private float speed = 500;
void Update()
{
//获取空格键输入(发射键)
if (Input.GetKeyDown(KeyCode.Mouse0))
{
//调用pool的get方法创建一个子弹
GameObject bullet= pool.Get(Bullet.name, startPlace.position, Quaternion.identity);
//给子弹刚体一个力(发射)
//Rigidbody.AddForce 添加力
//AddForce 力仅应用于激活的刚体。如果游戏对象未激活,添加力无效果。
//ForceMode.Impulse:此种方式采用瞬间力作用方式。
bullet.GetComponent<Rigidbody>().AddForce(Vector3.forward* speed, ForceMode.Impulse);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyBullet : MonoBehaviour
{
//这个脚本挂到子弹上,让子弹在激活后n秒自动“销毁”(进入不激活状态)
//创建协同延时操作destroys();
void Update()
{
//放在update中一直去检测;
//执行延时操作
StartCoroutine(destroys());
}
IEnumerator destroys()
{
//延时n秒
yield return new WaitForSeconds(3f);
//调用取消激活方法取消激活自身
pool.Return(this.transform.gameObject);
}
}
好了,以上就是对象池代码的编写了,当然了,这种写法只是一种,不光可以用字典这种数据结构来编写,我们也可以使用list集合来写出对象池。写法还有很多种。个人认为这种写法比较简单和容易理解。而且字典用起来比较方便。写的不好的地方,大家多多包涵哦!
///对象池代码补更//
这是后面修改了一下的对象池代码,因为毕竟游戏里面的对象那么多嘛,那么就需要我们来将这些对象池管理起来了,方面取用和维护对象的声明周期。。。
//==============================================
// Copyright(C) 2019 by XXXXX
// All rights reserved.
// ProductName: XXXXXXX
// Author: LYZY
// E_mail: lyzygw@qq.com
// Version: 0.1
// Description: 1.基类对象池
// 2.提供可以用来创建具体的游戏对象的对象池方法
// 3. 提供池内回收对象方法
// 4.提供统一的接口销毁方法
// 5. 缺少对象回收时候的重置方法,需要在使用对象的时候手动重置对象状态信息
//==============================================
using System.Collections;
using UnityEngine;
public class BasePool
{
/// <summary>
/// 默认的对象池名字
/// </summary>
protected string m_PoolName = string.Empty;
/// <summary>
/// 如果不指定池的容量的话,则使用默认容量
/// </summary>
protected const int m_DefMaxCount = 50;
/// <summary>
/// 初始创建的对象个数
/// </summary>
private int m_InitCount = 50;
/// <summary>
/// 用来存储池中的对象
/// </summary>
protected Queue m_queue = null;
/// <summary>
/// 池中的最大存放数量
/// </summary>
protected int m_MaxCount = 0;
/// <summary>
/// 要创建的对象
/// </summary>
protected GameObject m_Obj = null;
/// <summary>
/// 对象池的根节点
/// </summary>
protected Transform m_PoolRoot = null;
public BasePool()
{
m_MaxCount = m_DefMaxCount;
m_queue = new Queue();
}
/// <summary>
/// 初始化方法
/// </summary>
/// <param name="poolName">池名字</param>
/// <param name="prefab">池中的预制物</param>
public virtual void Init(string poolName, GameObject prefab)
{
m_PoolName = poolName;
m_Obj = prefab;
//初始创建对象池 默认为50个,后面使用直接加载即可
if (m_Obj == null) return;
for (int i = 0; i < m_InitCount; i++)
{
if (m_Obj != null)
{
GameObject go = Object.Instantiate(m_Obj);
go.name = "AvgItem " + (i + 1);
go.transform.SetParent(m_PoolRoot);
go.transform.localScale = Vector3.one;
go.SetActive(false);
m_queue.Enqueue(go);
}
}
}
/// <summary>
/// 初始化池的基本内容
/// </summary>
/// <param name="poolName">池的名字</param>
/// <param name="trans">池的根节点位置</param>
/// <param name="maxCount">池的容量</param>
public void InitPool(string poolName, Transform trans, int maxCount = 0)
{
if (maxCount > 0) m_MaxCount = maxCount;
m_PoolName = poolName;
m_PoolRoot = trans;
}
/// <summary>
/// 创建池内的游戏对象
/// </summary>
/// <param name="parentTrans">对象的父节点</param>
/// <returns></returns>
public virtual GameObject CreatObj(Transform parentTrans)
{
GameObject go = null;
if (m_queue.Count > 0)
{
go = m_queue.Dequeue() as GameObject;
go.transform.SetParent(parentTrans);
go.SetActive(true);
return go;
}
else
{
if (m_Obj != null)
{
go = Object.Instantiate<GameObject>(m_Obj);
m_queue.Enqueue(go);
go.SetActive(false);
}
go = m_queue.Dequeue() as GameObject;
go.transform.SetParent(parentTrans);
go.SetActive(true);
return go;
}
}
/// <summary>
/// 回收一个对象
/// </summary>
/// <param name="go"></param>
public virtual void RecycleObj(GameObject go)
{
if (m_queue.Contains(go))
{
Debug.LogError("该对象已经被回收过了,请勿多次回收同一个对象~~~");
return;
}
GameObject obj = go;
if (m_queue.Count > m_MaxCount)
{
Debug.Log("Destroy Destroy Destroy ");
GameObject.Destroy(obj);
}
else
{
Debug.Log("RecycleObj RecycleObj RecycleObj ");
obj.SetActive(false);
go.transform.SetParent(m_PoolRoot);
m_queue.Enqueue(go);
}
}
/// <summary>
/// 清空池内容
/// </summary>
public virtual void ClearPool()
{
if (m_queue.Count > 0)
foreach (GameObject obj in m_queue)
GameObject.Destroy(obj);
m_queue.Clear();
}
}
//==============================================
// Copyright(C) 2019 by XXXXXX
// All rights reserved.
// ProductName: XXXXXXX
// Author: LYZY
// E_mail: lyzygw@qq.com
// Version: 0.1
// CreateTime: 2019/12/26 14:06:35
// Description: 1.对象池管理类
//==============================================
using System.Collections;
using System.Collections.Generic;
using com.elf.framework;
using UnityEngine;
public class PoolManager : Singleton<PoolManager>
{
/// <summary>
/// 游戏内中的游戏对象池管理器
/// </summary>
private Dictionary<string, BasePool> m_AllObjPoolDic = new Dictionary<string, BasePool>();
/// <summary>
/// 所有池的父对象位置节点
/// </summary>
public Transform m_Parent = null;
/// <summary>
/// 得到池对象
/// </summary>
/// <param name="poolName"></param>
/// <param name="poolMaxCount"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetPoolObj<T>(string poolName, int poolMaxCount) where T : BasePool, new()
{
if (m_AllObjPoolDic.ContainsKey(poolName))
{
return m_AllObjPoolDic[poolName] as T;
}
else
{
return CreatGameObjPool<T>(poolName, poolMaxCount);
}
}
/// <summary>
/// 创建池对象
/// </summary>
/// <param name="poolName"></param>
/// <param name="poolMaxCount"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private T CreatGameObjPool<T>(string poolName, int poolMaxCount) where T : BasePool, new()
{
GameObject poolRoot = new GameObject(poolName);
poolRoot.transform.SetParent(m_Parent);
T pool = new T();
pool.InitPool(poolName, poolRoot.transform, poolMaxCount);
m_AllObjPoolDic.Add(poolName, pool);
return pool;
}
/// <summary>
/// 销毁掉所有的对象池
/// </summary>
public void DestoryAllPoolObj()
{
if (m_AllObjPoolDic.Count > 0)
foreach (BasePool pool in m_AllObjPoolDic.Values)
pool.ClearPool();
m_AllObjPoolDic.Clear();
GameObject.Destroy(m_Parent);
}
}
//==============================================
// Copyright(C) 2019 by XXXXXX
// All rights reserved.
// ProductName: XXXXXXX
// Author: LYZY
// E_mail: lyzy@qq.com
// Version: 0.1
// Description: 测试脚本
//==============================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestAVGPool : MonoBehaviour
{
public string m_PoolName = null;
public GameObject m_ItemPrefab = null;
public GameObject m_UIPos = null;
public Transform poolManager = null;
private AVGPlayBackPool avgPlayBackPool = null;
private List<GameObject> _list = new List<GameObject>();
// Use this for initialization
void Start()
{
PoolManager.Instance.m_Parent = poolManager;
avgPlayBackPool = PoolManager.Instance.GetPoolObj<AVGPlayBackPool>(m_PoolName, 100);
avgPlayBackPool.Init(m_PoolName, m_ItemPrefab);
}
private void Update()
{
if (Input.GetKeyUp(KeyCode.Space))
{
GameObject go = avgPlayBackPool.CreatObj(m_UIPos.transform);
_list.Add(go);
Debug.Log("创建了一个物体~~~");
}
if (Input.GetKeyUp(KeyCode.C))
{
if (_list.Count > 0)
{
Debug.Log("物体的名字是:" + _list[_list.Count - 1].name);
avgPlayBackPool.RecycleObj(_list[_list.Count - 1]);
_list.RemoveAt(_list.Count - 1);
}
}
}
}