Unity-对象池

简介
解决某些对象频繁创建或销毁造成的时间资源消耗
实例

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test_ObjectPools{private static Test_ObjectPools _instances; public static Test_ObjectPools GetInstances() { if (_instances==null) { _instances = new Test_ObjectPools(); } return _instances; }//构造函数 Dictionary> pools; public Test_ObjectPools() { pools = new Dictionary>(); }public void PushIntoPool(GameObject obj,Transform parent) { obj.SetActive(false); obj.transform.SetParent(parent); int key = obj.GetInstanceID(); if (!pools.ContainsKey(key)) { pools.Add(key, new List()); } pools[key].Add(obj); }public GameObject PushOutPool(GameObject obj,Vector3 pos, Quaternion rot) { int key = obj.GetInstanceID(); if (pools.ContainsKey(key)) { if (pools[key].Count>0) { GameObject result = pools[key][0]; result.SetActive(true); pools[key].Remove(result); result.transform.position = pos; result.transform.rotation = rot; return result; } }GameObject res = GameObject.Instantiate(obj, pos, rot) as GameObject; res.name = obj.GetInstanceID().ToString(); return res; }}

参考
【Unity-对象池】https://www.jianshu.com/p/090d928e7bbb

    推荐阅读