본문 바로가기

유니티

[Unity] Awake Start Coroutine 상속

유니티 이벤트 함수도 상속이되는지 궁금해서 해봤습니다

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;
    }
}