Unity C# 델리게이트 이벤트

Intro

C# 델리게이트 이벤트

이벤트

이벤트는 어떤 일이 발생하면 다른 클래스에 알려주는 기능을 할 수 있다.

예를들면 캐릭터가 죽었을때 이벤트를 통해 게임매니저에서 게임을 종료시키거나 할때 사용할 수 있다.

예제

Event1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Event1 : MonoBehaviour
{
    public delegate void ChainFunction();
    public static event ChainFunction OnDie;

   
    void Start()
    {
        OnDie();
    }

}

Event2

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Event2 : MonoBehaviour
{

    void Start()
    {
        Event1.OnDie += EndGame;
    }

   public void EndGame()
    {
        print("게임이 끝났습니다.");
    }
}

간단하게 OnDie 이벤트가 발생시 Event2 클래스에서 EndGame() 함수가 호출되게 할수있다.

1

참고자료

https://youtu.be/m9_D0DQ4SGU?list=PLUZ5gNInsv_O7XRpaNQIC9D5uhMZmTYAf

태그: ,

카테고리:

업데이트:

댓글남기기