[ Unity ] NGUI 버튼 이벤트 동적 할당
Unity3D 2015. 3. 11. 21:56반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | using UnityEngine; using System.Collections; public class MyTest : MonoBehaviour { public UIButton m_BtnTest; void Start() { EventDelegate eventBtn = new EventDelegate(gameObject.GetComponent(), "onBtnEvent"); eventBtn.parameters[0] = MakeParameter(m_BtnTest.gameObject, typeof(GameObject)); eventBtn.parameters[1] = MakeParameter(m_BtnTest, typeof(UIButton)); EventDelegate.Add(m_BtnTest.GetComponent().onClick, eventBtn); } // 이벤트 parameter를 생성하여 리턴. private EventDelegate.Parameter MakeParameter(Object _value, System.Type _type) { EventDelegate.Parameter param = new EventDelegate.Parameter(); // 이벤트 parameter 생성. param.obj = _value; // 이벤트 함수에 전달하고 싶은 값. param.expectedType = _type; // 값의 타입. return param; } public void onBtnEvent(GameObject _obj, UIButton _btn) { Debug.Log("onBtnEvent = " + _obj.name); Debug.Log("onBtnEvent _btn = " + _btn.name); } } using UnityEngine; using System.Collections; public class Test : MonoBehaviour { public UIButton m_BtnTest; public int m_nTestValue = 999; // Use this for initialization void Start() { EventDelegate.Parameter param = new EventDelegate.Parameter(); // 파라메타로 지정할 오브젝트가 있는 컴포넌트 param.obj = gameObject.GetComponent(); // 해당 오브젝트의 변수명 param.field = "m_nTestValue"; // 이벤트 등록 및 파라메타 추가 EventDelegate onClick = new EventDelegate(gameObject.GetComponent(), "onBtnTest"); onClick.parameters[0] = param; EventDelegate.Add(m_BtnTest.onClick, onClick); } public void onBtnTest(int _value) { // 버튼 클릭 시 999 출력 Debug.Log("onBtnTest value = " + _value); } } | cs |
반응형
'Unity3D' 카테고리의 다른 글
한방에 모아보는 Unity Shader 레퍼런스 Properties (0) | 2015.06.15 |
---|---|
Unity + Python GAE(Eclipse) (0) | 2015.03.17 |
NGUI 스크롤 아이템 그리드 (0) | 2015.03.11 |
Get an inactive gameobject? (0) | 2015.03.03 |
Unity3D Error building Player: UnityException: Can not sign application (0) | 2015.02.04 |