Hi all,
I’m trying to use the UnityEvent system so that my EditorScript can serialise Actions and save them to the object for Event binding, but I’m having trouble with the system. I have this code:
public BindedEvent CreateAndBindEvent(EventTriggerType DesiredTriggerType, OverloadEventBinder DesiredOverload, object[] MethodParamaterValues)
{
//Creates invokable action and name
UnityAction<object> Callback = DesiredOverload.CreateInvokableAction((object[])MethodParamaterValues.Clone());
string EventName = DesiredOverload.CreateEventName(MethodParamaterValues);
UnityObjectEvent CallbackEvent = new UnityObjectEvent();
UnityEventTools.AddPersistentListener(CallbackEvent, Callback);
//Binds the event
return new BindedEvent(EventName, DesiredTriggerType, CallbackEvent, ClassType);
}
However when I run it, I get a rather unhelpful exception thrown
ArgumentException: Could not register callback <>m__0 on . The class null does not derive from UnityEngine.Object
UnityEngine.Events.UnityEventBase.ValidateRegistration (System.Reflection.MethodInfo method, System.Object targetObj, PersistentListenerMode mode, System.Type argumentType) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:841)
UnityEngine.Events.UnityEventBase.ValidateRegistration (System.Reflection.MethodInfo method, System.Object targetObj, PersistentListenerMode mode) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:830)
UnityEngine.Events.UnityEventBase.RegisterPersistentListener (Int32 index, System.Object targetObj, System.Reflection.MethodInfo method) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:866)
UnityEngine.Events.UnityEvent`1[T0].RegisterPersistentListener (Int32 index, UnityEngine.Events.UnityAction`1 call) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:79)
UnityEngine.Events.UnityEvent`1[T0].AddPersistentListener (UnityEngine.Events.UnityAction`1 call, UnityEventCallState callState) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:67)
UnityEngine.Events.UnityEvent`1[T0].AddPersistentListener (UnityEngine.Events.UnityAction`1 call) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:60)
UnityEditor.Events.UnityEventTools.AddPersistentListener[Object] (UnityEngine.Events.UnityEvent`1 unityEvent, UnityEngine.Events.UnityAction`1 call) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Utils/UnityEventTools.cs:26)
AMPInternal.EventBinding.ClassEventBinder.CreateAndBindEvent (EventTriggerType DesiredTriggerType, AMPInternal.EventBinding.OverloadEventBinder DesiredOverload, System.Object[] MethodParamaterValues) (at Assets/AudioManagerPro/Editor/AMPEventBinderInspector.cs:71)
AMPInternal.Editor.AMPEventBinderInspector.OnInspectorGUI () (at Assets/AudioManagerPro/Editor/AMPEventBinderInspector.cs:258)
UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean rebuildOptimizedGUIBlock, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1229)
UnityEditor.DockArea:OnGUI()
I’d really appreciate it if anyone here could help, I’ve posted the rest of the code that I feel is relevant but please ask for more if you need to.
public UnityAction<object> CreateInvokableAction(params object[] MethodParamaterValues)
{
if (MethodParamaterValues.Length != MethodParamaters.Length) { throw new ArgumentException("Incorrect paramater count passed.", "MethodParamaterValues"); }
return (object TargetObject) => MethodOverload.Invoke(TargetObject, MethodParamaterValues);
}
[Serializable]
public class UnityObjectEvent : UnityEvent<object> { }