UnityEvent not showing extension methods
I was working on a simple script that would toggle the active state of some objects using an UnityEvent based on a button press (I was planning on using this to stop a user from walking when opening a menu). The problem was that when I tried to select my extension method that I wrote for the GameObject class, it was nowhere to be found in the UnityEvent method selection dropdown. I have since realised that this solution was not really gonna work how I wanted it to, plus it was kind of ugly, but it still got me wondering: Is it possible to execute extension methods with UnityEvents?
Toggle script:
using UnityEngine;
public static class ExtensionMethods
{
public static void ToggleActive(this GameObject go)
{
go.SetActive(go.activeSelf);
}
}
Answer by Nit_Ram · May 07, 2019 at 08:26 AM
I stumbled upon the same issue. Is there any chance we can see this feature being added in the future?
Answer by chantey · Dec 22, 2020 at 12:23 AM
I solve this by wrapping my extension methods in a utility component:
using UnityEngine;
public class TransformUtility : MonoBehaviour
{
public void SetPose(Pose pose) => transform.SetPose(pose);
}