リーダーボードの UI を準備する - 全期間のリーダーボード - (Unity モジュール)
What's on the menu
In this tutorial, you will learn how to prepare the leaderboard user interface (UI) to display the leaderboard as a ranking list using the AccelByte Gaming Services (AGS) Leaderboard service.
The Leaderboard Menu UI has been created for you, but they still need additional code before you can connect them with the AGS Game SDK. These prepared files are available in the Resources section and consist of the following:
LeaderboardSelectionMenuCanvas_Starter
: A UI menu that displays all available leaderboard lists.- CS file:
/Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardSelectionMenu_Starter.cs
- Prefab file:
/Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardSelectionMenuCanvas_Starter.prefab
- CS file:
LeaderboardCycleMenuCanvas_Starter
: A UI menu that displays all available leaderboard cycle options.- CS file:
/Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardCycleMenu_Starter.cs
- Prefab file:
/Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardCycleMenuCanvas_Starter.prefab
- CS file:
LeaderboardMenuCanvas_Starter
: A UI menu that displays the ranking list of the chosen leaderboard.- CS file:
/Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardMenu_Starter.cs
- Prefab file:
/Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardMenuCanvas_Starter.prefab
- CS file:
RankingEntryPanel
: A UI component to hold the ranking information.- CS file:
/Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/Components/RankingEntryPanel.cs
- Prefab file:
/Assets/Resources/Modules/LeaderboardEssentials/Prefabs/Components/RankingEntryPanel.prefab
- CS file:
LeaderboardItemButton
: A UI component that is used for the leaderboard options button.- Prefab file:
/Assets/Resources/Modules/LeaderboardEssentials/Prefabs/Components/LeaderboardItemButton.prefab
- Prefab file:
Leaderboard Selection Menu
The Leaderboard Menu displays the leaderboard list based on the query result from the AGS Leaderboard server in the form of buttons using the LeaderboardItemButton
prefab. This menu is accessible from the Main Menu and its menu button is already exposed in MainMenuCanvas.prefab
.
The following is already included in LeaderboardSelectionMenu_Starter.cs
:
The declarations of the UI references that are ready to use:
[SerializeField] private Transform leaderboardListPanel;
[SerializeField] private Transform noEntryPanel;
[SerializeField] private Transform loadingPanel;
[SerializeField] private Transform failedPanel;
[SerializeField] private Button backButton;
[SerializeField] private GameObject leaderboardItemButtonPrefab;The getter/setter to switch the menu states. The states are: default, loading, and failed.
private LeaderboardSelectionView CurrentView
{
get => currentView;
set
{
leaderboardListPanel.gameObject.SetActive(value == LeaderboardSelectionView.Default);
noEntryPanel.gameObject.SetActive(value == LeaderboardSelectionView.Empty);
loadingPanel.gameObject.SetActive(value == LeaderboardSelectionView.Loading);
failedPanel.gameObject.SetActive(value == LeaderboardSelectionView.Failed);
currentView = value;
}
}The function named
OnBackButtonClicked()
that listens to thebackButton
onClick
event and redirects the player to the previous menu with theMenuManager
:private void OnBackButtonClicked()
{
MenuManager.Instance.OnBackPressed();
}
Here's the preview of the Leaderboard Selection Menu:
Leaderboard Cycle Menu
A leaderboard can span over different cycles, e.g., All Time
or Weekly
. The Leaderboard Cycle Menu displays all cycle types available for the leaderboard as a button using the LeaderboardItemButton
prefab, which will then direct to the actual leaderboard. In this module, you will only use the All Time
type for a basic implementation.
The following are already included in LeaderboardCycleMenu_Starter.cs
:
The declarations of the UI references that are ready to use:
[SerializeField] private Button allTimeButton;
[SerializeField] private Button backButton;
[SerializeField] private Transform leaderboardListPanel;
[SerializeField] private Transform loadingPanel;
[SerializeField] private Transform loadingFailed;
[SerializeField] private GameObject leaderboardItemButtonPrefab;The getter/setter to switch the menu states. The states are: default loading, and failed.
public LeaderboardCycleView CurrentView
{
get => currentView;
set
{
leaderboardListPanel.gameObject.SetActive(value == LeaderboardCycleView.Default);
loadingPanel.gameObject.SetActive(value == LeaderboardCycleView.Loading);
loadingFailed.gameObject.SetActive(value == LeaderboardCycleView.Failed);
currentView = value;
}
}The function named
OnBackButtonClicked()
that listens to thebackButton
onClick
event and redirects the player to the previous menu withMenuManager
:private void OnBackButtonClicked()
{
MenuManager.Instance.OnBackPressed();
}
Here's the preview of the Leaderboard Cycle Menu:
Leaderboard Menu
The Leaderboard Menu displays the ranking list of the desired leaderboard queried from AGS. It also displays the current player ranking in the leaderboard if the user is not listed in top 10 players. Each ranking info displayed uses the RankingEntryPanel
prefab that contains the player name and their highest score in the leaderboard.
The following are already included in LeaderboardMenu_Starter.cs
:
The declarations of the UI references that are ready to use:
[SerializeField] private Transform rankingListPanel;
[SerializeField] private Button backButton;
[SerializeField] private GameObject rankingEntryPanelPrefab;
[SerializeField] private RankingEntryPanel userRankingPanel;
[SerializeField] private Transform resultPanel;
[SerializeField] private Transform emptyPanel;
[SerializeField] private Transform loadingPanel;
[SerializeField] private Transform loadingFailed;The getter/setter to switch the menu states. The states are: default, loading, empty, and failed.
public LeaderboardMenuView CurrentView
{
get => currentView;
set
{
resultPanel.gameObject.SetActive(value == LeaderboardMenuView.Default);
emptyPanel.gameObject.SetActive(value == LeaderboardMenuView.Empty);
loadingPanel.gameObject.SetActive(value == LeaderboardMenuView.Loading);
loadingFailed.gameObject.SetActive(value == LeaderboardMenuView.Failed);
currentView = value;
}
}The function named
OnBackButtonClicked()
that listens to thebackButton
onClick
event and redirects the player to the previous menu withMenuManager
:private void OnBackButtonClicked()
{
MenuManager.Instance.OnBackPressed();
}
Here's the preview of the Leaderboard Menu:
Ranking Entry Panel
The Ranking Entry Panel is a prepared script for handling the Ranking Entry UI. This panel displays the player ranking information such as the rank number, display name, and rank score of a specific user. This prefab is mainly used for displaying all the rankings of the leaderboard and the current user ranking for that leaderboard.
The following are already included in RankingEntryPanel.cs
:
The declarations for the UI references that are ready to use:
[SerializeField] private Image prefabImage;
[SerializeField] private TMP_Text playerRankText;
[SerializeField] private TMP_Text playerNameText;
[SerializeField] private TMP_Text playerScoreText;A constant variable to hold the default prefix for the player's display name:
private const string DefaultDisplayNamePrefix = "PLAYER-";
A function to reset the all the UI text:
public void ResetRankingEntry()
{
playerRankText.text = "";
playerNameText.text = "You Have No Ranking Yet";
playerScoreText.text = "0";
}A function to modify all the text values within the entry panel:
public void SetRankingDetails(string userId, int playerRank, string playerName, float playerScore)
{
// If display name is null or empty, set to default format: "PLAYER-<<5 char of userId>>"
if (string.IsNullOrEmpty(playerName)) playerName = $"{DefaultDisplayNamePrefix}{userId[..5]}";
playerRankText.text = $"#{playerRank}";
playerNameText.text = playerName;
playerScoreText.text = $"{playerScore}";
}A function to modify the entry panel's color:
public void SetPanelColor(Color color)
{
prefabImage.color = color;
}
Here's the preview of the Ranking Entry panel:
Ready the UI
In this section, you are going to prepare the UI for the AGS Game SDK Leaderboard integration.
Leaderboard Selection Menu UI
Open
LeaderboardSelectionMenu_Starter.cs
.Define a public static variable to store the chosen leaderboard code. Each leaderboard has its own leaderboard code, so this leaderboard code will be required for getting the rank list from other menu.
public static string chosenLeaderboardCode;
Since you will display the leaderboard list in buttons, create a new function for the button callback to change to the next menu and store the chosen leaderboard code to the public
chosenLeaderboardCode
. Later on, this function will be bound to the generated buttons.private void ChangeToLeaderboardCycleMenu(string newLeaderboardCode)
{
chosenLeaderboardCode = newLeaderboardCode;
MenuManager.Instance.ChangeToMenu(AssetEnum.LeaderboardCycleMenuCanvas_Starter);
}
Leaderboard Cycle Menu UI
Open
LeaderboardCycleMenu_Starter.cs
.Define the
LeaderboardCycleType
enum for the leaderboard cycle type you want to support. Later, when you query the leaderboard, you will need to specify the cycle type you want. In this module, you only need to defineAllTime
.public static LeaderboardCycleType chosenCycleType;
public enum LeaderboardCycleType
{
AllTime
}Next, declare the helper variable to store the cycle ID.
public static string chosenCycleId;
Create a function to store the chosen cycle type and direct to the Leaderboard Menu.
public static void ChangeToLeaderboardMenu(LeaderboardCycleType cycleType, string cycleId)
{
chosenCycleType = cycleType;
chosenCycleId = cycleId;
MenuManager.Instance.ChangeToMenu(AssetEnum.LeaderboardMenuCanvas_Starter);
}In the
Start()
function, addChangeToLeaderboardMenu()
to listen to the all-time buttononClick
event.private void Start()
{
backButton.onClick.AddListener(OnBackButtonClicked);
allTimeButton.onClick.AddListener(() => ChangeToLeaderboardMenu(LeaderboardCycleType.AllTime, string.Empty));
}In the
Enable()
function, add the code below to make sure the default state is shown when the leaderboard cycle menu is opened.private void OnEnable()
{
leaderboardListPanel.DestroyAllChildren(allTimeButton.transform);
CurrentView = LeaderboardCycleView.Default;
}
Enable starter mode
In the Unity Editor, open the
LeaderboardEssentialsAssetConfig
in the Inspector. You can find this file inAssets/Resources/Modules/LeaderboardEssentials/LeaderboardEssentialsAssetConfig.asset
.In the Inspector, below the
Tutorial Module Starter
category, make sure theIs Starter Active
is ticked.
Resources
- GitHub link to the files in the Unity Byte Wars repository:
- Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardSelectionMenu_Starter.cs
- Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardSelectionMenuCanvas_Starter.prefab
- Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardCycleMenu_Starter.cs
- Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardCycleMenuCanvas_Starter.prefab
- Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/LeaderboardMenu_Starter.cs
- Assets/Resources/Modules/LeaderboardEssentials/Prefabs/LeaderboardMenuCanvas_Starter.prefab
- Assets/Resources/Modules/LeaderboardEssentials/Scripts/UI/Components/RankingEntryPanel.cs
- Assets/Resources/Modules/LeaderboardEssentials/Prefabs/Components/RankingEntryPanel.prefab
- Assets/Resources/Modules/LeaderboardEssentials/Prefabs/Components/LeaderboardItemButton.prefab
- Assets/Resources/Modules/LeaderboardEssentials/LeaderboardEssentialsAssetConfig.asset