docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Integrate Steamworks features

    Call Steamworks features from your C# scripts and get their results.

    After you initialize the Steamworks API, call a feature, such as achievements, leaderboards, or friends lists, through the static class that matches its Steamworks interface, from either a client application or a game server. The methods keep the names and signatures from the Steamworks API reference. For example, SteamUser corresponds to ISteamUser.

    Prerequisites

    • Initialize the Steamworks API. For more information, refer to Initialize and shut down the Steamworks API.
    • Configure the feature on the Steamworks partner website if the feature requires it. For example, you must define achievements, stats, leaderboards, and inventory before you call them in your code. For more information, refer to the interface page for the features in Steamworks API reference.

    Call a Steamworks feature from a client application

    To call a Steamworks feature:

    1. In the Steamworks API reference, find the interface for the feature you want to call, for example, ISteamUserStats.
    2. Call a C# method on the corresponding class with the same name, SteamUserStats.
    3. Receive the data from the Steamworks API, depending on the method:
      1. If the method returns a SteamAPICall_t handle, call its Async version instead, and await the result. For example, SteamUserStats.GetNumberOfCurrentPlayers returns a SteamAPICall_t handle, so you call SteamUserStats.GetNumberOfCurrentPlayersAsync and await the result. For methods with no Async version, use SteamCallbacks.CallResultAsync.
      2. If the method triggers a callback, subscribe to it with SteamCallbacks.Subscribe. For example, SteamUserStats.StoreStats triggers the UserStatsStored_t callback. It also triggers the UserAchievementStored_t callback once for each newly unlocked achievement, so you subscribe to both.

    You also subscribe to events that your code didn't cause, such as PersonaStateChange_t when a friend's status changes. For more information, refer to Handle callbacks and call results.

    The following code example calls two Steamworks features through the SteamFriends and SteamUserStats classes. SteamFriends.GetPersonaName returns the signed-in user's name. SteamUserStats.GetNumberOfCurrentPlayersAsync() starts the request and returns a Task, so the script awaits it.

    using UnityEngine;
    using Unity.Steamworks.Interop;
    
    public class CurrentPlayers : MonoBehaviour
    {
        async void Start()
        {
            if (!SteamAPI.Init())
            {
                Debug.LogError("Steam is not running.");
                return;
            }
    
            Debug.Log($"Signed in as {SteamFriends.GetPersonaName()}");
    
            // Asynchronous call: await its call result.
            var result = await SteamUserStats.GetNumberOfCurrentPlayersAsync();
    
            if (result.m_bSuccess != 0)
                Debug.Log($"{result.m_cPlayers} players are in this game right now.");
            else
                Debug.LogWarning("Steam couldn't get the number of players.");
        }
    
        void OnDestroy()
        {
            SteamAPI.Shutdown();
        }
    }
    

    Synchronous methods return the same values as the Steamworks API, such as a bool, an EResult, or an invalid handle. The package doesn't add error codes or exceptions. For failures when you await a call result, refer to Await call results.

    Call a Steamworks feature from a game server

    To call a feature from a game server, follow the same steps as for a client application with the following differences:

    • Initialize the Steamworks API with GameServer.InitEx instead of SteamAPI.Init.
    • Call the SteamGameServer classes, such as SteamGameServerStats and SteamGameServerUtils, instead of the client classes.
    • Pass gameServer: true to SteamCallbacks.Subscribe and SteamCallbacks.CallResultAsync. Without this argument both methods receive only client messages, not server messages.

    For more information, refer to Initialize and shut down the Steamworks API and Handle callbacks and call results.

    Additional resources

    • Configure your Unity project for the Steamworks API
    • Initialize and shut down the Steamworks API
    • Callback delivery
    • Steam Game Servers (Valve)
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)