Search Unity

Extension Method in UnityEvent Inspector Panel?

Discussion in 'Scripting' started by IsDon, Jul 16, 2018.

  1. IsDon

    IsDon

    Joined:
    Feb 27, 2015
    Posts:
    35
    I am trying to add some usability extensions to a Scene for some colleagues to be able to add basic script-functionality just through the inspector. I can write extension methods for my scripts, but was hoping they could show up in the Action() part of a Script with:

    Code (CSharp):
    1. public UnityEvent Action;
    I'm not seeing any Extension Methods in the list under GameObject though (just the built in Unity public functions)

    Extension method:
    Code (CSharp):
    1. public static class ExtensionMethods
    2. {
    3.     public static void ToggleActive(this GameObject obj)
    4.     {
    5.         obj.SetActive(!obj.activeSelf);
    6.     }
    7. }
    Is this known behaviour (2017.4), and is there a preferred way of doing this sort of thing that I should be moving over to?

    Thanks,

    Don
    have a great day
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Extension methods are just syntax sugar around a static method call. Nothing is actually being added to the object, so its impossible for Unity to reflect into the object to pick up the method -- it simply doesn't exist.