FactorableBehaviour

Abstract MonoBehaviour base class for scene objects managed by a ScriptableFactory. Implements IFactorable with full GameObject lifecycle management — deactivation on instantiate, re-parenting on spawn/despawn, and automatic scene transfer. Subclass this for any pooled object that needs a Transform, components, and scene presence.

Definition

Namespace: Paragon.Core.ScriptableFactory Assembly: Paragon.dll

public abstract class FactorableBehaviour : ParagonBehaviour, IFactorable

Inherits: ParagonBehaviourSerializedMonoBehaviour Implements: IFactorable

Generic Variant

public abstract class FactorableBehaviour<TData> : FactorableBehaviour, IFactorable<TData>
    where TData : FactorableData

Implements additionally: IFactorable<TData>

Remarks

FactorableBehaviour uses explicit interface implementation for IFactorable methods, meaning the lifecycle hooks (OnInstantiate, OnSpawn, OnDespawn, OnDestroy) are only callable through the IFactorable interface (i.e., by the factory). Subclasses override the protected virtual counterparts instead.

GameObject Management

Phase

gameObject.activeSelf

transform.parent

Scene

After Instantiate

false

Factory transform

Factory's scene

After Spawn

true

null (root)

Active scene (via MoveToActiveScene())

After Despawn

false

Factory transform

Factory's scene

The onDestroy flag prevents re-parenting during Unity's OnDestroy() callback (when the factory itself may be destroyed).

Quick Lookup

Goal
How

Create a pooled scene object

Subclass FactorableBehaviour, override OnSpawn()

Add data initialization

Subclass FactorableBehaviour<TData>, override InitializeData(TData)

Return to pool from self

Call Despawn()

Permanently destroy

Call Destroy()

Check if active

Read IsSpawned

Setup on first creation

Override OnInstantiate()

Cleanup on despawn

Override OnDespawn()

Properties

IsSpawned

Returns true while the object is spawned and active.

Methods

Despawn

Returns this object to its owning factory's pool. No-op if the object has no factory reference.

Destroy

Permanently destroys the underlying GameObject.

Extension Points

Optional Overrides

Method
Called When
Purpose

OnInstantiate()

Factory creates instance (once)

One-time setup, cache component references

OnSpawn()

Object activated from pool

Initialize per-spawn state, enable systems

OnDespawn()

Object returned to pool

Reset state, disable systems, release references

OnDestroy()

Unity's OnDestroy callback

Final cleanup (auto-despawns if still spawned)

Required Overrides (Generic Variant)

Method
Purpose

InitializeData(TData data)

Configure object from data record (abstract in FactorableBehaviour<TData>)

Implementation Requirements

When subclassing FactorableBehaviour, you MUST:

  1. Mark the class as a MonoBehaviour component (inheriting from FactorableBehaviour handles this)

  2. Keep the class concrete (non-abstract) if it will be used as a prefab component

When subclassing FactorableBehaviour<TData>, you MUST additionally:

  1. Override InitializeData(TData data) (abstract)

  2. Apply data configuration before the object is used (called by factory before spawn)

You SHOULD:

  • Cache component references in OnInstantiate() (runs once per instance, not per spawn)

  • Reset all per-spawn state in OnDespawn() to ensure clean re-use from pool

  • Avoid calling base.OnDestroy() unless you understand the auto-despawn behavior

You SHOULD NOT:

  • Call IFactorable.OnSpawn() or IFactorable.OnDespawn() directly — these are for the factory

  • Assume OnDestroy() runs before OnDespawn() — the base class auto-despawns first

  • Instantiate FactorableBehaviour objects with Object.Instantiate() — always use the factory

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Basic Pooled Object

Data-Driven Object

See Also

Last updated