- Home /
Add UI Panel as child of anothe UI Panel on Canvas
Hello.
I'm developing a Leader board for my game, I'm using Unity 5 and the new UI system, so I have a Canvas that contains panels, and a Prefab which is a Panel that has 3 Text inside a child panel, this is the screenshot:
I get my Leader board list from the cloud, and I want to show the player names on the Leaderboard, so I have this code that get the list, create a prefab and show the data:
public void GetLeaderboard ()
{
Debug.Log ("Getting Leaderboard...");
for (int i = 0; i< entries.Count; i++) {
Destroy (entries [i]);
}
entries.Clear ();
new LeaderboardDataRequest_highScoreLeaderboard ().SetEntryCount (10).Send ((response) => {
foreach (var entry in response.Data) {
AddLeaderboardEntry (entry.Rank.ToString (),
entry.UserName.ToString (),
entry.GetNumberValue ("score").ToString ());
}
});
}
void AddLeaderboardEntry (string userRanking, string userName, string userScore)
{
Debug.Log (userRanking + "-" + userName + "-" + userScore);
GameObject boardEntry = (GameObject)Instantiate (availableLeaderboardEntry);
boardEntry.transform.SetParent (leaderboardPanel.transform);
boardEntry.transform.position = new Vector3 (50, lastLeaderboardEntryY, 0);
lastLeaderboardEntryY -= 30;
boardEntry.GetComponent<LeaderBoardEntry> ().rankString = userRanking;
boardEntry.GetComponent<LeaderBoardEntry> ().usernameString = userName;
boardEntry.GetComponent<LeaderBoardEntry> ().scoreString = userScore;
}
It works perfectly, but when creating the board entries, it shows them completely out of the screen and this is the result:
The entries are set as children of the LeaderBoard Panel, as they are on the Unity Editor, if I drag them from my prefabs folder to the Hierarchy panel by hand, they are shown perfectly, I've search a lot but didn't find any help on how to fix it. Maybe some of you can give me a clue on how to do what I want.
Try changing the X value and width in the Rect transform. Panel is flipped to the negative side change values.
Your answer
Follow this Question
Related Questions
GameCenter Leaderboar and Achievements at Once... 2 Answers
Leaderboard GUISkin? 1 Answer
adding scores and creating a leaderboard 1 Answer
ammo count help 1 Answer