1

I'm using Zenject, but this can apply to other frameworks as well. I'm not thrilled with boiler-plate code that i have to copy/paste all over. In this case, OnEnable should subscribe to a signal, OnDisable should unsubscribe. Can an attribute be written to make this a bit tighter?

ex:

private void Construct(SignalBus signalBus) { _signalBus = signalBus; }

private void OnEnable() { _signalBus.Subscribe<MyCustomPropertiesChangedSignal>(HandleMyCustomPropertiesChanged); }

private void OnDisable() { _signalBus.TryUnsubscribe<MyCustomPropertiesChangedSignal>(HandleMyCustomPropertiesChanged); }

I'd like to be able to simplify this perhaps more like this:

    [SubscribeEnable (MyCustomPropertiesChangedSignal)]
    void HandleMyCustomPropertiesChanged (MyCustomPropertiesChangedSignal signal)
    {
....
    }

Is that even possible?

Dave A.
  • 43
  • 5
  • If you want this kind of behaviour globally in all cases, then maybe what would be ideal for you is for the SignalBus itself to not trigger the event handler when the object that owns the event handler is disabled – Steve Vermeulen May 20 '20 at 02:38
  • If I can, I think that would be modifying Zenject? But then, this could apply to any type of function, could be 'Start' or 'OnButtonClick' etc. – Dave A. Jun 13 '20 at 01:49

0 Answers0