{"id":31791241,"url":"https://github.com/mfdeveloper/unitypatterns","last_synced_at":"2025-10-10T16:53:07.167Z","repository":{"id":49445406,"uuid":"481661541","full_name":"mfdeveloper/UnityPatterns","owner":"mfdeveloper","description":"Unity Design Patterns UPM package (e.g Singleton, Strategy, Facade...)","archived":false,"fork":false,"pushed_at":"2024-02-15T14:44:24.000Z","size":262,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-02-15T21:29:54.230Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mfdeveloper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-04-14T15:47:48.000Z","updated_at":"2022-06-14T15:44:43.000Z","dependencies_parsed_at":"2024-02-14T21:29:46.068Z","dependency_job_id":"1a8ea55e-6fb7-4b37-a75c-81a56129909a","html_url":"https://github.com/mfdeveloper/UnityPatterns","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/mfdeveloper/UnityPatterns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfdeveloper%2FUnityPatterns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfdeveloper%2FUnityPatterns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfdeveloper%2FUnityPatterns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfdeveloper%2FUnityPatterns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mfdeveloper","download_url":"https://codeload.github.com/mfdeveloper/UnityPatterns/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mfdeveloper%2FUnityPatterns/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004684,"owners_count":26083751,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-10T16:52:34.647Z","updated_at":"2025-10-10T16:53:07.160Z","avatar_url":"https://github.com/mfdeveloper.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unity Patterns\n\n**Unity version:** `2021.3.7f1`\n\nUnity Design Patterns implementations, to shared across projects as an [UPM](https://docs.unity3d.com/Manual/cus-layout.html) package. Below you can see which patterns are implemented until here\n\n## Unity: Singleton\n\nUnity Singleton Monobehaviour component, that can be attached to Game Objects. You can use `SingletonPersistent` to persist the instance among scenes\nor just `Singleton` class to use the same instance on the only one scene.\n\n### Main use cases\n\n- Managers that should use the same instance among scripts (e.g GameManager, ScoreManager, InputManager...)\n\n- When you need use any component that depends of a Game object in the scene (e.g access `AudioSource` inside of an singleton)\n\n- When you need a Singleton with Unity messages like `Start()`, and/or access some objects that are only available after the game starts\n  \u003e (e.g Audio Middlewares, Third Party packages/libraries...)\n\n\u003e Consider use a [ScriptableObject](https://docs.unity3d.com/Manual/class-ScriptableObject.html) instead\n\n### Getting started\n\nCreate a script that inherits from `Singleton\u003cMyScript\u003e` passing the script class by generics:\n\n```csharp\npublic class GameManager : Singleton\u003cGameManager\u003e {\n  ...\n}\n```\n\nIn any other script, access the `Instance` property. The value of this property should be equal from any script:\n\n```csharp\npublic class PlayerController : Monobehaviour {\n  \n  private void Awake() {\n      // Get the singleton instance\n      var gameManager = GameManager.Instance;\n  }\n}\n```\n\n### Persistent Singleton\n\nIf you wish a singleton that persists among scenes, you can create a class that inherit from `SingletonPersistent`:\n\n```csharp\npublic class GameManager : SingletonPersistent\u003cGameManager\u003e {\n  ...\n}\n```\n\nBy default, when a new scene is loaded and there is the same game object with the same script component (e.g `GameManager` above), the previous instance from the previous scene will be destroyed and will remains just one instance under **`DontDestroyOnload`** Unity scene.\n\n### Persistent Singleton: Optional settings\n\nOptionally, it's possible pass custom configurations to a `SingletonPersistent` script with `SingletonSettings` **_C#_** attribute:\n\n```csharp\n// Here if the same gameObject with the same script \n// exists in another scene, the next one will be\n// destroyed and the GameObject reference fields\n// will be copied to the previous \n[SingletonSettings(CopyFieldsValues = true, DestroyGameObject = PersistentDestroyOrder.NEXT)]\npublic class GameManager : SingletonPersistent\u003cGameManager\u003e {\n  ...\n}\n```\n\n\u003e For more details, see `Tests/Runtime/Examples` scripts examples\n\n## Unity: Factory Method\n\nA base Factory Method implementation for Unity. The main use case here is to use this to access a gameObject in the scene that contains a script that implements an `C#` interface:\n\n```csharp\n\n// Create a C# interface\npublic interface IMyComponent\n{\n\n}\n\n// Create a MonoBehaviour script that implements the interface above, and attach it to a gameObject in the scene\npublic class MyScript : MonoBehaviour, IMyComponent\n{\n\n}\n\n// Example to access the a gameObject script that implements an interface\nusing System.Linq;\nusing UnityEngine;\nusing UnityPatterns;\n\npublic class ExampleScript : MonoBehaviour\n{\n    public GameObject[] rootsFromDontDestroyOnLoad;\n    void Start()\n    {\n        IMyComponent myComponent = FactoryComponent.Get\u003cIMyComponent\u003e();\n\n        // (Optional) You can get the all gameObjects with a script that implements an interface\n        List\u003cIMyComponent\u003e myComponent = FactoryComponent.GetList\u003cIMyComponent\u003e();\n\n        Debug.Log($\"The component is: {myComponent.GetType().Name}\") // Prints: MyScript\n    }\n}\n```\n\nAlso, it's possible get a `ScriptableObject` instance from an interface or a class:\n\n```csharp\n// Create a C# interface\npublic interface IMyScriptable\n{\n\n}\n\n// Create a MonoBehaviour script that implements the interface above, and attach it to a gameObject in the scene\n[CreateAssetMenu(fileName = \"MyScriptable\", menuName = \"Data/Samples/MyScriptable\")]\npublic class MyScriptable : ScriptableObject, IMyScriptable\n{\n\n}\n\n// Example to access the a gameObject script that implements an interface\nusing System.Linq;\nusing UnityEngine;\nusing UnityPatterns;\n\npublic class ExampleScript : MonoBehaviour\n{\n    public GameObject[] rootsFromDontDestroyOnLoad;\n    void Start()\n    {\n        // Get a ScriptableObject instance from an interface\n        IMyScriptable myScriptable = FactoryComponent.Get\u003cIMyScriptable\u003e();\n\n        // Get a ScriptableObject instance from a class\n        MyScriptable myScriptable = FactoryComponent.Get\u003cMyScriptable\u003e();\n\n\n        Debug.Log($\"The component is: {myScriptable.GetType().Name}\") // Prints: MyScriptable\n    }\n}\n```\n\n### Main use cases\n\n- Get singleton managers from all scenes active scenes (including `DontDestroyOnLoad` automatic scene created by Unity).\n\n- Get any **gameObject** from scenes that contains a script that implements an `C#` interface.\n\n- Get a `ScriptableObject` that implements an `C#` interface or a from class reference. The last one is great to get an instance that automatically call `Init()` method.\n\n## Code templates\n\nUnder `Samples~` folder, this package share some code templates to easily create singleton classes from Unity Editor.\n\nTo use that, follow the steps below:\n\n1. On Unity Editor, click on `Window` =\u003e `Package Manager`\n2. On the opened window, find the package `Unity Design Patterns...` and import the sample: **ScriptTemplates**\n3. Move the imported folder `ScriptTemplates` to your root `Assets` folder in your Unity game project.\n4. Restart the Editor\n5. Openup again, and press right click on any folder of your game project, and check if appears: `Create` =\u003e `Custom Templates` =\u003e `UnitySingleton` :)\n\n## References\n\n- [mstevenson/MonoBehaviourSingleton.cs](https://gist.github.com/mstevenson/4325117)\n\n\u003e The implementation here was based in this gist above!!\n\n- [Design Pattern: Singletons in Unity](https://www.youtube.com/watch?v=Ova7l0UB26U)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfdeveloper%2Funitypatterns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmfdeveloper%2Funitypatterns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmfdeveloper%2Funitypatterns/lists"}