1
votes

I am instantiating the leader board on the image (https://imgur.com/a/I0aVSaR), the problem is, I can't align properly the numbers on the center row.

I think that if I set the width of the numbers of the right row, the center row will be aligned. Right now the elements on the right row have different widths.

My problem now is how to set the width via script, I already tried rectransform.deltaSize, SetSizeWithCurrentAnchors and other similar things with no luck.

I've also tried to set a preferred width on the inspector on Layout Element but is ignored (https://imgur.com/a/O3hDeGN).

This is how the elements are created:

   for (int i = 0; i < leaderboardEntries.Count; i++) 
   {
        Transform newEntry = Instantiate(entryPrefab);
        newEntry.SetParent(leaderboardEntriesPanel);

        Text entryName = newEntry.GetChild(0).GetComponent<Text>();
        Text entryScore = newEntry.GetChild(1).GetComponent<Text>();
        Text entryRank = newEntry.GetChild(2).GetComponent<Text>();

        entryName.text = leaderboardEntries[i].getName();
        entryScore.text = leaderboardEntries[i].getScore().ToString();
        entryRank.text = (i+1).ToString();

        entryName.alignment = TextAnchor.UpperLeft;
        entryScore.alignment = TextAnchor.UpperRight;
        entryRank.alignment = TextAnchor.UpperRight;

    }

The center row is the score, the left row the Name and the right row the rank.

Thanks!

1
what is your problem exactly? do you want to know how to set the width of a gameobject or how to align your center row? - AmRzA
How exactly they all should be aligned? Like this i.stack.imgur.com/BSCwq.png ? - trollingchar
Yes, exactly!!! - Grow Animation
maybe the problem is related to this imgur.com/a/posnxWu - Grow Animation

1 Answers

0
votes

if you want to change the width and height of a RectTransform you can try this:

rectTransform.sizeDelta = new Vector2( width, height); 

but fixing their alignment need something else, first of all, all alignments should be same, UpperLeft or MiddleLeft for example. then set anchor point of all your entries to the same value also. then something like this should work properly:

// in this example I considered all anchor points are middle left and all 
// alignments are middle left too

float width = leaderboardEntriesPanel.GetComponent<RectTransform>().rect.width;
for (int i = 0; i < leaderboardEntries.Count; i++) 
   {
        Transform newEntry = Instantiate(entryPrefab);
        newEntry.SetParent(leaderboardEntriesPanel);

        Text entryName = newEntry.GetChild(0).GetComponent<Text>();
        Text entryScore = newEntry.GetChild(1).GetComponent<Text>();
        Text entryRank = newEntry.GetChild(2).GetComponent<Text>();

        entryName.text = leaderboardEntries[i].getName();
        entryScore.text = leaderboardEntries[i].getScore().ToString();
        entryRank.text = (i+1).ToString();

        entryName.GetComponent<RectTransform>().anchoredPosition = new Vector2(width / 15, entryName.GetComponent<RectTransform>().anchoredPosition.y);
        entryScore.GetComponent<RectTransform>().anchoredPosition = new Vector2(width / 2, entryScore.GetComponent<RectTransform>().anchoredPosition.y);
        entryRank.GetComponent<RectTransform>().anchoredPosition = new Vector2(width * 4 / 5, entryRank.GetComponent<RectTransform>().anchoredPosition.y);

    }