Search Unity

Redirecting edior selection only seems to work if Inspector window is visible

Discussion in 'Scripting' started by jwvanderbeck, Aug 15, 2019.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    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?
     
  2. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    1,194
    Are you trying to alter the selection from inside an "OnInspectorGUI" function?
     
  3. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    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.
     
  4. Gambit-MSplitz

    Gambit-MSplitz

    Joined:
    May 16, 2015
    Posts:
    1,194
  5. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    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.
     
  6. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    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 :D
     
  7. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    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.