본문 바로가기

Unity/Knowledge

오브젝트 조작 및 컴퍼넌트 참조

Public class Csharp : MonoBehaviour

{

           public Test test;

           public Test[] testArr;

           public GameObject test; 

           public Transform tf;

 

void Awake [or] Start ()

{

           // 오브젝트 조작 - Find

           test = FindObjectOfType<Test>();                          // 오브젝트가 켜져있을 때,  자료형으로 검색 

           testArr = FindObjectsOfType<Test>();                   // 복수형 오브젝트

           test = GameObject.FindWithTag("Player");           // 태그로 검색

           test = GameObject.Find("Test");                             // 오브젝트 이름으로 검색

           test = GameObject.Find("Test/Test/Test");             // 경로 + 이름으로 검색  (상위 경로가 생략되어도 가능)

           tf = transform.Finde("Test/Test/Test");                   // (자식들 중) 경로 + 이름으로 검색

 

           // 오브젝트 조작 - Active

           bool isActive = test.activeInHierarchy;                    // 오브젝트가 켜져있는지 상태

           test.SetActive(true);                                                   // 오브젝트 켜고 끄기

           test.SetActive(!test.activeSelf);                                 // 오브젝트 켜고 끄기

           bool isEqaul = gameObject.CompareTag("Tag");    // 태그가 같은지 확인

           bool isEqual = gameObject.Equals(test);                 // 같은 오브젝트인지 확인

 


           // 오브젝트 조작 - Transform 

           transform.localPosition = Vector3.one;                     // 로컬좌표계 기준으로 (1,1,1) 이동

           transform.Parent = test.transfrom;                             // 오브젝트의 위치 이동 (부모 변경)

           transform.SetParaent(test.transform);                       // 오브젝트의 위치 이동 (부모 변경)

           transform.SetAsFirstSibling();                                    // 형제들 중 제일 위로 이동

           transform.SetAsLastSibling();                                    // 형제들 중 제일 아래로 이동

           transform.SetSiblingIndex(1);                                    // 형제들 중 지정한 인덱스로 이동

           int index = transform.GetSiblingIndex();                   // 형제들 중 순서를 int로 반환

           Transform childTf = transform.GetChild(3);              // 자식들 중 인덱스 오브젝트의 Transform 반환

           ChildTf.SetSiblingIndex(UnityEngine.Random.Range(0, child));      // 자식들 중 인덱스 오브젝트의 랜덤

           transform.Translate(Vector3.one * 10);                      // 이동 = 현재 위치 + 매개변수 vector

           transform.localRotation = Quaternion.identity;        // 로컬 좌표계 초기값으로 초기화

           transform.localRotation = Quaternion.Euler(-90, 0, 0);       // 부모의 회전 상태 기준으로 -90도 회전

           transform.LookAt(Input.mousePosition);                  // 방향

           transform.Rotate(90, 0 ,0);                                           // 각도 변경 (Inspector랑 동일)

         

           // 스크립트 연결 - Component

           gameObject.AddComponent<Test>();

           test = GetComponent<Test>();

           tests = GetComponents<BoxCollider2D>();

           test = GetCompnonetInChildren<Test>();                 // 자기 자신 + 자식

           testArr = GetCompnonetsInChildren<Test>();          // 자기 자신 + 자식들

           test = GetCompnonetInParent<Test>();                     // 자기 자신 + 부모

           test = GetCompnonetsInParent<Test>();                   // 자기 자신 + 부모


           // 오브젝트와 프리팹 생성 [ 직접 생성은 잘 안씀 ]

           GameObject obj1 = new GameObject();

           GameObject obj2 = new GameObject("Object1");

           GameObject obj3 = new GameObject("Object2, typeof(Rigidbody2D), typeof(BoxCollider2D))");

           GameObject obj4 = new GameObject("Object3, typeof(Test), typeof(SpriteRenderer))");

 

           obj1.transform.SetParent(transform);

           obj2.transform.SetParent(transform);

           obj3.transform.SetParent(transform);

           obj4.transform.SetParent(transform);

 

           Instantiate(testPrefab);                                                                       // 오브젝트 생성

           Instantiate(testPrefab, transform);                                                    // 오브젝트 생성, 부모지정

           Instantiate(testPrefab, transform.position, Quaternion.identity);  // 오브젝트 생성, 위치 지정, rotation

           Instantiate(testPrefab, transform.position, Quaternion.identity, transform); 

          // 오브젝트 생성, 위치 지정, rotation 지정, 부모지정


           Test arrow = Instantiate(testPrefab, transform);                                // 오브젝트 값을 변경해야 하는 경우

           arrow.spr = null;

           arrow.name = "basic";    

           arrow.AddData = (null, "basic");  

}