Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Transition not happening

Discussion in 'UI Toolkit' started by Songerk, Apr 29, 2024.

  1. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48

    The transition doesn't happened, it just going straight to the value, how can I fix it?
    I am changing the value with
    Code (CSharp):
    1. element.style.rotate = new StyleRotate(new Rotate(-rotation));
    and even with the debug, it's all seems correct
    upload_2024-4-29_17-55-55.png
     
    Last edited: Apr 29, 2024
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,011
    This should work. I would double-check that there isn't any conflict between multiple selectors having different transition values. If not, it might be worth filing a bug.
     
  3. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    what do you mean by "conflict between multiple selectors having different transition values"?
    do you mean different classes that have transitions of rotation on the element?
    if so I check all of them and none of them effect the rotation.
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,011
    Yes, or classes that define the transition-duration to something else. It doesn't seem to be the case according to the UI Toolkit debugger screenshot, though.

    It works on the latest Unity 6 beta, which version of Unity are you using?

    rotate-transition.gif
     
  5. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    126
    Hello! Are you setting you rotation on Start? If so, you need to wait for the first GeometryChange event, something like this:
    Code (CSharp):
    1.    
    2.     bool firstGeometryChanged = true;
    3.     void Start()
    4.     {
    5.         var button = document.rootVisualElement.Q("PlayButton");
    6.         button.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    7.     }
    8.  
    9.     private void OnGeometryChanged(GeometryChangedEvent evt)
    10.     {
    11.         if (firstGeometryChanged)
    12.         {
    13.             firstGeometryChanged = false;
    14.          
    15.             var button = document.rootVisualElement.Q("PlayButton");
    16.             button.style.rotate = new Rotate(45);
    17.         }
    18.     }
     
  6. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    I recently updated to 2022.3.26



    that actually worked, thank you.
    But I still has other problem, setting a new class with new translation and transition, than clicking the button, it just going straight to the value, the same problem as before, but this time I am adding it to a class.

     
  7. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    126
    @Songerk I think the problem here is that your transition should be defined before you modify the translation, not at the same time
     
  8. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    No, what I meant , the class already has this values modified in UI Builder, I am not changing them at runtime.
    at runtime, I am only adding the class to the element.
     
  9. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    126
    Right. When you click on play you are adding the .levelSelect class to the buttons. The problem is .levelSelect has both the transition and the new value for translate.

    You can try to have the transition in a class that is already added to the buttons before you add .levelSelect to them
     
  10. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    Tried that, didn't work.
    And about the problem that I had before, I set opacity on the event call of "GeometryChangedEvent" just like you told me, but it's still jump right to the end value.



    I Set it like
    Code (CSharp):
    1.     private void OnGeometryChanged(GeometryChangedEvent evt)
    2.     {
    3.         _mainScreen.UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    4.         _mainScreen.style.opacity = 100;
    5. }
     
  11. cpalma-unity

    cpalma-unity

    Unity Technologies

    Joined:
    Nov 30, 2020
    Posts:
    126
    It's difficult to know without context what could be the problem. The only thing I can think about is to make sure you don't have an inline style for the translation in your element, because the inline style will win over the selector value.

    The code for the opacity has one problem. Opacity is a 0..1 value. So it might be jumping to the final value because it goes from 0 to 100 instead of 0 to 1.
     
  12. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    What information do you need to understand the problem better?

    And about the second problem, thank you it works, kind of confusing because in the UI builder the opacity range is from 0 to 100.
     
  13. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    46
    Hi! Were you able to verify if there are any inline styles overriding the desired behaviour?

    If the problem still persists, would you mind sharing some of the code along with the UXML and USS being used? Thank you in advance!
     
  14. Songerk

    Songerk

    Joined:
    Mar 29, 2021
    Posts:
    48
    what do you mean by "inline styles overriding "?

    The script that use the UI Document:

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. using UnityEngine.UIElements;
    6.  
    7. public class MainMenuUiController : MonoBehaviour
    8. {
    9.     [SerializeField] private UIDocument _ui;
    10.     private VisualElement _mainScreen;
    11.  
    12.     private Button _startButton;
    13.     private Button _characterSelect;
    14.     private Button _deletSave;
    15.  
    16.    
    17.     public UnityEvent _startAction;
    18.     public UnityEvent _characterSelectButton;
    19.  
    20.  
    21.     private const string _deleteButtonName = "DeleteSave";
    22.     private const string _selectLevelName = "levelSelect";
    23.     private const string _mainScreenName = "Main_Screen";
    24.     void Start()
    25.     {
    26.         var root = _ui.rootVisualElement;
    27.         _mainScreen = root.Q<VisualElement>(_mainScreenName);
    28.  
    29.         SetVersion(root);
    30.  
    31.         SetButtons();
    32.  
    33.         _mainScreen.RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    34.  
    35.     }
    36.  
    37.     private void SetButtons()
    38.     {
    39.         _startButton = _mainScreen.Q<Button>("PlayButton");
    40.         _characterSelect = _mainScreen.Q<Button>("CharacterSelect");
    41.  
    42.         _characterSelect.clicked += SelectCharacter;
    43.         _startButton.clicked += StartPlay;
    44.  
    45.         _deletSave = _ui.rootVisualElement.Q<Button>(_deleteButtonName);
    46.         _deletSave.clicked += DeleteSaveFile;
    47.     }
    48.  
    49.     private void OnGeometryChanged(GeometryChangedEvent evt)
    50.     {
    51.         _mainScreen.UnregisterCallback<GeometryChangedEvent>(OnGeometryChanged);
    52.         _mainScreen.style.opacity = 1;
    53.  
    54.         RegisterCallback<TransitionEndEvent>(_startButton, SetRotationSequnce);
    55.         SetRotationBase(_startButton);
    56.         RegisterCallback<TransitionEndEvent>(_characterSelect, SetRotationSequnce);
    57.         SetRotationBase(_characterSelect);
    58.  
    59.     }
    60.     void StartPlay()
    61.     {
    62.  
    63.         PressedButton();
    64.         _startButton.RegisterCallback<TransitionEndEvent>((evt) => { _startAction?.Invoke(); });
    65.     }
    66.  
    67.     void SelectCharacter()
    68.     {
    69.  
    70.         PressedButton();
    71.         _characterSelect.RegisterCallback<TransitionEndEvent>((evt) => {
    72.             Debug.Log("Character Button Pressed");
    73.             _characterSelectButton?.Invoke(); });
    74.     }
    75.  
    76.     private void PressedButton()
    77.     {
    78.         ResetAnimation();
    79.         _characterSelect.ToggleInClassList(_selectLevelName);
    80.         _startButton.ToggleInClassList(_selectLevelName);
    81.     }
    82.  
    83.     private void ResetAnimation()
    84.     {
    85.         _startButton.style.transitionDuration = StyleKeyword.Initial;
    86.         _characterSelect.style.transitionDuration = StyleKeyword.Initial;
    87.     }
    88.  
    89.     public void RegisterCallback<TEvent>(VisualElement visualElement, Action<VisualElement> callback) where TEvent : EventBase<TEvent>, new()
    90.     {
    91.         EventCallback<TEvent> eventCallback = new EventCallback<TEvent>((evt) => callback(visualElement));
    92.         visualElement.RegisterCallback(eventCallback);
    93.     }
    94.     private void SetRotationBase(VisualElement element)
    95.     {
    96.         var rotation = element.resolvedStyle.rotate.angle.value;
    97.         element.style.rotate = new StyleRotate(new Rotate(-rotation));
    98.     }
    99.     private void SetRotationSequnce(VisualElement element)
    100.     {
    101.         var rotation = element.style.rotate.value.angle.value;
    102.         element.style.rotate = new StyleRotate(new Rotate(-rotation));
    103.     }
    104.  
    105.    
    106.  
    107.  
    108.     private void DeleteSaveFile()
    109.     {
    110.         string filePath = Path.Combine(Application.persistentDataPath, "ClothData.json");
    111.         if (File.Exists(filePath))
    112.         {
    113.             var lastName = _deletSave.text;
    114.             _deletSave.text = "Save Was Deleted";
    115.             File.Delete(filePath);
    116.             _deletSave.schedule.Execute(() =>
    117.             {
    118.                 _deletSave.text = lastName;
    119.             }).StartingIn(2500);
    120.         }
    121.         else
    122.         {
    123.             var lastName = _deletSave.text;
    124.             _deletSave.text = "There was No File";
    125.             _deletSave.schedule.Execute(() =>
    126.             {
    127.                 _deletSave.text = lastName;
    128.             }).StartingIn(2500);
    129.         }
    130.  
    131.     }
    132.     private void SetVersion(VisualElement root)
    133.     {
    134.         var label = root.Q<Label>("Version");
    135.         label.text = ($"Version: {Application.version}");
    136.     }
    137.  
    138. }
    139.  

    The UXML:

    Code (CSharp):
    1. <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
    2.     <Style src="project://database/Assets/_Project/Art/UI%20Tool%20Kit/StartMenu/Button.uss?fileID=7433441132597879392&amp;guid=0b3ab8fdc2652524e85f20825c933be9&amp;type=3#Button" />
    3.     <Style src="project://database/Assets/_Project/Art/UI%20Tool%20Kit/StartMenu/AspectRation.uss?fileID=7433441132597879392&amp;guid=fd1b298db0177dc41b07f8922d935b38&amp;type=3#AspectRation" />
    4.     <SafeAreaManager>
    5.         <ui:VisualElement name="Main_Screen" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); background-image: none; margin-left: 74px; margin-right: 74px; margin-top: 74px; margin-bottom: 74px; padding-left: 41px; padding-right: 41px; padding-top: 41px; padding-bottom: 41px; opacity: 0; transition-property: opacity; transition-duration: 4s; transition-timing-function: ease-out;">
    6.             <ui:VisualElement picking-mode="Ignore" name="Logo" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); height: 40%;">
    7.                 <ui:VisualElement style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); -unity-slice-left: 0; -unity-slice-top: 0; -unity-slice-right: 0; -unity-slice-bottom: 0; padding-left: 0; margin-left: 8%; margin-right: 8%; flex-shrink: 0; justify-content: center; align-items: center; margin-top: 0; padding-top: 0; margin-bottom: 0;">
    8.                     <ui:Label tabindex="-1" text="UntitleGame" parse-escape-sequences="true" display-tooltip-when-elided="true" style="font-size: 107px; -unity-font: url(&quot;project://database/Assets/_Project/Art/UI%20Tool%20Kit/UnityDefaultRuntimeTheme.tss?fileID=2230732570650464555&amp;guid=c04ea85137589f94ba90e30be9953adb&amp;type=3#NotInter-Regular&quot;); -unity-font-definition: url(&quot;project://database/Assets/_Project/Art/Font/LilitaOne-Regular%20SDF.asset?fileID=11400000&amp;guid=a81274d4161429c498209a90a46ca168&amp;type=2#LilitaOne-Regular SDF&quot;);" />
    9.                 </ui:VisualElement>
    10.             </ui:VisualElement>
    11.             <ui:VisualElement name="Buttons" picking-mode="Ignore" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); flex-shrink: 1; height: 60%;">
    12.                 <ui:VisualElement name="MainButtons" picking-mode="Ignore" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); height: 60%; margin-bottom: 94px; padding-top: 0; margin-left: 94px; margin-right: 94px; margin-top: 94px; -unity-slice-bottom: 0; width: auto; min-width: 225px; justify-content: center;">
    13.                     <ui:Button text="Play" display-tooltip-when-elided="true" name="PlayButton" view-data-key="PlayButton" class="menu-button" style="justify-content: flex-start; align-self: auto; align-items: stretch; max-height: none; min-height: 88px; font-size: 49px; -unity-font-style: normal; background-image: none; background-color: rgba(236, 230, 230, 0.44); -unity-font: initial; -unity-font-definition: url(&quot;project://database/Assets/_Project/Art/Font/LilitaOne-Regular%20SDF.asset?fileID=11400000&amp;guid=a81274d4161429c498209a90a46ca168&amp;type=2#LilitaOne-Regular SDF&quot;); -unity-text-align: lower-center; text-overflow: clip; letter-spacing: 0; rotate: 1deg;" />
    14.                     <ui:Button text="Character Select" display-tooltip-when-elided="true" name="CharacterSelect" view-data-key="CharacterSelect" class="menu-button" style="justify-content: flex-start; align-self: auto; align-items: stretch; max-height: none; min-height: 88px; font-size: 49px; -unity-font-style: normal; background-image: none; background-color: rgba(236, 230, 230, 0.44); -unity-font: initial; -unity-font-definition: url(&quot;project://database/Assets/_Project/Art/Font/LilitaOne-Regular%20SDF.asset?fileID=11400000&amp;guid=a81274d4161429c498209a90a46ca168&amp;type=2#LilitaOne-Regular SDF&quot;); -unity-text-align: lower-center; text-overflow: clip; letter-spacing: 0; rotate: -1deg;" />
    15.                 </ui:VisualElement>
    16.                 <ui:VisualElement name="VisualElement" style="flex-grow: 1; background-color: rgba(0, 0, 0, 0); height: 30%; padding-left: 0; padding-right: 0; padding-top: 0; padding-bottom: 0; justify-content: center; align-items: flex-start; flex-direction: column; margin-left: 20px; margin-right: 20px; margin-top: 20px; margin-bottom: 20px;" />
    17.             </ui:VisualElement>
    18.         </ui:VisualElement>
    19.         <ui:Button text="Delete Save" parse-escape-sequences="true" display-tooltip-when-elided="true" name="DeleteSave" style="position: absolute; bottom: 5%; left: 5%; flex-grow: 1; width: auto; -unity-font-definition: url(&quot;project://database/Assets/_Project/Art/Font/LilitaOne-Regular%20SDF.asset?fileID=11400000&amp;guid=a81274d4161429c498209a90a46ca168&amp;type=2#LilitaOne-Regular SDF&quot;); min-width: 200px; font-size: 30px;" />
    20.         <ui:Label tabindex="-1" text="Label" parse-escape-sequences="true" display-tooltip-when-elided="true" name="Version" style="position: absolute; bottom: 5%; right: 5%; font-size: 36px;" />
    21.     </SafeAreaManager>
    22. </ui:UXML>
    23.  
    The USS:


    Code (CSharp):
    1. .CircleButton {
    2.     align-self: flex-start;
    3.     border-top-left-radius: 86px;
    4.     border-bottom-left-radius: 86px;
    5.     border-top-right-radius: 86px;
    6.     border-bottom-right-radius: 86px;
    7.     border-left-width: 0;
    8.     padding-left: 9px;
    9.     padding-right: 9px;
    10.     padding-top: 9px;
    11.     padding-bottom: 9px;
    12.     margin-left: 12px;
    13.     min-width: 180px;
    14.     min-height: 180px;
    15. }
    16.  
    17. .menu-button {
    18.     margin-top: 25px;
    19.     margin-right: 25px;
    20.     margin-bottom: 25px;
    21.     margin-left: 25px;
    22.     border-bottom-color: rgba(148, 148, 148, 0.65);
    23.     border-top-left-radius: 29px;
    24.     border-top-right-radius: 29px;
    25.     border-bottom-right-radius: 29px;
    26.     border-bottom-left-radius: 29px;
    27.     border-top-width: 12px;
    28.     border-right-width: 12px;
    29.     border-bottom-width: 16px;
    30.     border-left-width: 12px;
    31.     border-top-color: rgba(255, 255, 255, 0);
    32.     border-left-color: rgba(255, 255, 255, 0);
    33.     border-right-color: rgba(255, 255, 255, 0);
    34.     transition-property: rotate;
    35.     transition-duration: 2s;
    36.     transition-timing-function: ease-in-out;
    37.     transition-delay: 0s;
    38. }
    39.  
    40. .menu-button:active {
    41.     transition-duration: 0.2s, 0.1s;
    42.     transition-property: scale, font-size;
    43.     transition-timing-function: ease-out-bounce, ease;
    44.     scale: 0.95 0.95;
    45.     transition-delay: 0s, 0s;
    46.     font-size: 50px;
    47.     border-top-width: 0;
    48.     border-right-width: 0;
    49.     border-bottom-width: 0;
    50.     border-left-width: 0;
    51. }
    52.  
    53. .cornerButton {
    54.     border-top-width: 2px;
    55.     border-right-width: 2px;
    56.     border-bottom-width: 2px;
    57.     border-left-width: 2px;
    58.     border-top-left-radius: 150px;
    59.     border-top-right-radius: 150px;
    60.     border-bottom-right-radius: 150px;
    61.     border-bottom-left-radius: 150px;
    62.     width: 150px;
    63.     scale: 1 1;
    64.     height: 150px;
    65.     margin-top: 32px;
    66.     margin-right: 32px;
    67.     margin-bottom: 32px;
    68.     margin-left: 32px;
    69.     background-color: rgba(0, 0, 0, 0.18);
    70.     font-size: 28px;
    71. }
    72.  
    73. .cornerButton:active {
    74.     scale: 0.8 0.8;
    75. }
    76.  
    77. .vanish {
    78.     transition-duration: 0.2s;
    79.     opacity: 0;
    80.     display: flex;
    81.     visibility: visible;
    82. }
    83.  
    84. .highlight {
    85.     border-left-color: rgba(0, 97, 255, 0.57);
    86.     border-right-color: rgba(0, 97, 255, 0.57);
    87.     border-top-color: rgba(0, 97, 255, 0.57);
    88.     border-bottom-color: rgba(0, 97, 255, 0.57);
    89.     border-top-width: 13px;
    90.     border-right-width: 13px;
    91.     border-bottom-width: 13px;
    92.     border-left-width: 13px;
    93.     border-top-left-radius: 44px;
    94.     border-top-right-radius: 44px;
    95.     border-bottom-right-radius: 44px;
    96.     border-bottom-left-radius: 44px;
    97.     background-color: rgba(53, 143, 255, 0.07);
    98.     opacity: 1;
    99.     transition-duration: 1s;
    100.     transition-timing-function: ease-in-bounce;
    101. }
    102.  
    103. .levelSelect {
    104.     translate: 0 200%;
    105.     display: flex;
    106.     transition-property: translate;
    107.     transition-duration: 20s;
    108.     transition-timing-function: ease-in;
    109.     transition-delay: 0s;
    110. }
    111.