{"id":13661745,"url":"https://github.com/KorStrix/Unity_GetComponentAttribute","last_synced_at":"2025-04-25T03:31:07.108Z","repository":{"id":102974956,"uuid":"183735209","full_name":"KorStrix/Unity_GetComponentAttribute","owner":"KorStrix","description":"Implemented Unity's GetComponent as an Attribute.","archived":false,"fork":false,"pushed_at":"2020-11-20T07:56:13.000Z","size":937,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-10T16:46:10.305Z","etag":null,"topics":["attribute","getcomponent","unity"],"latest_commit_sha":null,"homepage":"https://korstrix.github.io/project/getcomponentattribute/","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/KorStrix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2019-04-27T06:06:26.000Z","updated_at":"2023-09-19T14:54:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"86162d46-6ec6-4858-a877-a1ab89053023","html_url":"https://github.com/KorStrix/Unity_GetComponentAttribute","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KorStrix%2FUnity_GetComponentAttribute","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KorStrix%2FUnity_GetComponentAttribute/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KorStrix%2FUnity_GetComponentAttribute/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KorStrix%2FUnity_GetComponentAttribute/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KorStrix","download_url":"https://codeload.github.com/KorStrix/Unity_GetComponentAttribute/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250747730,"owners_count":21480700,"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","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":["attribute","getcomponent","unity"],"created_at":"2024-08-02T05:01:40.749Z","updated_at":"2025-04-25T03:31:07.088Z","avatar_url":"https://github.com/KorStrix.png","language":"C#","readme":"![UnityVersion badge](https://img.shields.io/badge/Unity-5.6%2B-red)\n![Platform badge](https://img.shields.io/badge/platform-Standalone%20%7C%20Android%20%7C%20IOS-green)\n[![License badge](https://img.shields.io/badge/license-MIT-blue)](https://github.com/KorStrix/Unity_GetComponentAttribute/blob/master/LICENSE)\n\n# 개요\n\n유니티에서 자주 사용되는 **GetComponent, GetComponentInParents, GetComponentInChildren 등을**\n\n속성으로 구현하여 **원하는 때에 해당 변수나 프로퍼티에 자동으로 할당** 할 수 있는 프로젝트입니다.\n\n![](https://github.com/KorStrix/Unity_GetComponentAttribute/blob/master/Images_ForGhithub/Preview.gif?raw=true)\n\n- **상기 이미지는 오딘 에셋을 사용한 예시입니다.**\n  - 오딘을 사용하지 않을 경우 Property, Dictionary 등이 인스펙터에 나타나지 않습니다.\n  - **(Property, Dictionary도 동작은 정상동작합니다. 인스펙터에 그려지지만 않을 뿐입니다.)**\n\n\u003cbr\u003e\n\n# 예시\n\n### Before Workflow\n```csharp\n// 기존 작업 방식은 public이나\npublic GameObject pLegacyWorkflow_Public_Inspector_Link;\n\n// (private || protected) \u0026\u0026 SerilizeField로 변경 후 인스펙터에 일일이 드래그 \u0026 드랍방식\n[SerializeField]\nprivate GameObject pLegacyWorkflow_Private_Inspector_Link;\n\n[SerializeField]\nprivate GameObject pLegacyWorkflow_Private_InScript;\n\npublic Rigidbody pLegacyWorkflow_Property { get; private set; }\n\n\nprivate void Awake()\n{\n  // 혹은 스크립트에 일일이 할당하는 로직\n  pLegacyWorkflow_Private_InScript = FindChildObject(\"Require Object Name\");\n  pLegacyWorkflow_Property = GetComponentInChildren\u003cRigidbody\u003e();\n}\n\nprivate GameObject FindChildObject(string strObjectName)\n{\n  Transform[] arrAllChildObject = GetComponentsInChildren\u003cTransform\u003e();\n}\n```\n\n### After Workflow\n\n```csharp\n[GetComponentInChildren(\"Somthing Require GameObject Name In Children\")]\nprivate GameObject pPrivate_Find_Name;\n\n[GetComponentInChildren]\npublic Rigidbody pProperty { get; private set; }\n\nvoid Awake()\n{\n  // 아래 코드 한줄로 모든 GetComponentAttribute의 필드 혹은 Property가 할당됩니다.\n  GetComponentAttributeSetter.DoUpdate_GetComponentAttribute(this);\n}\n```\n\n\u003cbr\u003e\n\n# 사용 주의사항\n- Awake시 다음과 같이 Manager의 함수를 호출해야 합니다.\n\n```csharp\nprivate void Awake()\n{\n    // 모노비헤비어를 상속받은 클래스에서 사용하고 싶을 때\n    GetComponentAttributeSetter.DoUpdate_GetComponentAttribute(this);\n    // 모노비헤비어를 상속받지 않은 클래스에서 사용하고 싶을 때\n    GetComponentAttributeSetter.DoUpdate_GetComponentAttribute(this, p_pNotInherit_Mono);\n}\n```\n\n- 다음과 같이 = null을 안할 경우 유니티 컴파일러가 변수를 할당하지 않았다는 경고를 출력합니다.\n\n```csharp\n[GetComponent]\nprivate Transform pTransform; // 컴파일러가 경고 출력\n\n[GetComponent]\nprivate Transform pTransform2 = null; // 컴파일러가 경고를 출력하지 않음\n```\n\n\u003cbr\u003e\n\n# 기능들\n\n### 1. 유니티에서 자주 사용하는 함수 GetComponent(s), GetComponenInParents, GetComponent(s)InChildren 등을 지원\n\n### 2. GetComponentInChildren Attribute\n- 하위 오브젝트에 **같은 타입의 오브젝트가 여러개 있을 경우, 이름으로 찾아서 할당하는 기능**\n\n```csharp\npublic enum ETestChildObject\n{\n  TestObject_Other_FindString,\n  TestObject_Other_FindEnum,\n}\n\n// Attribute 매개변수로 nameof연산자를 통해 string이 들어간 경우입니다.\n[GetComponentInChildren(nameof(ETestChildObject.TestObject_Other_FindString))]\nprivate Transform pChildComponent_FindString = null;\n\n// Attribute 매개변수로 Enum이 들어간 경우입니다.\n[GetComponentInChildren(ETestChildObject.TestObject_Other_FindEnum)]\nprivate Transform pChildComponent_FindEnum = null;\n```\n\n#### 2-1. Array, List, Dictionary 변수 자동 할당 지원 **(Array를 제외한 Collection의 경우 new를 할당해야 합니다.)**\n\nGameObject의 이름을 기반으로 찾습니다.\n\n```csharp\n[GetComponentInChildren]\npublic List\u003cTransform\u003e listTest = new List\u003cTransform\u003e();\n\n[GetComponentInChildren] // 인자에 Enum을 넣을 경우 오브젝트의 이름을 Enum으로 파싱하여 할당.\nprivate Dictionary\u003cETestChildObject, Transform\u003e mapTest_KeyIsEnum = new Dictionary\u003cETestChildObject, Transform\u003e();\n\n[GetComponentInChildren] // 인자에 string을 넣을 경우 오브젝트의 이름을 할당.\nprivate Dictionary\u003cstring, Transform\u003e mapTest_KeyIsString = new Dictionary\u003cstring, Transform\u003e();\n\n[SerializeField]\n[GetComponentInChildren] // Array의 경우 null을 대입해도 정상 동작\nTransform[] arrComponent = null;\n```\n\n#### 2-2. 중복된 이름의 오브젝트를 Collection로 담는것도 지원 (GetComponent, GetComponentInChidlren).\n- Array와 List만 지원합니다.\n\n```csharp\npublic enum ETestChildObject\n{\n  TestObject_Other_FindString,\n  TestObject_Other_FindEnum,\n}\n\n[GetComponent] // 해당 게임오브젝트에 같은 컴포넌트가 있는 경우 여러개가 담김\npublic List\u003cTransform\u003e listTest = new List\u003cTransform\u003e();\n\n[GetComponentInChildren] // 인자에 Enum을 넣을 경우 오브젝트의 이름을 Enum으로 파싱하여 Enum을 그룹으로 할당.\nprivate Dictionary\u003cETestChildObject, List\u003cTransform\u003e\u003e mapTest_KeyIsEnum = new Dictionary\u003cETestChildObject, List\u003cTransform\u003e\u003e();\n\n[GetComponentInChildren] // 인자에 string을 넣을 경우 오브젝트의 이름을 그룹으로 할당.\nprivate Dictionary\u003cstring, Transform\u003e mapTest_KeyIsString = new Dictionary\u003cstring, Transform\u003e();\n\n```\n\n### 3. 변수, 프로퍼티 구분없이 지원\n- **주의사항으로, set 한정자는 해당 클래스가 접근이 가능하게 해야 합니다.**\n\n```csharp\n[GetComponentInChildren]\npublic Transform pChildComponent_FindEnumProperty { get; private set; }\n```\n\n### 4. Monobehaviour를 상속받지 않은 클래스도 지원\n```csharp\npublic class GetComponentAttribute_Example : MonoBehaviour\n{\n  [System.Serializable] // 인스펙터 노출용\n  public class InnerClass_NotInherit_Mono\n  {\n      [GetComponent]\n      public Transform pTransform_Owner;\n\n      [GetComponentInChildren]\n      public Transform[] arrTransform_Children;\n  }\n\n  public InnerClass_NotInherit_Mono p_pNotInherit_Mono;\n\n  private void Awake()\n  {\n      // 모노비헤비어를 상속받지 않은 클래스에서 사용하고 싶을 때\n      GetComponentAttributeSetter.DoUpdate_GetComponentAttribute(this, p_pNotInherit_Mono);\n  }\n}\n```\n\n\u003cbr\u003e\n\n\n## 설치 및 사용 방법\n1번 혹은 2번 방법 중 하나만 선택하여 설치\n\n### 1. GetComponentAttribute.cs의 내용을 복사하여 설치할 프로젝트에 생성\n- 링크 https://github.com/KorStrix/Unity_GetComponentAttribute/blob/master/Runtime/GetComponentAttribute.cs\n\n### 2. Package로 받기 (유니티 2018버전 이상)\n- 설치할 유니티 프로젝트 - Packages - manifest.json 파일을 TextEditor로 열어 최하단에 쉼표 및 하단 내용 추가\n```\n\"com.korstrix.getcomponentattribute\":\"https://github.com/KorStrix/Unity_GetComponentAttribute.git\"\n```\n\n\u003cbr\u003e\n\n## 주의사항\n\nChildren 오브젝트가 너무 많을 경우 퍼포먼스 낭비가 심합니다.\n\n\u003cbr\u003e\n\n# 그 외\n\n### 참고한 프로젝트\n- [Unity3D 자동 GetComponent 블로그 링크](https://openlevel.postype.com/post/683269)\n\n### 연락처\n유니티 개발자 모임 카카오톡 \u0026 디스코드 링크입니다.\n\n- 카카오톡 : https://open.kakao.com/o/gOi17az\n- 디스코드 : https://discord.gg/9BYFEbG\n","funding_links":[],"categories":["C\\#"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKorStrix%2FUnity_GetComponentAttribute","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKorStrix%2FUnity_GetComponentAttribute","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKorStrix%2FUnity_GetComponentAttribute/lists"}