반응형
- 델리게이트 선언
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public delegate void GameEvent(); //이벤트 선언
public static event GameEvent eGameStart, eGamePause, eGameEnd;
public delegate void ScoreEvent(int number); //이벤트 선언
public static event ScoreEvent ePlusScore, eMinusScore;
private void Start()
{
eGameStart();
eGamePause();
eGameEnd();
ePlusScore(10);
eMinusScore(10);
}
}
- 델리게이트 활용
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UseExample : MonoBehaviour
{
private void OnEnable() //이벤트 등록
{
GameEvent.eGameStart += this.GameStart;
GameEvent.eGamePause += this.GamePause;
GameEvent.eGameEnd += this.GameEnd;
ScoreEvent.ePlusScore += PlusScore;
ScoreEvent.eMinusScore += MinusScore;
}
private void OnDisable() //이벤트 해제
{
GameEvent.eGameStart -= this.GameStart;
GameEvent.eGamePause -= this.GamePause;
GameEvent.eGameEnd -= this.GameEnd;
ScoreEvent.ePlusScore -= PlusScore;
ScoreEvent.eMinusScore -= MinusScore;
}
public void GameStart()
{
}
public void GamePause()
{
}
public void GameEnd()
{
}
public void PlusScore(int number)
{
Debug.Log(number + "만큼 증가");
}
public void MinusScore(int number)
{
Debug.Log(number + "만큼 감소");
}
}
반응형