728x90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// 함수들을 관리해주는 녀석들
public delegate void ChainFunction(int value);
public static event ChainFunction onStart;
// ChainFunction chain; 이벤트를 선언하지 않을 경우 해줘야합니다.
int power;
int defence;
public void SetPower(int value)
{
power += value;
print("power의 값이" + value + "만큼 증가했습니다. 총 power의 값 = " + power);
}
public void SetDefence(int value)
{
defence += value;
print("defence의 값이" + value + "만큼 증가했습니다. 총 defence의 값 = " + defence);
}
void Start(){
// chain += SetPower; //ChainFunction에 SetPowr를 넣는다.
// chain += SetDefence;
// chain(5); // SetPower와 SetDefence가 동시에 실행됩니다.
// chain -= SetDefence; //SetDefence가 빠지고 SetPower만 실행
onStart += SetPower;
onStart += SetDefence;
}
public void OnDisalbe()
{
onStart(5); //게임이 꺼지면 세개의 함수를 리턴한다.
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test2 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Test.onStart += Abc; //시작하자마자 onStart에 Abc함수가 담긴다.
}
public void Abc(int value) //int value를 해주는 이유는 ChainFunction의 값이 int value를 가지고 있기 때문이다.
{
print(value + '값이 증가했습니다.')
}
// Update is called once per frame
void Update()
{
}
}
델리게이트는 하나의 클래스 안에 있는 함수들을 관리 감독할 수 있고 그 델리게이트를 받아서 사용하는 event는 타 클래스까지 함수를 관리 감독 할 수 있다.
728x90