메소드를 대신 호출하는 역할을 함.
delegate 선언
delegate Type delegateName(parameter);
delegate 변수 생성
delegateName delegateVar;
delegate 메소드 참조
delegateVar = new delegateName(func);
*편의상 유니티를 사용함.
Ex1) Test1
결과화면

콜백메서드 활용
델리게이트가 매소드의 파라미터로 들어가서 다른 매소드를 호출해준다.
Ex2) Test2
결과화면

일반화 델리게이트
어떤 타입이든 참조 가능하다.
Ex3) Test3
결과 화면

델리게이트 체인
하나의 델리게이트가 여러개의 매소드를 참조하는 기능
Ex4) Test4
결과화면

참고
http://mrw0119.tistory.com/19
http://itmining.tistory.com/43
delegate 선언
delegate Type delegateName(parameter);
delegate 변수 생성
delegateName delegateVar;
delegate 메소드 참조
delegateVar = new delegateName(func);
*편의상 유니티를 사용함.
Ex1) Test1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DelegateTest1 : MonoBehaviour {
// 델리게이트 선언
delegate int MyDelegate(int a, int b);
public int Plus(int a, int b)
{
return a + b;
}
public int Minus(int a, int b)
{
return a - b;
}
// Use this for initialization
void Start()
{
// 델리게이트 변수 생성
MyDelegate calculate;
// 델리게이트 메소드 참조
calculate = new MyDelegate(Plus);
int sum = calculate(3, 2);
Debug.Log("sum : " + sum);
calculate = new MyDelegate(Minus);
Debug.Log("minus : " + calculate(3, 2));
}
}
| cs |
결과화면

콜백메서드 활용
델리게이트가 매소드의 파라미터로 들어가서 다른 매소드를 호출해준다.
Ex2) Test2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DelegateTest2 : MonoBehaviour {
delegate int Mydelegate(int a, int b);
// 콜백메서드 활용
int Calculator(int a, int b, Mydelegate dele)
{
return dele(a, b);
}
int plus(int a, int b)
{
return a + b;
}
int minus(int a, int b)
{
return a - b;
}
int multiply(int a, int b)
{
return a * b;
}
private void Start()
{
Mydelegate Plus = new Mydelegate(plus);
Mydelegate Minus = new Mydelegate(minus);
Mydelegate Multiply = new Mydelegate(multiply);
Debug.Log("Plus : " + Calculator(3, 2, Plus));
Debug.Log("Minus : " + Calculator(3, 2, Minus));
Debug.Log("Multiply : " + Calculator(3, 2, Multiply));
}
}
| cs |
결과화면

일반화 델리게이트
어떤 타입이든 참조 가능하다.
Ex3) Test3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DelegateTest3 : MonoBehaviour {
// 선언
delegate T Mydelegate<T>(T a, T b);
// 델리게이트를 적용할 함수
void Calculator<T>(T a, T b, Mydelegate<T> dele)
{
Debug.Log(dele(a, b));
}
int plus(int a, int b)
{
return a + b;
}
float minus(float a, float b)
{
return a - b;
}
double multiply(double a, double b)
{
return a * b;
}
private void Start()
{
// 생성과 참조
Mydelegate<int> Plus = new Mydelegate<int>(plus);
Mydelegate<float> Minus = new Mydelegate<float>(minus);
Mydelegate<double> Multiply = new Mydelegate<double>(multiply);
Debug.Log("Plus(int) : ");
Calculator(3, 2, Plus);
Debug.Log("Minus(float) : ");
Calculator(3.3f, 2.2f, Minus);
Debug.Log("Multiply(double) : ");
Calculator(3.3d, 2.2d, Multiply);
}
}
| cs |
결과 화면

델리게이트 체인
하나의 델리게이트가 여러개의 매소드를 참조하는 기능
Ex4) Test4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DelegateTest4 : MonoBehaviour {
delegate void MyDelegate();
public void Method01()
{
Debug.Log("01");
}
public void Method02()
{
Debug.Log("02");
}
public void Method03()
{
Debug.Log("03");
}
private void Start()
{
MyDelegate dele;
dele = new MyDelegate(Method01);
dele += Method02;
dele += Method03;
dele();
dele -= Method01;
dele -= Method03;
dele();
}
}
| cs |
결과화면

참고
http://mrw0119.tistory.com/19
http://itmining.tistory.com/43
댓글
댓글 쓰기