Awake는 실례화되고 바로 불려가는 물건 아닌가...?
5508 단어 Unity
오프닝
결론
환경
실험 스크립트
AwakeTest.csusing UnityEngine;
public class AwakeTest : MonoBehaviour
{
public string objectName = null;
void Awake()
{
Debug.Log("Awake!" + objectName);
}
}
objectName 은 어떤 객체인지 확인하는 데 사용됩니다.
행위 확인
0. 전제 사항
이러한 느낌으로 위의 스크립트를 GameObject/Cube/Sphere에 추가하여 ObjectName에서 직접 개체 이름을 설정합니다.
GameObject/Cube/Sphere의 active 상태를 전환하는 실험입니다.
1. 처음부터 Active
using UnityEngine;
public class AwakeTest : MonoBehaviour
{
public string objectName = null;
void Awake()
{
Debug.Log("Awake!" + objectName);
}
}
0. 전제 사항
이러한 느낌으로 위의 스크립트를 GameObject/Cube/Sphere에 추가하여 ObjectName에서 직접 개체 이름을 설정합니다.

GameObject/Cube/Sphere의 active 상태를 전환하는 실험입니다.
1. 처음부터 Active


2. 처음에는 Deactive

아무것도 아니야!



3. 하위 요소의 일부만 비활성


큐브만 안 나와!
→ Cube를 Active로 설정하면 log도 나타납니다
4. 그래도 나는 실례화된 후에 쓰고 싶다!
스크립트 수정
구조 함수를 정의하시겠습니까?
AwakeTest.cs
using UnityEngine;
public class AwakeTest : MonoBehaviour
{
public string objectName = null;
public AwakeTest()
{
Debug.Log("Init!" + objectName);
}
void Awake()
{
Debug.Log("Awake!" + objectName);
}
}
실험
3. 실행 후 하위 요소의 일부분만 Deactive 조건에 부합됩니다
Init!Cube
출력하면 이긴다!
🤔...
기본 MonoBehaviour 상속의 경우 이벤트 함수를 사용합니다.(new도 안돼)
그래도 어쨌든...!이런 상황에서 Prefab는 Instantiate를 정의하고 생성한 후 즉시 전용 처리를 호출하여 방법에 따라 이런 분위기를 조성할 수 있다.
다시 결론을 얻다
참고 자료
Reference
이 문제에 관하여(Awake는 실례화되고 바로 불려가는 물건 아닌가...?), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/nir_takemi/items/13c73234a632f55b9a58텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)