Wanzyee Studio
PrefabPool

Maintain the instances created from the prefabs, for reusing in the current scene. More...

Static Public Member Functions

static void Allocate< T > (T prefab, int minTotal=0, int minSpare=0, int maxTotal=int.MaxValue, int maxSpare=int.MaxValue)
 Adjust the count of spare instances created from the prefab. More...
 
static void Allocate (GameObject prefab, int minTotal, int minSpare, int maxTotal, int maxSpare)
 Adjust the count of spare instances created from the prefab. More...
 
static T Require< T > (T prefab, Transform parent=null, Vector3 position=default, Vector3 rotation=default, bool active=true)
 Get an instance created from the prefab. More...
 
static GameObject Require (GameObject prefab, Transform parent, Vector3 position, Vector3 rotation, bool active)
 Get an instance created from the prefab. More...
 
static bool Release< T > (T instance, bool destroy=false)
 Put the instance back to the pool. More...
 
static bool Release (GameObject instance, bool destroy)
 Put the instance back to the pool. More...
 

Detailed Description

Maintain the instances created from the prefabs, for reusing in the current scene.

About resetting instances:

  1. It's hard to determine if any of an instance's component states has been changed.
  2. This'll clean up any new component attached after required out from the pool.
  3. You may use dynamic component, OnDisable(), or IPoolee, to reset dynamic content.
  4. By the way, it's recommended not to use complex structure prefabs.

The instance life cycle from Require() to Release():

  1. Move out from the pool hierarchy.
  2. Invoke IPoolee.OnRequireFromPool() of all components.
  3. Activate the UnityEngine.GameObject if need.
  4. Using phase.
  5. Deactivate the UnityEngine.GameObject.
  6. Invoke IPoolee.OnReleaseIntoPool() of all components.
  7. Clean up dynamic components.
  8. Move into the pool hierarchy.

Member Function Documentation

static void Allocate< T > ( prefab,
int  minTotal = 0,
int  minSpare = 0,
int  maxTotal = int.MaxValue,
int  maxSpare = int.MaxValue 
)
static

Adjust the count of spare instances created from the prefab.

Template Parameters
TThe component or interface type.
Parameters
prefabPrefab.
minTotalMinimum total count.
minSpareMinimum spare count.
maxTotalMaximum total count.
maxSpareMaximum spare count.
Type Constraints
T :class 
static void Allocate ( GameObject  prefab,
int  minTotal,
int  minSpare,
int  maxTotal,
int  maxSpare 
)
static

Adjust the count of spare instances created from the prefab.

Parameters
prefabPrefab.
minTotalMinimum total count.
minSpareMinimum spare count.
maxTotalMaximum total count.
maxSpareMaximum spare count.
static T Require< T > ( prefab,
Transform  parent = null,
Vector3  position = default,
Vector3  rotation = default,
bool  active = true 
)
static

Get an instance created from the prefab.

Template Parameters
TThe component or interface type.
Parameters
prefabPrefab.
parentHierarchy parent.
positionLocal position.
rotationLocal rotation.
activeIf to set the instance active.
Returns
The instance.
Type Constraints
T :class 
static GameObject Require ( GameObject  prefab,
Transform  parent,
Vector3  position,
Vector3  rotation,
bool  active 
)
static

Get an instance created from the prefab.

Parameters
prefabPrefab.
parentHierarchy parent.
positionLocal position.
rotationLocal rotation.
activeIf to set the instance active.
Returns
The instance.
static bool Release< T > ( instance,
bool  destroy = false 
)
static

Put the instance back to the pool.

Template Parameters
TThe component or interface type.
Parameters
instanceInstance.
destroyIf to destroy the instance if isn't from the pool.
Returns
true if the instance is from the pool and released.
Type Constraints
T :class 
static bool Release ( GameObject  instance,
bool  destroy 
)
static

Put the instance back to the pool.

Parameters
instanceInstance.
destroyIf to destroy the instance if isn't from the pool.
Returns
true if the instance is from the pool and released.