Search Unity

Question UI Animation Problem

Discussion in 'UGUI & TextMesh Pro' started by leftshoe18, May 24, 2020.

  1. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    When I assign a game object as the Event System's currently-selected object it correctly selects the object but does not do the animation until I move off the object and come back to it. I'm simply setting the desired object through code using this:

    Code (CSharp):
    1. EventSystem.current.SetSelectedObject(my button object);
    Navigation through menus works just fine and like I said it actually does select the object (I can hit submit and use the menu option just fine without moving the selection or anything) it just doesn't do the animation/transition correctly.
     
  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Well I found a solution that feels really hacky but it works. I added a coroutine that re-triggered the animation after waiting one frame. Since it's in the menu only I'm kinda okay with this slight performance hit but I'm still confused as to why it's happening in the first place.
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I checked the source code. I couldn't find
    SetSelectedObject()
    but
    SetSelectedGameObject()
    . That should actually trigger the state change with transition. Alternatively you could also write
    myButton.Select()
    which does basically the same as the other line but does some additional checks (so, it is a bit safer to use that one, but on the other hand you don't get informed if there is a problem).

    It can be, if you try to set the selection right after loading the scene that the UI stuff is not fully initialized yet (the UI needs a frame for some stuff to layout correctly). In that case waiting one frame is the solution to go for. ´
    This is not really a performance hit.
     
    leftshoe18 likes this.
  4. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Yeah that was a typo on my fault copying it down here. Didn't realize the Select() thing existed. lol Thank you.