Object pooling in Unity 2021+

While developing games, you may have noticed that there are about 100 instances of bullets per second.





  • A : reduce the number of bullets to 20





  • B : implement your own pooling system 





  • C : pay $ 50 for a pooling system in the Asset Store





  • D: Pooling API Unity, 2021





( )





.





, Pooling API, 2021 .





Unity 2021, , Unity.





?





?

: ?





, .





Unity , , , .





.





, , :





  • , .





  • , ( ). C#.





, :





  • ( new/dispose).





  • , , .





  • , .





, ?





( - , )





.





, () Unity - ?

, , ( ), , .





- , C# , , .





: , , C# . .





.





, .





, ?





, . , - , .





, , .





, .





.





— , () () , .





?





, (, ), , , , , .





?





:





  • , , , , .. ...





  • , , , .. ...





  • , : Take (), Return (), Reset ().





… , :





  • .





  • , , .





  • - , .





, . , .





, , ...





?

() :





  1. . , , , . , , : reset.





  2. , . , , , , .





  3. . . , - .





, , — , .





, . , .





: — .





, . .





.





.





Unity 2021:

Unity, :













  • UnityEngine.Pool







.





A)

— .





, :





  • (Create & dispose)





  • (Take)





  • (Return)





  • (Reset)





, :













  • /









? , ...





( ).





, -, , .





, . . .





B)

, :





  • The Unity Asset Store





  • Github









:





Pooling Toolkit

13Pixels Pooling

Pure Pool

Pro Pooling

  • RecyclerKit





  • Unity Object Pool





… .





.





:





  • .





  • , .





  • = . , .





  • ( ).





, , , .





, Unity API Unity 2021.





.





C) Pooling API Unity

2021 , Unity C#, .





Unity. , Unity.





— .





, . .





, Unity Pooling API , , , .





Object Pooling API Unity 2021

— , Unity 2021+.





( , ... , , )





, Unity Pooling API:





  •  









. .





( )





1.

, , — . , .





, , . Unity:













createFunc







, () => new GameObject(“Bullet”) or () => new Vector3(0,0,0)







actionOnGet







, , , .





actionOnRelease







, , , .





actionOnDestroy







, , ( ) .





collectionCheck







True, , Unity , , ( ).





defaultCapacity







: /, .





maxSize







: , . , .





GameObjects



:





_pool = new ObjectPool<GameObject>(createFunc: () => new GameObject("PooledObject"), actionOnGet: (obj) => obj.SetActive(true), actionOnRelease: (obj) => obj.SetActive(false), actionOnDestroy: (obj) => Destroy(obj), collectionChecks: false, defaultCapacity: 10, maxPoolSize: 10);
      
      



; .





, , GameObject



. , .





, _GameObject_’.





?





2.

, Unity , — _GameObject_’, , .





, createFunc



.





, GameObject



, Unity .





createFunc



.





GameObject



?





3.

, _pool



, Get



:





GameObject myGameObject = _pool.Get();
      
      



.





( ).





, , .





4.

, , . ?





: (destroy/dispose) . , .





? :





_pool.Return(myObject);
      
      



:





  1. actionOnRelease



    , , , .. ...





  2. , / MaxSize





  3. , .





  4. , , actionOnDestroy



    .





.





.





5.

, (dispose) , , .





, actionOnDestroy



, .





Destroy(myObject)



, , Unity.





, , , .





6.

— , . , , . , , :





_pool.Dispose();
      
      



. .









, Unity, .





Unity 2021+

LinkedPool ObjectPool

— , C# (95%+ , , , ).





— , .





LinkedPool



ObjectPool



, Unity , .





ObjectPool



Stack C#, C# :





private T[] _array;
      
      



, .





— 0 ( = 0) ( = 100000). , .





, . , .





: , maxCapacity



.





LinkedPool



, . :





internal class LinkedPoolItem { internal LinkedPool<T>.LinkedPoolItem poolNext; internal T value; }
      
      



LinkedPool



, , .





: . , , .





, Unity.





ListPool, DictionaryPool, HashSetPool, CollectionPool

C# Unity.





, , , , , - .





/ .





. , .





.





, , . , :





  • .





  • .





, , Unity, - .





, , , .





:





var manuallyReleasedPooledList = ListPool<Vector2>.Get();
manuallyReleasedPooledList.Add(Random.insideUnitCircle);
// Use your pool
// ...
ListPool<Vector2>.Release(manuallyReleasedPooledList);
      
      



, :





using (var pooledObject = ListPool<Vector2>.Get(out List<Vector2> automaticallyReleasedPooledList))
{
   automaticallyReleasedPooledList.Add(Random.insideUnitCircle);
   // Use your pool
   // ...
}
      
      



, using



, Unity .





CollectionPool



- ; , , , .





ListPool



, DictionaryPool



HashSetPool



- .





. , Unity . .





, : . , . .





, : GenericPool



UnsafeGenericPool



.





, , . - .





GenericPool UnsafeGenericPool

, ?





, GenericPool



UnsafeGenericPool



. , , .





, .





, ( ) .





var pooledGameObject = GenericPool<GameObject>.Get(); pooledGameObject.transform.position = Vector3.one; GenericPool<GameObject>.Release(pooledGameObject);
      
      



.





UnsafeGenericPool



: . , , , ( ). , , .





. , , ?





!





, . , .





:





GenericPool



ObjectPool



collectionCheck = true







UnsafeGenericPool



ObjectPool



collectionCheck = false







, , . .





( )

3 , , .





, , , .





, :





  1. . , , ( ).





  2. . , , . «». .





  3. , , . , . , - , , , .





  4. . , -, . . , 4, 1000+. . . , , .





  5. . , , .





.





, ?

— :





  • , ;





  • , ;





Unity 2021+ , -, , pooling API.





. , .





— , . , .






"Unity Game Developer. Professional". , : , .








All Articles