I’ve created custom buttons in Unity’s UI and have added my own UnityEvents to these buttons so that I can assign functions to call on click events and such. I’m doing this with:
[System.Serializable]
public class ToggleEvent : UnityEvent<bool> { }
[SerializeField]
public ToggleEvent clickEvent;
With the event calls looking simply like:
public void ButtonEvent(bool value) { }
This works great in the editor but when I call
clickEvent.Invoke(true);
…it’ll only pass whatever the bool value is marked off in the editor, not what’s actually being passed. How do I override what’s being set in the editor and pass my own variables in code?
Welp I seem to have found an ugly workaround. Instead of calling clickEvent.Invoke I can disable persistent methods and invoke the one that I want to call like so:
// this is to turn off the value and call set in the editor
clickEvent.SetPersistentListenerState(0, UnityEventCallState.Off);
clickEvent.RemoveAllListeners();
// get the method assigned in the editor and call it
MethodInfo methodInfo = UnityEventBase.GetValidMethodInfo(clickEvent.GetPersistentTarget(0), clickEvent.GetPersistentMethodName(0), new System.Type[] { typeof(bool) } );
methodInfo.Invoke(GameObject.FindObjectOfType<MenuModsScript>(), new object[] { _enabled });
I’ll clean it up for sure but if anyone knows anything else I’m all ears.