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

Question Add position changing animation for VisualElements sorting

Discussion in 'UI Toolkit' started by MadnessAres_0209, Jun 8, 2022.

  1. MadnessAres_0209

    MadnessAres_0209

    Joined:
    Oct 25, 2021
    Posts:
    2
    I am creating a runtime Scoreboard that can sort the order while score changes.
    Is it possible to add a fly animation to the sorting order process.

    Code (CSharp):
    1. public void Sort()
    2.     {
    3.         freeforallPanel.Sort((a, b) =>
    4.         {          
    5.             if (int.Parse((a[1][0]as Label).text)> int.Parse((b[1][0]as Label).text))
    6.             {
    7.                 return -1;
    8.             }
    9.             else if (int.Parse((a[1][0] as Label).text) == int.Parse((b[1][0] as Label).text))
    10.             {
    11.                 return 0;
    12.             }
    13.             else if (int.Parse((a[1][0] as Label).text) < int.Parse((b[1][0] as Label).text))
    14.             {
    15.                 return 1;
    16.             }
    17.             return 0;
    18.         });
    19.     }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Just a comment on efficiency: consider that int.Parse is an expensive operation. Do it only once up front. DRY principle.

    And a comment on readability: implement the above, and you‘ll notice it‘ll be more concise code that‘s easier to grasp too. ;)

    Finally, you can simply do:

    Code (CSharp):
    1. var aText = (text of label a);
    2. var bText = (text of label b);
    3. return aText.Compare(bText);
    as far as I understood it‘s just a regular string compare once you get each label‘s text.
     
    Last edited: Jun 8, 2022
  3. MadnessAres_0209

    MadnessAres_0209

    Joined:
    Oct 25, 2021
    Posts:
    2
    thx, it helps.
    Any idea for the adding animations?