🎮 Unity Study/Unity

[Unity] Button onClick.AddListener

ibelieveinme 2024. 2. 16. 15:26
728x90

 

AddLisener 로 버튼에 이벤트 할당하기

 

1. 인자가 없는 메서드 일 때

void Awake()
{
    button.onClick.AddListener(PressBtn);
}

void PressBtn()
{
}

 

 

2. 인자 있는 메서드 일 때

void Awake()
{
    button.onClick.AddListener(() => OpenPopup("Title"));
    button.onClick.AddListener(delegate { OpenPopup("Title"); });
}

void OpenPopup(string title)
{
}

인자가 있는 메소드를 호출할 때는 람다식이나 델리게이트를 사용한다.

 

 

* 참고

https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.Button-onClick.html

 

Unity - Scripting API: UI.Button.onClick

You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see: You've told us there are code samples on this page which don't work. If you know ho

docs.unity3d.com

 

728x90