Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How do I automatically trigger a transition at runtime?

Discussion in 'UI Toolkit' started by Gronade, Jan 22, 2022.

  1. Gronade

    Gronade

    Joined:
    Feb 14, 2020
    Posts:
    5
    I'm trying to animate a dialog box so it pops up from the bottom of the screen. I know about selectors like :hover but I want the transition to occur without any input from the player. How would I do this?
     
  2. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    117
    You can add a new class (like opened-dialog-box) when you want to show it. And make this dialog box's initial position at the bottom. And also don't forget to add transition properties in it's USS code (a simple "transition-duration: 0.5s;" is enough to animate it's property changes). And lastly, define openec-dialog-box's position where you want it to be in USS.
     
    floriankorsakissok likes this.
  3. Gronade

    Gronade

    Joined:
    Feb 14, 2020
    Posts:
    5
    I may have misunderstood because it doesn't seem to work. It goes to where its supposed to be on screen but it happens instantly.

    Here are my USS classes:
    Code (CSharp):
    1. .main-container-hidden {
    2.     flex-grow: 1;
    3.     align-items: flex-end;
    4.     justify-content: center;
    5.     flex-direction: row-reverse;
    6.     padding-bottom: 10px;
    7.     opacity: 0;
    8.     transition-duration: 2s;
    9.     left: -300px;
    10. }
    11.  
    12. .main-container-revealed {
    13.     left: 0px;
    14.     opacity: 1;
    15.     transition-duration: 2s;
    16.     transition-property: all;
    17. }
    And the code I use to change the class:
    Code (CSharp):
    1. mainContainer.ToggleInClassList("main-container-revealed");
     
  4. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    You have to wait for the initial layout to be done before you set the transition class! You do that by listening for the
    GeometryChangedEvent
    (use
    RegisterCallback
    , and don't forget to
    UnregisterCallback
    afterwards most probably).

    Hope this helps!
     
    floriankorsakissok likes this.
  5. Laogon09

    Laogon09

    Joined:
    Feb 12, 2018
    Posts:
    3
    How would you go about Unregistering the callback afterward? Is there a way to unregister all callbacks? I am not sure where in code you would need to be to unregister, since you can't do it from within the callback itself.
     
  6. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    You can do it from within the callback itself, have you ran into trouble when doing that?
     
  7. Laogon09

    Laogon09

    Joined:
    Feb 12, 2018
    Posts:
    3
    Yes, if I do something like this:

    Code (CSharp):
    1.        EventCallback<GeometryChangedEvent> evt = (GeometryChangedEvent e) =>
    2.         {
    3.             // some code
    4.             popup.UnregisterCallback(evt);
    5.         };
    evt says it is unassigned. Maybe I am going about this the wrong way?

    Note: here, popup would be the VisualElement I register the callback to right after this.
     
  8. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    This is how I usually do it:

    Code (CSharp):
    1.  
    2.   private void SomeInitializationMethod()
    3.   {
    4.     popup.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    5.   }
    6.  
    7.   private void OnGeometryChanged(GeometryChangedEvent evt)
    8.   {
    9.     popup.UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    10.  
    11.     // Do the things I need to do...
    12.   }
     
    Kirsche likes this.
  9. Laogon09

    Laogon09

    Joined:
    Feb 12, 2018
    Posts:
    3
    This works, thank you!
     
    JuliaP_Unity likes this.
  10. RomainTirbisch

    RomainTirbisch

    Joined:
    Sep 29, 2021
    Posts:
    1
    Hello !

    I tried your solution, it works the first time. But if I detatch my visual from the panel and then attatch it again then the geometry changed event is not trigger anymore.
    Is there a way to force trigger this event ? To now when visuals are listening for transition ?
     
  11. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    My guess is your layout didn't change, which I would suppose it would but apparently it's not. You can try the AttachToPanelEvent if you want to do it again when it's reattached to a new panel (though this will also happen the first time around, so make sure you don't subscribe to it until you're ready to detach from the panel probably).