Last active
July 9, 2025 07:58
-
-
Save Fenikkel/e269a853efd872297c6a3011d1c64eb9 to your computer and use it in GitHub Desktop.
Revisions
-
Fenikkel renamed this gist
Mar 21, 2025 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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/8541f9eb-1026-4778-8d49-d8f7b913eb97" alt="Factory example"> </p> -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> ## Notes Simple tu use and foolproof. It uses abstraction to make it almost invisible on your code and easy to reuse it. -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 4 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,21 +54,18 @@ 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); } } } -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> ## Compatibility - Any Unity version - Any pipeline (Build-in, URP, HDRP, etc) ## Support ⭐ Star if you like it ❤️️ Follow me for more -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 9 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -60,11 +60,11 @@ public class FactoryManager : MonoBehaviour { 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)) { @@ -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> -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 27 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); } } } ``` -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> -
Fenikkel revised this gist
Mar 18, 2025 . 1 changed file with 20 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ## Usage 1. Inherid `PrefabProduct` on your product controller class: ```csharp 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 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>"); return newProduct; } } ``` -
Fenikkel created this gist
Mar 18, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) */ } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ # Factory pattern for Unity's prefabs ## 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 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>