- Home /
Question by
jamesmunro · Jul 12, 2017 at 03:38 PM ·
uimonobehaviourevents
Call AddListener on sibling components from MonoBehaviour.Reset
I am investigating options for moving away from the default Unity drag-and-drop editor event assignment.
I want to create a simple script that remaps the UnityEvent OnClick for a Button to my own C# OnClick event. To do this I was hoping to be able to use MonoBehaviour.Reset to control mapping the editor-style UnityEvents to my default C# style events but I cannot get it to work.
Is it even possible to use MonoBehaviour.Reset to modify events on sibling components?
[RequireComponent(typeof(Button))]
public class ButtonEventAdapter : MonoBehaviour {
private Button button;
public delegate void UserInterfaceDelegate ();
public event UserInterfaceDelegate ClickedEvent;
private void Awake () {
button = GetComponent<Button> ();
}
private void Reset () {
button = GetComponent<Button> ();
//this does not work
button.onClick.RemoveAllListeners ();
button.onClick.AddListener (() => OnClick ());
}
public void OnClick () {
if (ClickedEvent != null)
ClickedEvent ();
}
}
Comment