vendredi 4 octobre 2019

What is 'delegate removal' before 'delegate combination' pattern for?

In many Unity code examples where one has to subscribe to editor callbacks, you frequently meet the following pattern:

Undo.postprocessModifications -= OnPostprocessModifications;
Undo.postprocessModifications += OnPostprocessModifications;

// then, later, in some other method
Undo.postprocessModifications -= OnPostprocessModifications;

I used to do the following (inside an EditorWindow):

void OnEnable()
{
    Undo.postprocessModifications += OnPostprocessModifications;
}

void OnDisable()
{
    Undo.postprocessModifications -= OnPostprocessModifications;
}

Question:

What exactly is that delegate removal before delegate combination pattern for ?

Is it solely to ensure extra safety or does it has other benefits?

Aucun commentaire:

Enregistrer un commentaire