Skip to content

Instantly share code, notes, and snippets.

@Fenikkel
Last active March 21, 2025 10:01
Show Gist options
  • Save Fenikkel/e269a853efd872297c6a3011d1c64eb9 to your computer and use it in GitHub Desktop.
Save Fenikkel/e269a853efd872297c6a3011d1c64eb9 to your computer and use it in GitHub Desktop.
Factory pattern

Factory pattern for Unity's prefabs

Factory example

 

Notes

Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it.

 

Usage

  1. Inherid PrefabProduct on your product controller class:
    public class ProductA : PrefabProduct
    {
        public override void Initialize()

        {
            Debug.Log($"Unique initialization for <b>{this.GetType()}<b>");
        }
        
        // Add prefab control functions here
    }
  1. Create a prefab for the product and attach your product controller:

Prefab example

  1. Inherid PrefabFactory with the desired type of product on your factory controller class:
public class FactoryA : PrefabFactory<ProductA>
{
    public override PrefabProduct CreateProduct(Vector3 position = new Vector3(), Quaternion rotation = new Quaternion())
    {
        PrefabProduct newProduct = base.CreateProduct(position, rotation);

        Debug.Log($"Unique behavior for <b>{this.GetType()}</b> while creating <b>{newProduct.GetType()}</b>");

        return newProduct;
    }
}
  1. Create a GameObject, attach your factory controller and assign your prefab with the product controller to the variable Product Prefab:

Factory controller example

  1. Repeat previous steps for each product

  2. Create a manager to control all your factories:

public class FactoryManager : MonoBehaviour
{
    [SerializeField] FactoryA _FactoryA;
    [SerializeField] FactoryB _FactoryB;
    // More factories...

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            ProductA productA = (ProductA)_FactoryA.CreateProduct();
        }
        
        if (Input.GetKeyDown(KeyCode.W))
        {
            PrefabProduct productB = _FactoryB.CreateProduct(Vector3.up, Quaternion.identity);
        }
    }
}
  1. Create a GameObject, attach your factory manager and assign your factory controllers:

Factory manager example

 

Compatibility

  • Any Unity version
  • Any pipeline (Build-in, URP, HDRP, etc)

 

Support

⭐ Star if you like it
❤️️ Follow me for more

using UnityEngine;
public abstract class PrefabFactory<T> : MonoBehaviour where T : Component
{
[SerializeField]
protected T _ProductPrefab;
public virtual PrefabProduct CreateProduct(Vector3 position = new Vector3(), Quaternion rotation = new Quaternion())
{
GameObject instance = Instantiate(_ProductPrefab.gameObject, position, rotation);
PrefabProduct newProduct = instance.GetComponent<PrefabProduct>();
newProduct.Initialize();
return newProduct;
}
}
using UnityEngine;
public abstract class PrefabProduct : MonoBehaviour
{
public abstract void Initialize();
/*
Add more mandatory methods:
- Destroy
- Select
- Move
- Damage
...
(PrefabProduct works like an interface)
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment