Skip to content

Instantly share code, notes, and snippets.

@Fenikkel
Last active July 9, 2025 07:58
Show Gist options
  • Select an option

  • Save Fenikkel/e269a853efd872297c6a3011d1c64eb9 to your computer and use it in GitHub Desktop.

Select an option

Save Fenikkel/e269a853efd872297c6a3011d1c64eb9 to your computer and use it in GitHub Desktop.

Revisions

  1. Fenikkel renamed this gist Mar 21, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion info.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Factory pattern for Unity's prefabs

    <p align="center">
    <img src="https://gist.github.com/user-attachments/assets/c0776d11-d8b2-4633-bcfd-f4eefca38fd7.webm" alt="Factory example">
    <img src="https://gist.github.com/user-attachments/assets/8541f9eb-1026-4778-8d49-d8f7b913eb97" alt="Factory example">
    </p>

    &nbsp;
  3. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    # Factory pattern for Unity's prefabs

    <p align="center">
    <img src="https://gist.github.com/user-attachments/assets/c0776d11-d8b2-4633-bcfd-f4eefca38fd7.webm" alt="Factory example">
    </p>

    &nbsp;
    ## Notes
    Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it.
  4. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -54,21 +54,18 @@ public class FactoryManager : MonoBehaviour
    {
    [SerializeField] FactoryA _FactoryA;
    [SerializeField] FactoryB _FactoryB;
    [SerializeField] FactoryC _FactoryC;
    // More factories...
    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.Q))
    {
    ProductA productA = (ProductA)_FactoryA.CreateProduct();
    }
    else if (Input.GetKeyDown(KeyCode.W))
    {
    PrefabProduct productB = _FactoryB.CreateProduct(Vector3.left * 2, Quaternion.identity);
    }
    else if (Input.GetKeyDown(KeyCode.E))

    if (Input.GetKeyDown(KeyCode.W))
    {
    _FactoryC.CreateProduct(Vector3.right * 2, Quaternion.identity);
    PrefabProduct productB = _FactoryB.CreateProduct(Vector3.up, Quaternion.identity);
    }
    }
    }
  5. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -80,3 +80,13 @@ public class FactoryManager : MonoBehaviour
    <img src="https://gist.github.com/user-attachments/assets/d9bcee6b-2a25-4a3a-9c85-b3424983753c" alt="Factory manager example">
    </p>

    &nbsp;
    ## Compatibility
    - Any Unity version
    - Any pipeline (Build-in, URP, HDRP, etc)

    &nbsp;
    ## Support
    ⭐ Star if you like it
    ❤️️ Follow me for more

  6. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -60,11 +60,11 @@ public class FactoryManager : MonoBehaviour
    {
    if (Input.GetKeyDown(KeyCode.Q))
    {
    _FactoryA.CreateProduct();
    ProductA productA = (ProductA)_FactoryA.CreateProduct();
    }
    else if (Input.GetKeyDown(KeyCode.W))
    {
    _FactoryB.CreateProduct(Vector3.left * 2, Quaternion.identity);
    PrefabProduct productB = _FactoryB.CreateProduct(Vector3.left * 2, Quaternion.identity);
    }
    else if (Input.GetKeyDown(KeyCode.E))
    {
    @@ -73,3 +73,10 @@ public class FactoryManager : MonoBehaviour
    }
    }
    ```

    7. Create a GameObject, attach your factory manager and assign your factory controllers:

    <p align="center">
    <img src="https://gist.github.com/user-attachments/assets/d9bcee6b-2a25-4a3a-9c85-b3424983753c" alt="Factory manager example">
    </p>

  7. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -46,3 +46,30 @@ public class FactoryA : PrefabFactory<ProductA>
    <img src="https://gist.github.com/user-attachments/assets/bf0de962-e187-4de5-8002-472c70632bb9" alt="Factory controller example">
    </p>

    5. Repeat previous steps for each product

    6. Create a manager to control all your factories:
    ```csharp
    public class FactoryManager : MonoBehaviour
    {
    [SerializeField] FactoryA _FactoryA;
    [SerializeField] FactoryB _FactoryB;
    [SerializeField] FactoryC _FactoryC;

    private void Update()
    {
    if (Input.GetKeyDown(KeyCode.Q))
    {
    _FactoryA.CreateProduct();
    }
    else if (Input.GetKeyDown(KeyCode.W))
    {
    _FactoryB.CreateProduct(Vector3.left * 2, Quaternion.identity);
    }
    else if (Input.GetKeyDown(KeyCode.E))
    {
    _FactoryC.CreateProduct(Vector3.right * 2, Quaternion.identity);
    }
    }
    }
    ```
  8. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion info.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,10 @@ public class FactoryA : PrefabFactory<ProductA>
    return newProduct;
    }
    }
    ```
    ```

    4. Create a GameObject, attach your factory controller and assign your prefab with the product controller to the variable `Product Prefab`:
    <p align="center">
    <img src="https://gist.github.com/user-attachments/assets/bf0de962-e187-4de5-8002-472c70632bb9" alt="Factory controller example">
    </p>

  9. Fenikkel revised this gist Mar 18, 2025. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -7,11 +7,12 @@ Simple tu use and foolproof. It uses abstraction to make it almost invisible on
    &nbsp;
    ## Usage

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

    {
    Debug.Log($"Unique initialization for <b>{this.GetType()}<b>");
    }
    @@ -20,8 +21,22 @@ Simple tu use and foolproof. It uses abstraction to make it almost invisible on
    }
    ```

    2. Create a prefab for the product:
    2. Create a prefab for the product and attach your product controller:
    <p align="center">
    <img src="https://gist.github.com/user-attachments/assets/9af55658-6d1a-48eb-95b9-419991df02b4" alt="Prefab example">
    </p>

    3. Inherid `PrefabFactory` with the desired type of product on your factory controller class:
    ```csharp
    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>");

    <img src="" alt="Prefab example">
    </p>
    return newProduct;
    }
    }
    ```
  10. Fenikkel created this gist Mar 18, 2025.
    18 changes: 18 additions & 0 deletions PrefabFactory.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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;
    }
    }
    17 changes: 17 additions & 0 deletions PrefabProduct.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    using UnityEngine;

    public abstract class PrefabProduct : MonoBehaviour
    {
    public abstract void Initialize();

    /*
    Add more mandatory methods:
    - Destroy
    - Select
    - Move
    - Damage
    ...
    (PrefabProduct works like an interface)
    */
    }
    27 changes: 27 additions & 0 deletions info.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    # Factory pattern for Unity's prefabs

    &nbsp;
    ## Notes
    Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it.

    &nbsp;
    ## Usage

    1. Inherid PrefabProduct on your prefab controller class:
    ```csharp
    public class PrefabControllerA : PrefabProduct
    {
    public override void Initialize()
    {
    Debug.Log($"Unique initialization for <b>{this.GetType()}<b>");
    }

    // Add prefab control functions here
    }
    ```

    2. Create a prefab for the product:
    <p align="center">

    <img src="" alt="Prefab example">
    </p>