[FEATURE REQUEST] Decorator enabling

I often find that I create classes with many methods that I have to associate with event in onEnable and disassociate in onDisable, and for each of them I have to add an instruction in onEnable and onDisable.
I wish it was possible to simplify the code with a decorator that indicates the object and the event.
Example

public class MainMenu : MonoBehaviour
{
    private Button _button;

    private void OnEnable()
    {      
        _button.RegisterCallback<ClickEvent>(CmdPlay);
    }

    private void CmdPlay(ClickEvent clickEvent)
    {
        Debug.Log("CmdPlay press "+clickEvent.ToString());
    }

    private void OnDisable()
    {
        _button.UnregisterCallback<ClickEvent>(CmdPlay);
    }
}

Into something that looks like:

public class MainMenu : MonoBehaviour
{
	private Button _button;   
	[DecoratorThatAssociatesInEnableDisable(_button.ClickEvent)] // I mean precisely the object as such, not its name
    private void CmdPlay(ClickEvent clickEvent)
    {
        Debug.Log("CmdPlay press "+clickEvent.ToString());
    }
}

so that it is generic and works with other events without having to edit the code each time but just writing the reference to the object and event into the decorator.

I find it more elegant and shorter to use a decorator that indicates what it refers to rather than having to write code in OnEnable, where I maybe write something else.

OR

public class Example : MonoBehaviour
{
    private MyClass	myObj;

    private void OnEnable()
    {
        myObj.OnInitialized += OnInitialized;
    }
	private void OnDisnable()
    {
        myObj.OnInitialized -= OnInitialized;
    }

    private void OnInitialized()
    {
        Debug.Log("initialized...");
    }
}

into

public class Example : MonoBehaviour
{
    private MyClass	myObj;

	[DecoratorThatAssociatesInEnableDisable(myObj.OnInitialized)]
    private void OnInitialized()
    {
        Debug.Log("initialized...");
    }
}

Do you know VB: Sub EventHandler() **Handles** Obj.Ev_Event?

Try looking into Source generators. Here’s an example that a quick google search gave me.

https://medium.com/@EnescanBektas/using-source-generators-in-the-unity-game-engine-140ff0cd0dc

This feature is too specific and also hacky.

[DecoratorThatAssociatesInEnableDisable(_button.ClickEvent)]

Attributes can only accept constants, in that case strings or nameof. Ok, it will be “_button.OnClick” (Odin Inspector uses something similar).
What about different event types? We have at least Action and UnityEvent which have different subscription logic.
How would it work under the hood? Reflection, type checking and specific handlers which will handle subscriptions? What about performance? What about debuggability? What about compilation times?
It’s not like “oh, smart engineers will know how to do it”, no, not everything is easily possible without drawbacks.

If you still want that, try doing that with source generators, it’s probably the best and fastest you can get.

This vaguely reminds me of @LurkingNinja’s Domain Reload source generation. It might be a helpful starting point for your own requirements.

Oh or maybe it was this one: GitHub - LurkingNinja/com.lurking-ninja.dependency: Simplified dependency Injection for Unity utilizing SourceGeneration making it as performant as possible.

Thanks for the idea but use remote APIs.

@Lekret

I know this is something not yet implemented, which is why I labeled it as [FEATURE REQUEST].
As for debugging and performance, I wouldn’t worry: methods like Start and Update are standard and are interpreted by debug.