응용프로그램/유니티(Unity)

Unity - C# Delegate

zelkova 2020. 6. 24. 17:42

<목차로 돌아가기>

 

Delegate 선언과 참조 설명 블로그

참조 블로그 2

 

Delegate를 메서드 파라미터로 전달

 

main.cs

test.setDelegateFunc(btnDeleteImage);

void btnDeleteImage(int idx){ 

//소스코드 

}

 

test.cs

    public delegate void DelItemIndex(int idx);

    private DelItemIndex delItemIndex;

 

    public void setDelegateFunc(DelItemIndex func)

    {

        delItemIndex = func;

    }

    public void btnDeleteItem()

    {

        delItemIndex(itemIndex);

    }

 

 

event를 사용하여 추가제거하는 방법

 

ColliderCheck.cs

    public delegate void ColliderExitCheck(String item,string name);

    public static event ColliderExitCheck colliderExitCheck;

 

 

test.cs

    void Start(){

        ColliderCheck.colliderExitCheck += checkExitCollider;

    }

    private void OnDestroy()

    {

        ColliderCheck.colliderExitCheck -= checkExitCollider;

    }

 

   void checkExitCollider(String item,string name)

    {

 

    }

 

 

 

Action함수 delegate와 큰 차이는 없는듯.

[Serializable]

public class SelectionPageChangedEvent : UnityEvent<int> { }

 

[SerializeField]

private SelectionPageChangedEvent m_OnSelectionPageChangedEvent 

    = new SelectionPageChangedEvent();

 

public SelectionPageChangedEvent OnSelectionPageChangedEvent { 

    get { return m_OnSelectionPageChangedEvent; } 

    set { m_OnSelectionPageChangedEvent = value; } 

}

 

scrollSnap.OnSelectionPageChangedEvent.AddListener(endScrollChangeEvent);

scrollSnap.OnSelectionPageChangedEvent.RemoveListener(endScrollChangeEvent);

 

반응형