{"id":27125433,"url":"https://github.com/dubit/unity-hierarchy-behaviour","last_synced_at":"2026-03-13T08:35:23.033Z","repository":{"id":51144276,"uuid":"136184586","full_name":"dubit/unity-hierarchy-behaviour","owner":"dubit","description":"A quick way to create a MonoBehaviour as a child object that includes an Initialize method with or without arguments that are type-safe.","archived":false,"fork":false,"pushed_at":"2023-05-02T13:24:41.000Z","size":62,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"development","last_synced_at":"2025-04-07T14:53:12.617Z","etag":null,"topics":["behaviour","csharp","gameobject","hierarchy","monobehaviour","transform","unity"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dubit.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}},"created_at":"2018-06-05T13:50:43.000Z","updated_at":"2024-11-04T00:01:13.000Z","dependencies_parsed_at":"2022-09-09T20:31:51.775Z","dependency_job_id":null,"html_url":"https://github.com/dubit/unity-hierarchy-behaviour","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/dubit/unity-hierarchy-behaviour","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubit%2Funity-hierarchy-behaviour","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubit%2Funity-hierarchy-behaviour/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubit%2Funity-hierarchy-behaviour/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubit%2Funity-hierarchy-behaviour/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dubit","download_url":"https://codeload.github.com/dubit/unity-hierarchy-behaviour/tar.gz/refs/heads/development","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dubit%2Funity-hierarchy-behaviour/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30462462,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-13T06:34:02.089Z","status":"ssl_error","status_checked_at":"2026-03-13T06:33:49.182Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["behaviour","csharp","gameobject","hierarchy","monobehaviour","transform","unity"],"created_at":"2025-04-07T14:53:19.696Z","updated_at":"2026-03-13T08:35:23.013Z","avatar_url":"https://github.com/dubit.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unity-hierarchy-behaviour\n\n## What is it?\nIts a collection of GameObject extension methods to allow for runtime instantiation a MonoBehaviour that include an Initialize method that takes type-safe arguments.\n\n## What are the Core Features?\nThe ability to easily add child game objects with a specified `IHierarchyBehaviour` that can be initialize with type-safe args.\n\n## What are the benifits?\n * Control the flow of data\n * Divide responsibility of components\n * Visualize that responsibility\n * Control lifecycles\n\n## What are the requirements?\n * Unity 2018.x\n\n## How to use it\nHierarchyBehaviour is entirely run via `GameObject` Extension methods for creation and interfaces for implementation.\n\n```c#\npublic class MyClass : MonoBehaviour, IHierarchyBehaviour\n{\n    public void Initialize()\n    {\n        Debug.Log(\"Initialized\");\n    }\n}\n```\n```c#\npublic class MyClassWithArgs : MonoBehaviour, IHierarchyBehaviour\u003cCustomArgs\u003e\n{\n    public void Initialize(CustomArgs args)\n    {\n        Debug.Log(\"Initialized with \" + args);\n    }\n}\n```\n\nThe Initialize methods are automatically called by the GameObjectExtention methods used to create the new instance.\n- CreateChild (New, Resources, Loaded or Instantiated)\n- ReplaceChild (New, Resources, Loaded or Instantiated)\n\nHowever you can just add your class that implements `IHierarchyBehaviour` and choose to call Initialize when you prefer to.\n\nIn addition you can also implement multiple `IHierarchyBehaviour`'s, for example:\n```c#\npublic class LightEstimation : MonoBehaviour, IHierarchyBehaviour, IHierarchyBehaviour\u003cLight[]\u003e\n{\n    private Light[] lights;\n\n    public void Initialize()\n    {\n        lights = FindObjectsOfType\u003cLight\u003e();\n    }\n    \n    public void Initialize(params Light[] lights)\n    {\n        this.lights = lights;\n    }\n}\n```\nIn this case the class `LightEstimation` has the option to be initialized via  \n`gameObject.CreateChild\u003cLightEstimation\u003e();` in which it will call `Initalize()` with no args.  \n\nor we can do  \n```\ngameObject.CreateChild\u003cLightEstimation, Light[]\u003e(new[]\n{\n    directionalLight\n});\n```\nIf you already have reference to it you can simply do  \n```c#\nlightEstimation.Initialize();\n```\nor  \n```c#\nlightEstimation.Initalize(directionalLight, pointLight);\n```\n\n### CreateChild\n\nWith Name\n```C#\nvar myGameObject = gameObject.CreateChild(\"HelloWorld\");\n```\nWithout Name\n```C#\nvar myGameObject = gameObject.CreateChild();\n```\n\nWith Arguments\n```C#\nvar myClassWithArgs = gameObject.CreateChild\u003cMyClassWithArgs, CustomArgs\u003e(new CustomArgs(\"HelloWorld\"));\n```\nWithout Arguements\n```C#\nvar myClass = gameObject.CreateChild\u003cMyClass\u003e();\n```\n\nThis will create a child new GameObject and adds the component specified by the `TBehaviour` type parameter.\nThe type parameter must extend MonoBehaviour and implement `IHierarchyBehaviour` or `IHierarchyBehaviour\u003cTArgs\u003e`.\nThis will return the new instance of `TBehaviour`.\n\n### CreateChild from resources\nWith Arguements\n```C#\nvar myClassWithArgs = gameObject.CreateChild\u003cMyClassWithArgs, CustomArgs\u003e(\"MyResourcePath\", new CustomArgs(\"HelloWorld\"));\n```\nWithout Arguements\n```C#\nvar myClass = gameObject.CreateChild\u003cMyClass\u003e(\"MyResourcePath\");\n```\nGameObject\n```C#\nvar myGameObject = gameObject.CreateChild(path: \"MyResourcePath\");\n```\n\nThis will create a child new GameObject and adds the component specified by the `TBehaviour` type parameter.\nThe type parameter must extend MonoBehaviour and implement `IHierarchyBehaviour` or `IHierarchyBehaviour\u003cTArgs\u003e`.\nThis will return the new instance of `TBehaviour`.\n\n### CreateChild from loaded\nWith Arguements\n```C#\nvar myClassWithArgs = gameObject.CreateChild\u003cMyClassWithArgs\u003e(prefab, new CustomArgs(\"HelloWorld\"));\n```\nWithout Arguements\n```C#\nvar myClass = gameObject.CreateChild\u003cMyClass\u003e(prefab);\n```\nGameObject\n```C#\nvar myGameObject = gameObject.CreateChild(prefab.gameObject);\n```\n\nThis will take a pre-existing (loaded or instantiated) `IHierarchyBehaviour` and clone it.  \nThe type parameter must extend MonoBehaviour and implement `IHierarchyBehaviour` or `IHierarchyBehaviour\u003cTArgs\u003e`.  \nThis will return the new instance of `TBehaviour`.\n\n\n### ReplaceChild\nWith Arguements\n```C#\nvar myClassWithArgs = gameObject.ReplaceChild\u003cMyClassWithArgs, CustomArgs\u003e(toReplace, new CustomArgs(\"HelloWorld\"));\n```\nWithout Arguements\n```C#\nvar myClass = gameObject.ReplaceChild\u003cMyClass\u003e(toReplace);\n```\n\nYou can also use ReplaceChild with resourced assets and loaded assets.\n```C#\nvar myClassWithArgs = gameObject.ReplaceChild\u003cMyClassWithArgs\u003e(toReplace, \"MyResourcePath\");\n```\n```C#\nvar myClass = gameObject.ReplaceChild\u003cMyClass\u003e(toReplace, prefab);\n```\n\nThis will destroy the given child MonoBehaviour and create a child new GameObject and adds the component specified by the `TBehaviour` type parameter.  \nThe type parameter must extend MonoBehaviour and implement `IHierarchyBehaviour` or `IHierarchyBehaviour\u003cTArgs\u003e`.  \nThis will return the new instance of `TBehaviour`.  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdubit%2Funity-hierarchy-behaviour","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdubit%2Funity-hierarchy-behaviour","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdubit%2Funity-hierarchy-behaviour/lists"}