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?