Rainbow🌈Coder 2021. 12. 25. 23:05
728x90

Delegate μœ„μž„μ΄λž€ 전체 μž‘μ—… 쀑 일뢀λ₯Ό λˆ„κ΅°κ°€λ₯Ό λŒ€μ‹ ν•΄ 효율적으둜 μ²˜λ¦¬ν•˜λŠ” ν˜•νƒœλ₯Ό λ§ν•œλ‹€.

또 λ‹€λ₯Έ ν˜•νƒœμ˜ μœ„μž„μœΌλ‘œ C# μ œλ„ˆλ¦­κ³Ό Action λ˜λŠ” Action<T>λ₯Ό μ‚¬μš©ν•˜λŠ” 방식이 μžˆλ‹€.

μœ„μž„μ„ μ‚¬μš©ν•˜λŠ” μ£Όμš”ν•œ 두 개의 νŒ¨ν„΄μ€ μ„€μ •κ°€λŠ₯λ©”μ†Œλ“œ(Configurable method)νŒ¨ν„΄κ³Ό

μœ„μž„(delegation) νŒ¨ν„΄μ΄λ‹€.

 

1. μ„€μ • κ°€λŠ₯ λ©”μ†Œλ“œ νŒ¨ν„΄

μΌμ΄λ‚˜ ν•¨μˆ˜μ˜ 일뢀λ₯Ό λ‹€λ₯Έ λ©”μ†Œλ“œλ‘œ 전달해 μž‘μ—…μ„ μ™„λ£Œν•˜λŠ”λ° μ‚¬μš©ν•˜λŠ” νŒ¨ν„΄μ΄λ‹€.

이 νŒ¨ν„΄μ€ κ³ μœ ν•œ 방식을 κ°€μ§„ κ°œλ³„ μ½”λ“œκ°€ κ³΅ν†΅λœ μž‘μ—…μ„ μˆ˜ν–‰ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

예λ₯Ό λ“€λ©΄ κ±·κΈ°, λ›°κΈ°, 탐색 등이 이에 ν•΄λ‹Ήν•œλ‹€.

이 λͺ¨λ“  μž‘μ—…μ€ μΊλ¦­ν„°μ˜ κΈ°λ³Έ ν–‰μœ„κ°€ 될 수 μžˆλ‹€.

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

public class DeleTest : MonoBehaviour
{
    //μœ„μž„ λ©”μ†Œλ“œ μ‹œκ·Έλ‹ˆμ²˜ μ •μ˜
    delegate void RobotAction();
    //μœ„μž„μ„ μœ„ν•œ 프라이빗 속성
    RobotAction myRobotAction;

    private void Start()
    {
        //μœ„μž„μ„ μœ„ν•œ κΈ°λ³Έ λ©”μ†Œλ“œ μ„€μ •
        myRobotAction = RobotWalk;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.W)) myRobotAction = RobotWalk;//μœ„μž„ λ©”μ†Œλ“œλ₯Ό 걷기둜 μ„€μ •
        if (Input.GetKeyDown(KeyCode.R)) myRobotAction = RobotRun;//μœ„μž„ λ©”μ†Œλ“œλ₯Ό λ›°κΈ°λ‘œ μ„€μ •
        //μ—…λ°μ΄νŠΈ λ•Œ μ„ νƒλœ μœ„μž„μ„ μ‹€ν–‰
        myRobotAction();
    }

    void RobotWalk()
    {
        Debug.Log("뚜 λ‘‘.. 뚜 뚝.. κ±·λŠ”λ‹€.. 둜보트..");
    }

    void RobotRun()
    {
        Debug.Log("λšœλ‘‘ 뚜뚝 λ›΄λ‹€ λ‘œλ΄‡..");
    }
}

2. μœ„μž„ νŒ¨ν„΄

μœ„μž„ νŒ¨ν„΄μ€ λ©”μ†Œλ“œκ°€ 헬퍼 라이브러리λ₯Ό ν˜ΈμΆœν•˜λŠ” μƒν™©μ—μ„œ μ‚¬μš©λœλ‹€.

μš”μ²­ν•œ μž‘μ—…μ΄ μ™„λ£Œλ˜λ©΄ λ‹€μ‹œ 메인 ν•¨μˆ˜λ‘œ λŒμ•„μ˜¨λ‹€.

μ›Ήμ—μ„œ 자료λ₯Ό λ‹€μš΄λ‘œλ“œν•  λ•Œ 이 νŒ¨ν„΄μ„ 주둜 μ‚¬μš©ν•œλ‹€.

λ‹€μš΄λ‘œλ“œκ°€ λλ‚˜λ©΄ 받은 λ‚΄μš©μ„ κ°€μ§€κ³  νŠΉμ •ν•œ 후속 처리λ₯Ό μ§„ν–‰ν•˜λŠ” ν˜•νƒœμ΄λ‹€.

(μ½”λ“œ μƒλž΅)

 

 

3. ν•©μ„± μœ„μž„

λ‹€μˆ˜μ˜ ν•¨μˆ˜λ₯Ό ν•˜λ‚˜μ˜ μœ„μž„μ— μ§€μ •ν•  수 μžˆλ‹€.

이 κΈ°λŠ₯은 λ‹€μˆ˜μ˜ 곡용 ν•¨μˆ˜λ₯Ό μ—°κ²°ν•΄ μ‹€ν–‰ν•˜λ €κ³  ν•  λ–„ 맀우 μœ μš©ν•˜λ‹€.

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

public class DeleTest : MonoBehaviour
{
        //WorkerManagerμœ„μž„
        delegate void MyDelegateHook();
        MyDelegateHook ActionTodo;
        public string WorkerType = "Peon";

        //μ‹œμž‘ν•  λ•Œ μ›Œμ»€μ—κ²Œ μž‘μ—…μ„ ν• λ‹Ήν•œλ‹€
        void Start()
        {
            //직원은 할일이 λ§Žλ‹€
            if(WorkerType == "Peon")
            {
                ActionTodo += DoJob1;
                ActionTodo += DoJob2;
            }
            //κ·Έ μ™Έ λ‚˜λ¨Έμ§€λŠ” λ‹€λ₯Έ 일을 ν•œλ‹€
            else
            {
                ActionTodo += DoJob3;
            }
        }

        //μ—…λ°μ΄νŠΈμ—μ„œ ActionToDo에 μ§€μ •λœ 일을 μˆ˜ν–‰ν•œλ‹€
        void Update()
        {
            ActionTodo();
        }

        void DoJob1()
        {
            Debug.Log("일정검토");
        }
        void DoJob2()
        {
            Debug.Log("μ„œλ₯˜μ œμΆœ");
        }
        void DoJob3()
        {
            Debug.Log("λͺ°λž˜ λͺ¨λ°”μΌκ²Œμž„!");
        }
    }
728x90