开发过程中会经常遇到频繁大量的生成某种物体(如常见的怪物或者子弹之类的)这种情况下为了节省资源占用建议使用对象池。
原理是:通过循环使用对象, 减少资源在初始化和释放时的性能损耗。
思路是: 在需要时,从池中提取,不用时,放回池中。
//一版
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**对象池*/
public class TerrainPool
{
//单例
private static TerrainPool instance;
public static TerrainPool GetInstance()
{
if (instance == null)
{
instance = new TerrainPool();
}
return instance;
}
//对象池
Dictionary<string, List<GameObject>> _terrainPool = new Dictionary<string, List<GameObject>>();
/// <summary>
/// 从对象池中获取对象
/// </summary>
/// <param name="objName"></param>
/// <returns></returns>
public GameObjectGetTerrain(string objName)
{