Skip to content
yenmoc edited this page Feb 20, 2024 · 16 revisions

image

IFactory

Interface Contains a single method called Create to create an object of type T

ScriptableFactory

Abstract class inherite from ScriptableObject and interface IFactory

GameObjectFactory

Class inherite from abstract class ScriptableFactory. It contains a prefab property and implements the Create method by calling unity's Instantiate API

image

Pool

image

Interface IPool will contain 3 main methods

  • Prewarm will receive the pool size to be initialized and perform initialization first. if a pool is already initialized it will not initialize again
  • Request request to get an element from pool
  • Return perform element return to pool

Usages

Suppose you need to create pool for GameObject, create a ScriptableObject from GameObjectPool via menu Create > Pancake > Misc > GameObjectPool

It will require a ScriptableObject created from GameObjectFactory

image

You will create GameObjectFactory via menu Create > Pancake > Misc > GameObject Factory

image

Now you just need to fill in the prefab field of the object to which you want to apply the pool.

Name file GameObjectFactory, GameObjectPool you should match the object that will apply the pool to make it easy to find

  • ResetOn - Reset flag IsPrewarmed, clear pool container

    • SceneLoaded : when the scene is loaded by LoadSceneMode.Single
    • AdditiveSceneLoaded : when the scene is loaded by LoadSceneMode.Additive
    • ApplicationStarts : Once, when the application starts.
  • IsCacheSpawned : add object spawned by pool into managed list. You can return all spawned object by use method ReturnAllSpawned

  • RootIsUI : If RootIsUI is true the pool root object will be generated with RectTransform

Now you can you by declaring a property of type GameObejctPool

[SerializeField] private GameObjectPool coinFxPool;

Then

coinFxPool.SetParent(newParent, true); // change parent

...

var coinFx = coinFxPool.Request(); // get from pool

coinFxPool.Return(coinFx); // return to pool
Clone this wiki locally