I've been looking online for an answer for a while now, and can't find anything conclusive, which leads me to think you can't, but I thought it better to ask just to clarify.
I'm using a custom inspector window to handle text display, and I've got it to handle single argument functions with AddIntPersistentListener
, AddStringPersistentListener
, ect, but I want to be able to do more complicated things that require 2 or 3 parameters. Originally, I tried using a lambda for this, but it throws an error when I try to update the event.
I understand you can use a UnityEvent<T0,T1,T2>
to have multiple arguments, but to my knowledge, those aren't persistent. I have a function that generates the event, so I could generate the event and then invoke it, meaning that it wouldn't have to be persistent, but I feel that would be clunky.
Does anyone have any suggestions?
CodePudding user response:
Adding persistent listeners is done with UnityEventTools.
But since you have used it for an UnityEvent with one parameter I think you know this.
Using lambda functions with not work for this as unity needs a reference so serialize in the listener.
using UnityEditor.Events; // works only in editor
using UnityEngine;
using UnityEngine.Events;
public class PersistentListenerExample: MonoBehaviour
{
public UnityEvent<int, string, float> OnMyEvent;
private void Start()
{
int a = 1;
string b = "2";
float c = 3.0f;
UnityEventTools.AddPersistentListener(OnMyEvent, OnMyEventFired);
}
public void OnMyEventFired(int a, string b, float c)
{
Debug.Log("OnMyEventFired with params: " a ", " b ", " c);
}
}