Experiment/Unity

Unity, C#, Action 과 +=, -=

hiheybye 2024. 5. 25. 16:17

 

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

public class DelegateTest : MonoBehaviour
{
    public Action A;
    public Action B;
    public Action C;
    
    void Start()
    {
        A = () => Debug.Log("Action A");
        B = () => Debug.Log("Action B");
        C = () => Debug.Log("Action C");

    }


}

 

 

A += B; , A+=C;

더보기
    void Start()
    {
        //~~~

        A += B;
        A += C;

        Debug.Log("A.Invoke()");
        A.Invoke();

        Debug.Log("B.Invoke()");
        B.Invoke();

    }

 

A.Invoke()

A -> B -> C

 

B.Invoke()

B

 

 

 

 

A += B ;  B += C;

더보기
     void Start()
     {
        //~~~

         A += B;
         B += C;

         Debug.Log("A.Invoke()");
         A.Invoke();

         Debug.Log("B.Invoke()");
         B.Invoke();

     }

 

A.Invoke()

A -> B

 

B.Invoke()

B -> C

 

 

A -= A;

더보기
    void Start()
    {
        //~~~

        A -= A;

        bool test = A == null;
        Debug.Log(test);

    }

 

 True