Skip to main content

Requirements

  • Playtolia SDK (with Authentication enabled)

Sessions API

Playtolia Sessions API is the main access point for user-related information and profile management. Using Sessions API you can get user IDs, name, email, locale, and manage player profiles. Just like other components, Sessions are managed with a Stateful object: PlaytoliaSession. PlaytoliaSession listens to changes in the auth state to automatically update user information. If the player has not signed in or logged out, state will be null

Getting the current session

Import the PlaytoliaSDK.Runtime to your script by adding the using directive
ProfileMenu.cs
using PlaytoliaSDK.Runtime;
Call the GetUser function to get the current user session
ProfileMenu.cs
User user = PlaytoliaSession.GetUser();
if(user == null) return; // user is not logged in

string email = user.Email;
string uid = user.Uid;
string playerId = user.PlayerId;
string username = user.Username;
Always check if the user is null before accessing user properties. The Sessions API will return null if the player is not authenticated or if the session has expired.
PlaytoliaSession automatically refreshes user data when authentication state changes. You can also manually refresh using PlaytoliaSession.Refresh() if needed.

Understanding User structure

The User object contains comprehensive player information:
  • Uid: Unique user identifier across the platform
  • PlayerId: Game-specific player identifier
  • Username: Player’s display name
  • Email: Player’s email address
  • Lang: Player’s language/locale preference
  • Location: Player’s geographic location
  • Timezone: Player’s timezone setting
  • Avatar: URL to player’s profile image
  • CreatedAt: Account creation timestamp
UserProfileDisplay.cs
public void DisplayUserProfile()
{
    User user = PlaytoliaSession.GetUser();
    if (user == null)
    {
        Debug.Log("No user logged in");
        return;
    }

    Debug.Log($"User ID: {user.Uid}");
    Debug.Log($"Player ID: {user.PlayerId}");
    Debug.Log($"Username: {user.Username}");
    Debug.Log($"Email: {user.Email}");
    Debug.Log($"Language: {user.Lang}");
    Debug.Log($"Location: {user.Location}");
    Debug.Log($"Timezone: {user.Timezone}");
    Debug.Log($"Avatar URL: {user.Avatar}");
    Debug.Log($"Account Created: {user.CreatedAt}");
}

Listen to user state changes

During the runtime, PlaytoliaSession state might change several times due to:
  • Authentication updates: login, log out, token refresh
  • Profile updates: Changing username, email, or other profile data
  • Manual refresh via PlaytoliaSession.Refresh()
In order to make sure your game logic and UI stays up-to-date you can add listeners to PlaytoliaSession, just like with any other stateful component.
ProfileMenu.cs
public void Start()
{
    PlaytoliaSession.AddListener(() => {
        User user = PlaytoliaSession.GetUser();
        if(user == null)
        {
            HandleUserLoggedOut();
            return;
        }

        UpdateProfileUI(user);
    });
}

void UpdateProfileUI(User user)
{
    usernameText.text = user.Username;
    emailText.text = user.Email;
    playerIdText.text = user.PlayerId;
}

void HandleUserLoggedOut()
{
    // Clear profile UI
    usernameText.text = "Guest";
    emailText.text = "";
    playerIdText.text = "";
}

Profile management example

Here’s a practical example of managing user profiles in your game:
PlayerProfileManager.cs
using PlaytoliaSDK.Runtime;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
using System.Collections;

public class PlayerProfileManager : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private Text usernameDisplay;
    [SerializeField] private Text emailDisplay;
    [SerializeField] private Text playerIdDisplay;
    [SerializeField] private Text joinDateDisplay;
    [SerializeField] private RawImage avatarImage;
    [SerializeField] private GameObject profilePanel;
    [SerializeField] private GameObject loginPrompt;

    void Start()
    {
        // Listen to session changes
        PlaytoliaSession.AddListener(OnSessionChanged);

        // Initial state check
        OnSessionChanged();
    }

    void OnSessionChanged()
    {
        User user = PlaytoliaSession.GetUser();
        bool isLoggedIn = user != null;

        // Show/hide appropriate panels
        profilePanel.SetActive(isLoggedIn);
        loginPrompt.SetActive(!isLoggedIn);

        if (isLoggedIn)
        {
            DisplayUserProfile(user);
        }
    }

    void DisplayUserProfile(User user)
    {
        // Update basic info
        usernameDisplay.text = user.Username ?? "Unknown Player";
        emailDisplay.text = user.Email ?? "No email";
        playerIdDisplay.text = $"ID: {user.PlayerId}";

        // Parse and display creation date
        if (!string.IsNullOrEmpty(user.CreatedAt))
        {
            if (DateTime.TryParse(user.CreatedAt, out DateTime createdDate))
            {
                joinDateDisplay.text = $"Joined: {createdDate:MMM dd, yyyy}";
            }
        }

        // Load avatar if available
        if (!string.IsNullOrEmpty(user.Avatar))
        {
            StartCoroutine(LoadAvatarCoroutine(user.Avatar));
        }
    }

    IEnumerator LoadAvatarCoroutine(string avatarUrl)
    {
        using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(avatarUrl))
        {
            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.Success)
            {
                Texture2D texture = DownloadHandlerTexture.GetContent(request);
                avatarImage.texture = texture;
            }
        }
    }

    void OnDestroy()
    {
        PlaytoliaSession.RemoveListener(OnSessionChanged);
    }
}

Locale and internationalization

Use user locale information for game localization:
LocalizationManager.cs
using PlaytoliaSDK.Runtime;
using UnityEngine;

public class LocalizationManager : MonoBehaviour
{
    void Start()
    {
        PlaytoliaSession.AddListener(UpdateLocalization);
    }

    void UpdateLocalization()
    {
        User user = PlaytoliaSession.GetUser();

        string locale = user?.Lang ?? GetSystemLocale();
        ApplyLocalization(locale);
    }

    string GetSystemLocale()
    {
        return Application.systemLanguage.ToString().ToLower().Substring(0, 2);
    }

    void ApplyLocalization(string locale)
    {
        Debug.Log($"Applying localization for: {locale}");

        // Apply language settings based on locale
        switch (locale.ToLower())
        {
            case "en":
                LoadEnglishLocalization();
                break;
            case "es":
                LoadSpanishLocalization();
                break;
            case "fr":
                LoadFrenchLocalization();
                break;
            default:
                LoadEnglishLocalization(); // Fallback
                break;
        }
    }

    void LoadEnglishLocalization() { /* Load English strings */ }
    void LoadSpanishLocalization() { /* Load Spanish strings */ }
    void LoadFrenchLocalization() { /* Load French strings */ }
}

Need to save custom player data?

For storing custom game-specific data like player progress, preferences, and achievements, you’ll want to use the Attributes API.

Attributes

Save and read custom data such as progress, preferences, and roles. All backed up to the cloud and synchronised between your players devices.