Hello, I want all monobehaviors to unsubscribe from events in OnDestroy call without even implementing it, but I’m not sure how to do this. I was thinking that I could implement extension method of MonoBehavior and add OnDestroy function and default behavior to it, than when object inherits from MonoBehavior it would by default run OnDestroy when gameobject is destroyed, but I’m not sure if that’s correct approach and if it can be done that way. I Just want to avoid having OnDestroy written and implemented in same way in all MonoBehaviors, that looks awful.
something like this is what yoou are looking for
public class OnDestroyMonobehaviour : MonoBehaviour
{
protected void OnDestroy()
{
//your stuff
}
}
public class RandomClass : OnDestroyMonobehaviour
{
private void Start()
{
//you can use it as a normal monobehaviour method and will implement the OnDestroy from the parent
}
}