I have a custom UnityEvent where I want to pass an object of the type IData dynamically (I want to pass the object from code, instead of assigning it in the inspector).
[Serializable]
public class CustomMethod : Interaction
{
[Serializable]
public class CustomActionEvent : UnityEvent<IData>
{
public CustomActionEvent() { }
}
public CustomActionEvent CallbackMethod;
protected override void Interact(IData data)
{
this.CallbackMethod.Invoke(data);
}
}
I also have seen screenshots where there were two section when selecting a method (dynamic and static). However when I write a public method with IData as Parameter, it is not in the list of dynamic methods (it is also not in the list of static methods).
I see why it is not in the static methods list: IData cannot be Serialized by Unity itself and therefore it won’t by possible to define it inside the Inspector by default.
But why doesn’t it show up in the dynamic section?
Can I add it to the dynamic method list somehow?
the listener you add just needs to have the proper method signature, and it will let you do it dynamically.
using UnityEngine;
using UnityEngine.Events;
public class UATest : MonoBehaviour {
[SerializeField] private CustomEvent _customEvent;
private void Start() {
MyType myType = new MyType(Vector3.one, Vector3.zero);
_customEvent.Invoke(myType);
}
}
[System.Serializable]
public class CustomEvent : UnityEvent<MyType> {
}
public class MyType {
public Vector3 aVector3;
public Vector3 bVector3;
public MyType(Vector3 a, Vector3 b) {
aVector3 = a;
bVector3 = b;
}
public override string ToString() {
return aVector3.ToString() + " : " + bVector3.ToString();
}
}
using UnityEngine;
public class Listener : MonoBehaviour {
public void method1(MyType myType) {
Debug.Log(myType);
}
}
Hmm, strange… I tested yours and it worked. Then I modified it to have a similar setup (interface as parameter instead of class) and it still worked.
Then I tested my own callback again and now it works.
Not sure what was the problem. Maybe reloading the scene did the job.
Thanks for digging out a 3 1/2 Month old post…
Maybe it helps somebody… I know how to program, so your answer is not relevant for me. Maybe somebody else will benefit from it.
However: If you read my the introduction post you will recognize that the code I posted already fullfills all requirements.
And if you read post #3 you will see that the problem was caused by some unity bug or something and therefore the whole thread doesn’t need any answer anymore.
Sorry for being that harsh, I know you posted with best intentions. But in general it is frowned upon digging out old threads. Especially if the answer is unnecessary