유니티 이벤트 함수도 상속이되는지 궁금해서 해봤습니다
private으로 상속안할때
public class Parent : MonoBehaviour
{
private void Awake()
{
Debug.Log("Parent 로그");
}
}
public class Child : Parent
{
private void Awake()
{
Debug.Log("Child 로그");
}
}

public으로 하면 경고는 뜨지만 똑같이 실행된다.
상속
public이나 protected로 만든 후 부모에 virtual 자식에 override를 써주고
부모 함수는 base로 호출한다.
public class Parent : MonoBehaviour
{
protected virtual void Awake()
{
Debug.Log("Parent 로그");
}
}
public class Child : Parent
{
protected override void Awake()
{
Debug.Log("Child 로그");
base.Awake();
}
}

코루틴 상속
public class Parent : MonoBehaviour
{
protected virtual IEnumerator Cor()
{
Debug.Log("부모 코루틴");
yield return null;
}
}
public class Child : Parent
{
protected void Awake()
{
StartCoroutine(base.Cor());
StartCoroutine(Cor());
}
protected override IEnumerator Cor()
{
Debug.Log("자식 코루틴");
yield return null;
}
}

'유니티' 카테고리의 다른 글
| [Unity] Rigidbody 날아가는 궤적 그리기 (0) | 2023.02.26 |
|---|---|
| [Unity] Material Property Block 사용하여 배칭시키기 (0) | 2023.02.20 |
| [Unity] 서로 이어지는 이펙트 만들기 (2D) (0) | 2023.01.29 |
| [Unity] Json Utility : 유니티에서 Json 읽기 쓰기 (0) | 2022.03.15 |
| [Unity] 레이어 마스크(Layer Mask) (0) | 2022.02.27 |