Skip to main content

Put it all together - Stat tracking and display - (Unity module)

Last updated on June 23, 2025

Connect the UI to Display Player Statistics

  1. Open the StatsProfileMenu_Starter class and define a variable to store the wrapper you created.

    private StatsEssentialsWrapper_Starter statsWrapper;
  2. 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 the StatsEssentialsModels class, along with the target GameObject panels, into the statsPanelList. You can then use statsPanelList 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();
    }
    }
  3. Now, replace the DisplayStats() function with the code below. It collects the statistics codes from the statsPanelList 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);
    }
  4. 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);
    }
  5. Finally, compile your project and make sure there are no errors.

Resources