Search Unity

Quick Transition Tutorial!

Discussion in 'UI Toolkit' started by SimonDufour, Nov 26, 2021.

  1. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    Here is a small tutorial detailing how to achieve a transition from scratch!

    1. Create the documents:

    1 - Create Documents.gif
    You can create uss/uxml and a c# window using the menu item in the project.
    The window will automatically open at the end, or else you can open manually with the menu item in Window->UI Toolkit


    2. Add a selector and a transition

    2 - Add a Selector+Style+transition.gif


    You can open in the UI Builder the UXML that was just created by double-clicking on it.
    Once in the UI Builder, create a new selector so that we apply a new style when the mouse is over the label with
    Label:hover
    . This is really generic because it will apply to every label, so you could do something more specific as a selector.
    In that selector, you can modify a few properties, and activate the transition at the bottom by setting a duration other than 0 and specify on witch transition it applies


    3. Enjoy!
    For those wondering why there was a third label, it was created in the c# file and the stylesheet was not applied on it. I simply opened the file and removed the line creating the extra label.

    3 - Enjoy, remove the c# label.gif




    If you are really attentive, you can see that there is a transition when you put your mouse over the label, but the style just "snap" when you remove your mouse. This is because the transition property is added with the
    Label:hover
    selector. I did this to show the order in which the style is applied. We start by resolving the transition style, and then we look if there is any transition that need to be done. So if the transition is removed at the same time as other properties, there won't be any animation on these properties.

    To obtain a transition on both the "mouse start hovering" and "mouse stop hovering" the transition property should be set in a selector that do not change, in this case it could be on every Label. You have seen how to create a selector, so as homework, try to create this second selector with the transform. You can remove the style from the first selector by using a right click on it and select the unset option.

    Let us know if you have any questions!
     

    Attached Files:

  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    So this is wonderful for setting up transitions in uss and works pretty well but as far as c# goes, how would you hook into the transition events?

    Also if we wanted to animate in c# would we still use the experimental animation?
     
  3. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    They are going through the regular events in UI toolkit
    Code (CSharp):
    1. ve.RegisterCallback<TransitionRunEvent>(evt => elapsedTime = (float)evt.elapsedTime);
    2. ve.RegisterCallback<TransitionStartEvent>(evt => elapsedTime = (float)evt.elapsedTime);
    You can find more info in the API documentation for Unity 2021.2+ :

    TransitionEndEvent
    TransitionCancelEvent
    TransitionStartEvent
    TransitionRunEvent


    It depends on exactly what drives your animation. If it needs to be linked to your game state, I would probably not use the experimental animation, but if they are standalone UI animation, most of the time you could use the USS transition (fade, slide, expand) but if you need something more specific, then yes the experimental animation framework is always there.

    Transition are easy to setup from C# :
    Code (CSharp):
    1.             ve.style.backgroundColor = Color.green;
    2.             ve.style.transitionProperty = new List<StylePropertyName> { new StylePropertyName("background-color") };
    3.             ve.style.transitionDuration = new StyleList<TimeValue>(new List<TimeValue> { new TimeValue(1.0f, TimeUnit.Second) })
    Then you could trigger the transition by changing it on the next frame as Julia explains here
     
    Last edited: Jan 25, 2022
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Thanks for the reply @SimonDufour. That helps a lot, glad to see UITK making progress.

    Two more things:

    1st. A bit of a bug, I have a transition setup in uss that scales a menu when it is enabled or disabled. This works great except all the buttons I have as children of that menu have their size incorrect when it is scaled back up to normal size. Once I mouse over them they change back to normal style. Its like the size of the button is based on the text, which is almost default styling. It ignores most of my styling accept for the background image and text size that I can see. I'm using 2021.2.7f1 and UITK that is included.

    2nd. If I want to loop animations like pulsing, what would be the best way to do it. Right now I have the MonoBehaviour with the UIDocument on it controlling the elements in update.
     
  5. KamilCSPS

    KamilCSPS

    Joined:
    May 21, 2020
    Posts:
    448
    This might be of help;
    https://forum.unity.com/threads/how...inside-a-custom-control.1155488/#post-7413278
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I had that same thought to use the TransitionEnd to trigger the loop again. I'm probably going to stick with using regular old update and a sin wave function to pulse, the events seem a bit clunky and may lead to hiccups with timing. I'll do some testing and find out which would be better.
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    @SimonDufour Just so you know the bug that shrinks my buttons pretty much does it to everything I try to animate the scale of. All the controls get shrunk and ignore any style applied.
     
  8. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    575
    @Chris-Trueman Can you file a bug in the editor with a small repro project? This that would give us a starting point to reproduce the exact situation that you are encountering. I have not been able to get something similar on my side.
     
  9. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Once I have some time to set it up, for sure.
     
  10. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Due to another bug with TextFields not accepting input with a custom font asset, I setup a repro for both.
    Case # 1399150 for this bug.
     
    SimonDufour likes this.