すべてを統合する - 統計データを追跡し表示する - (Unity モジュール)
Connect the UI to Display Player Statistics
Open the
StatsProfileMenu_Starter
class and define a variable to store the wrapper you created.private StatsEssentialsWrapper_Starter statsWrapper;
Next, replace the
OnEnable()
function with the code below. It initializes the wrapper and stores the list of statistics codes based on the models from theStatsEssentialsModels
class, along with the target GameObject panels, into thestatsPanelList
. You can then usestatsPanelList
later to display the statistic value entries.void OnEnable()
{
statsWrapper ??= TutorialModuleManager.Instance.GetModuleClass<StatsEssentialsWrapper_Starter>();
statsPanelList ??= new()
{
(SinglePlayerStatsData, singlePlayerStatsPanel),
(EliminationStatsData, eliminationPlayerStatsPanel),
(TeamDeathmatchStatsData, teamDeathmatchPlayerStatsPanel)
};
if (statsWrapper != null)
{
DisplayStats();
}
}Now, replace the
DisplayStats()
function with the code below. It collects the statistics codes from thestatsPanelList
and sends a request to retrieve the statistic values. Once completed, it calls the callback function.private void DisplayStats()
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
string[] statCodes = statsPanelList.SelectMany(x => x.statData.StatsCodes).ToArray();
statsWrapper.GetUserStatsFromClient(statCodes, null, OnGetUserStatsCompleted);
}Next, create the callback function to generate the statistic value entries. The entries are initialized in groups based on game modes using the panels and data from the
statsPanelList
.private void OnGetUserStatsCompleted(Result<PagedStatItems> result)
{
if (result.IsError)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
return;
}
if (result.Value.data.Length <= 0)
{
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Empty);
return;
}
// Generate entries to display statistics in groups.
StatItem[] statItems = result.Value.data;
statsPanelList.Select(x => x.panel).ToList().ForEach(x => x.DestroyAllChildren());
foreach ((GameStatsData statData, Transform panel) statsPanel in statsPanelList)
{
foreach (GameStatsData.GameStatsModel model in statsPanel.statData.StatsModels)
{
StatItem statItem = statItems.FirstOrDefault(x => x.statCode == model.StatCode);
StatsProfileEntry entry = Instantiate(statsEntryPrefab, statsPanel.panel).GetComponent<StatsProfileEntry>();
entry.Setup(model.DisplayName, statItem != null ? (int)statItem.value : 0);
}
}
widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Not_Empty);
}Finally, compile your project and make sure there are no errors.
Resources
The files used in this tutorial are available in the Unity Byte Wars GitHub repository.