Documentation

Did you find this helpful?

Getting Started with Steam, Steamworks and Unity

This example shows how to log into PlayFab using Steam through SteamWorks and Unity

Prerequisites:

Setting up PlayFab title

To enable support for Steam authorization, PlayFab requires you to enable Steam add-on. On your title Game Manager page navigate to "Add-ons" tab (1). In the list of available addons, locate Steam and click the title (2):

Enter your AppId (1), Web API Key (2) and click "Install Steam" (3)

This concludes setting up PlayFab title for Steam integration.

Setting up Unity Project

We start by downloading the latest release of Steamworks.NET from Releases page. Get Unity Package version of the release and import it into the project. One you import the package, close unity and navigate to project root folder. Locate "steam_appid.txt" file. Open it and replace AppId with your own:

Open Unity back and create a new scene. Inside that scene create new object called "Steam". 

  • Add "SteamManager" component to this gameobject. This component is part of Steamworks.Net
  • Create and Add "SteamScript" component to this gameobject. 

The code for "SteamScript" component is exposed below (Thanks to Dylan Hunt for the following example):

// Import all the necessary namespaces
using System;
using System.Text;
using PlayFab;
using PlayFab.ClientModels;
using Steamworks;
using UnityEngine;

public class SteamScript : MonoBehaviour {
    //This method returns
    public string GetSteamAuthTicket() {
        byte[] ticketBlob = new byte[1024];
        uint ticketSize;

        // Retrieve ticket; hTicket should be a field in the class so you can use it to cancel the ticket later
        // When you pass an object, the object can be modified by the callee. This function modifies the byte array you've passed to it.
        HAuthTicket hTicket = SteamUser.GetAuthSessionTicket(ticketBlob, ticketBlob.Length, out ticketSize);

        // Resize the buffer to actual length
        Array.Resize(ref ticketBlob, (int)ticketSize);

        // Convert bytes to string
        StringBuilder sb = new StringBuilder();
        foreach (byte b in ticketBlob) {
            sb.AppendFormat("{0:x2}", b);
        }
        return sb.ToString();
    }

    public void OnGUI() {
        if (GUILayout.Button("Log In")) {
            if (SteamManager.Initialized) {
                // Execute PlayFab API call to log in with steam ticket
                PlayFabClientAPI.LoginWithSteam(new LoginWithSteamRequest {
                    CreateAccount = true,
                    SteamTicket = GetSteamAuthTicket()
                }, OnComplete,OnFailed);
            }
        }
    }

    // Utility callbacks to log the result
    private void OnComplete(LoginResult obj) {
        Debug.Log("Success!");
    }

    private void OnFailed(PlayFabError error) {
        Debug.Log("Failed: " + error.GenerateErrorReport());
    }
}

Testing

You may test right inside the editor. Run the scene and click "Log In" button in the top left corner (1). The console message should appear soon, indicating the authentication result (2):



Did you find this helpful?