unity出了自己的对象池 这里记录一下用法
-
命名空间就是这个
-
一般有两种用法,第一种是在using里面获取,脱离这个域就释放。第二种是在Get和Release配合使用
// This version will only be returned to the pool if we call Release on it.//只有使用Release才会释放var simplifiedPoints = ListPool<Vector2>.Get();
//这种只要脱离了Scope就会自动释放// Copy the points into a temp list so we can pass them into the Simplify method// When the pooled object leaves the scope it will be Disposed and returned to the pool automatically.// This version is ideal for working with temporary lists.using (var pooledObject = ListPool<Vector2>.Get(out List<Vector2> tempList)){for (int i = 0; i < points.Length; ++i){tempList.Add(points[i]);}LineUtility.Simplify(tempList, 1.5f, simplifiedPoints);}return simplifiedPoints;
- 一般比较常用的就几个集合类和下面这个,可以对自己的类进行一个池子回收。
using UnityEngine.Pool;public class GenericPoolExample
{class MyClass{public int someValue;public string someString;}void GetPooled(){// Get an instancevar instance = GenericPool<MyClass>.Get();// Return the instanceGenericPool<MyClass>.Release(instance);}
}
- 对于一些unity中的GameObject对象可以使用ObjectPool