반응형
- 델리게이트 선언
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 + "만큼 감소");
}
}
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 예외처리 Try Catch Finally 문 간단 사용법 (0) | 2022.06.30 |
---|---|
유니티 C# 반복문 for, foreach, While 문 간단 사용법 (0) | 2022.06.30 |
유니티 C# 조건문 if, else, Swtich 문 간단 사용법 (0) | 2022.06.30 |
유니티 C# Application 관련 API 사용법 모음 (0) | 2022.06.21 |
유니티 C# 로컬 데이터 저장, 불러오기 PlayerPrefs 간단 사용법 (0) | 2022.06.21 |
유니티 C# 타이머 만들기 시분초 00:00:00 Timer (0) | 2022.06.21 |
유니티 C# 충돌 처리 OnTrigger Enter, Stay, Exit 간단 사용법 (0) | 2022.06.21 |
유니티 C# 코루틴 동작, 시간 딜레이 Coroutine, Invoke 간단 사용법 (0) | 2022.06.21 |
댓글