Hey all,
Little weird thing here. I want to, under certain known conditions, redirect a selection in the editor from one object up to a given parent object. So essentially when the user makes a selection, I grab Selection.activeTransform and do some checks then if neccesary change the selection with by setting Selection.activeTransform to the new section.
And this works fine. IF the inspector window is visible. If the inspector window is not visible, for example we often work with several tabs in the same pane with tine inspector, so if one of the other tabs is selected instead, then the selection will not change until you make the inspector visible again.
This feels like a straight up Unity bug to me, but has anyone run into this and know of a workaround?
Are you trying to alter the selection from inside an “OnInspectorGUI” function?
Update at the moment, though that was initially just done to make it easy to test quickly. I’m certainly open to putting it elsewhere if that matters.
Seems odd thought that where the code is located would matter given the behaviour.
From the docs: “(In edit mode) Update is only called when something in the Scene changed.”
However, it looks like you can use EditorApplication.QueuePlayerLoopUpdate() to get updates to function in edit mode like play mode. I haven’t used it and the docs don’t do a great job of explaining it, so hopefully it works for you. Unity - Scripting API: EditorApplication.QueuePlayerLoopUpdate
I’ll double check tomorrow when I get into the studio, but i’m 90% certain the problem isn’t with the Update method getting called as we have other functions in there that work as expected. That was why I threw this in there to test quickly as we already the hook in place.
Ok so digging more into this. You are correct that Update isn’t being called as expected. It seems if the Inspector window is open then Update does get called if you change selection, but if another window is open it does not. As a result the call in script to change the selection isn’t getting called unless the inspector is open.
So I instead hooked into the Selection.selectionChanged delegate. That seems to work in that I reliably get the selection changed call even if the inspector is closed, and my call to set Selection.activeTransform to a new value DOES get executed, but no strangely the call doesn’t actually do anything, even if the inspector is open. The same code that DID work before now does nothing, even though it is reliably called where before it wasn’t.
Very odd. Thinking maybe Unity was somehow blocking the call to prevent infinite recursion (I respond to the event by setting a new selection which calls the event which sets a new selection…) I tried wrapping the call that changes the selection in calls to unsubscribe from the delegate, change the selection, then resubscribe to the delegate but that had no effect either.
So basically I’ve gone from code that works but doesn’t get called reliably, to code that gets called reliably but doesn’t work 
Combining the two approches works. Unity for some reason simply won’t allow you to change the selection from within he selectionChaged delegate, even if you unsubscribe first but what does work is using the selectionChanged delegate to fire off an Update call and then using my code as it originally was.
Turns out, that calling a Coroutine from the subscribed method solves the problem. Make sure you yield return new WaitForSeconds(0.01f); before setting the active selection!
1 Like