Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Aligning text in leaderboard - ideas? Scrap or keep?

Discussion in 'UGUI & TextMesh Pro' started by tvance929, Jan 30, 2016.

  1. tvance929

    tvance929

    Joined:
    May 15, 2015
    Posts:
    14
    Im trying to create a leaderboard for my game. I am looping through a list of leaders and want to align names on the left and scores on the right.

    I plan on limiting names to only 5 letters -- so my code of 3 tabs would work ( see the image - toddv and 24 look fine ) but this just seems terribly hacky and brutal and would probably break on different screen sizes.

    I'm willing to go a whole different route, but just would love some suggestions. (hackish code below)

    Code (CSharp):
    1.  foreach (Score score in highScoresList)
    2.              {
    3.                  theBoard.text += score.playerName + "\\t\\t\\t" + score.score + "\\n ";
    4.              }
    5.              theBoard.text = theBoard.text.Replace("\\n", "\n");
    6.              theBoard.text = theBoard.text.Replace("\\t", "\t");
     

    Attached Files:

  2. Trav3l3r

    Trav3l3r

    Joined:
    Sep 16, 2014
    Posts:
    58
    Are you using the new Unity GUI? If so, you can take advantage of the alignment properties on the Text component. Create a column for the names and another for the scores. Align each to your needs and that's pretty much it.
     

    Attached Files:

    tvance929 likes this.
  3. tvance929

    tvance929

    Joined:
    May 15, 2015
    Posts:
    14
    Ah ha... I'm assuming you are suggesting that I use extra text boxes as columns (and it looks like it will work) - I'm going to use two extra text boxes under CONTENT in the scroll rect... that should work!
     
  4. tvance929

    tvance929

    Joined:
    May 15, 2015
    Posts:
    14
    Fantastic! Thanks Trav3l3r!!! ( So do you know how to force it to scroll to the top from code? :) )

    Code (CSharp):
    1. var scrollView = GameObject.FindGameObjectsWithTag("ScrollView");
    2.         foreach (var scrollRect in scrollView)
    3.         {
    4.             scrollRect.GetComponent<ScrollRect>().velocity = new Vector2(0f, -3000f);          
    5.         }
    upload_2016-1-30_14-1-52.png
    upload_2016-1-30_14-0-39.png
     
    Last edited: Jan 30, 2016
  5. Trav3l3r

    Trav3l3r

    Joined:
    Sep 16, 2014
    Posts:
    58
    Glad I was able to help :)

    I believe you can simply translate the Content object to the desired location
    something along the lines of (from the top of my head)
    Code (CSharp):
    1. void FixedUpdate(){
    2. //content is a public var pointing to your Content object
    3. //target is a public var pointing to a target destination objec. Or it could also be a Vector3  
    4. content.position = Vector3.MoveTowards(content.position, target.position, speed * Time.deltaTime);
    5. }
     
    tvance929 likes this.
unityunity