Unity

[UNITY] Delegate / Event 를 생각하려 할 때 마다 까먹어서 적어두는 용

Guk-blog 2020. 9. 1. 16:57
728x90
반응형
using UnityEngine;
public class EventManager :MonoBehaviour
{
    public static EventManager instance;
    public delegate void MyDelegate();
    public event MyDelegate eventCall;


    void Awake()
    {
        instance = this;
    }


    public void EventCall()
    {
        eventCall();
    }
}
using UnityEngine;

public class Delegate_Sample : MonoBehaviour
{
    void EventFunction()
    {
        Debug.Log("Success");
    }

    public void OnButtonClick()
    {
        Debug.Log("Click");
        EventManager.instance.EventCall();
    }

    private void OnEnable()
    {
        EventManager.instance.eventCall += EventFunction;
    }

    private void OnDisable()
    {
        EventManager.instance.eventCall -= EventFunction;
    }
}
728x90
반응형