How to call a function after selection has changed?

I am working on an editor window where I need to unselect a gizmo everytime it is selected using Ctrl+left click. I have intercepted ctrl + left click event but the Selection.activeGameObject changes after this event is broadcasted. I have tried invoking a function which unselects the object but invoke fails. Coroutine also doesn’t work.

Use Selection.selectionChanged delegate, as in the Reddit answer below.

Link to Reddit answer :

It’s not a normal MonoBehaviour event because it is only for the Editor, so this will be called when the highlighted asset is changed in the project assets. It’s not for runtime.

As it is a delegate, you will need to add the callback:


void OnEnable()
{
    Selection.selectionChanged += DidCallBackReturnTrue;
}

void OnDisable()
{
    Selection.selectionChanged -= DidCallBackReturnTrue;
}

Now there is the callback OnSelectionChange

you can use it like this

private void OnSelectionChange(){
    // Do stuff
}