Home

Integrate PlayFab with Openfort in Unity

Overview#

PlayFab is a backend service provided by Microsoft for game developers, offering tools for live game management, all powered by Azure's cloud infrastructure.

In this integration, we use the Google Play Games plugin for Unity to handle authentication with PlayFab, ensuring a seamless and secure login experience for users on Android devices.

By integrating the Openfort SDK into Azure Functions, we establish a seamless connection to PlayFab. Unity clients using the PlayFab Unity SDK can tap into these functions, accessing the full range of Openfort features within the game environment.

You can check the complete code for this guide in our GitHub repository.

Application Workflow#

Integration workflow

Prerequisites#

Set up Openfort#

  1. Add a Contract

    This sample requires a contract to run. We use 0x38090d1636069c0ff1Af6bc1737Fb996B7f63AC0 (NFT contract deployed in 80001 Mumbai). You can use this for the guide:

import contract
  1. Add a Policy

    We aim to cover gas fees for users. Set a new gas policy:

policy information

Now, add a rule so our contract uses this policy:

policy rule

Deploy Azure Backend#

Open azure-backend with VS Code and sign in to Azure:

Azure backend image

Ensure your Function App (here, it's "openfort-playfab") is listed:

Function App image

In the terminal, run:


_10
npm install

In the explorer, right-click on a function and select Deploy to Function App:

Deploy to Function App

Next, choose your Function App:

Select Function App

Then, click on Deploy:

Deploy Confirmation

Navigate to your Azure Portal and open your Function App. You should see all the functions listed:

Functions List

Click on any function and select Get Function Url:

Get Function URL

Subsequently, add this URL (along with all others) to PlayFab to enable access to our Azure Functions from within PlayFab.

Set up PlayFab Title#

  1. Register Azure Functions

    Visit the PlayFab developer dashboard, choose your title, and click on Automation:

Automation Selection

Our functions are already registered. To do the same, click Register function and provide the function name along with its URL:

Register cloud function

Repeat this for all deployed functions.

  1. Install Google Add-on

    Navigate to Add-ons and choose Google:

Google Add-on Selection

Then, visit the Google Cloud APIs Credentials dashboard and click on the arrow icon next to your OAuth Web Application Client:

OAuth Web Application Client

Copy both the Client ID and Client Secret:

OAuth param details

Return to the PlayFab dashboard, paste these details into the Google OAuth Client ID and Google OAuth Client Secret fields, and click Install Google:

OAuth config
  1. Add PlayFab OAuth Redirect URL

    For this, go to the Google Cloud APIs Credentials dashboard. Here, select your Web application:

Web Application Selection

In the Authorized redirect URIs section, add the URL below and click Save:


_10
https://oauth.playfab.com/oauth2/google

oauth url redirect

Set up Azure Backend#

Our Azure backend requires environment variables from both PlayFab and Openfort. Let's configure them.

  1. Add Openfort Environment Variables

    • Navigate to the Azure Portal and select your Function App.
    • Under Configuration, click New application setting:
oauth playfab config

After adding these, your configuration panel should resemble the following. Click Save:

oauth env portal
  1. Add PlayFab Environment Variables

  • Visit the PlayFab developer dashboard, select your title, and navigate to Settings wheel --> Title settings:

    playfab title settings
  • In the API Features section, copy your Title ID:

    get playfab title id
  • Under Secret Keys, note down your Secret key:

    Playfab Secret Key
  • Return to the Azure Portal and choose your Function App.

  • Under Configuration, select New application setting and input:

    • Name: PLAYFAB_TITLE_ID
    • Value: [Your Title ID]
  • Add another application setting:

    • Name: PLAYFAB_SECRET_KEY
    • Value: [Your Secret Key]

Your configuration panel should now look like the following. Confirm your changes by clicking Save:

Portal config panel

Set up Unity Client#

This Unity sample project is already equipped with:

To begin, open unity-client with Unity:

  1. Configure PlayFab SDK

    • Navigate to the Project tab.
    • Search for PlayFabSharedSettings and input your PlayFab Title ID:
unity playfab setting
  1. Configure Google Play Games SDK

    • Even if you've set up the Google Play Games SDK following the required tutorial, ensure that you've correctly configured all fields by navigating to Window --> Google Play Games --> Setup --> Android setup:

      Google Play Games Config 1
    unity playfab setting

    By doing this, when the game runs on Android, it will utilize Google Play Games for user authentication via PlayFab. Otherwise, the default PlayFab authentication will be used.

Test in Editor#

Play Login scene, opt for Register, provide an email and password, then click Register again. This scene should appear:

unity-mint

Select Mint. After a brief period, you should see a representation of your newly minted NFT:

Minted NFT

In the Openfort Players dashboard, a new player entry should be visible. On selecting this player:

Player Entry

You'll notice that a mint transaction has been successfully processed:

Mint Transaction

Additionally, by choosing your Mumbai Account and viewing ERC-721 Token Txns, the transaction is further confirmed:

scw-address
Transaction Confirm 2

Test on Android#

Upon building and running the game on an Android device, the registration/login process is automated via Google Play Games, resulting in a streamlined user experience.

Extra: Guest Registration Functionality#

Guest registration functionality - Workflow diagram

Playing as a guest is a common way for players to authenticate to games, and all BaaS providers offer that functionality. The problem is that if the player has made some progress while playing as a guest, they will lose it for the next gaming session.

For this reason, we have introduced Guest registration functionality in Lost Dungeon, allowing players to authenticate as guests and register with an email and password at their convenience:

Guest registration functionality - Resgitaration

When that happens, a new user is created in the BaaS (PlayFab in this case), and the Openfort player information associated with the guest user is transferred to the new user:

Guest registration functionality - Openfort info

This way, for the next session, the player can log in as the newly registered user and retain all the progress made while being a guest. Furthermore, they will now be able to enable self-custody and transfer all the progress (acquired blockchain assets) to a self-custodial account of their choice.

Conclusion#

Upon completing the above steps, your Unity game will be fully integrated with Openfort and PlayFab. Always remember to test every feature before deploying to guarantee a flawless player experience.

Get support#

If you found a bug or want to suggest a new [feature/use case/sample], please file an issue.

If you have questions, comments, or need help with code, we're here to help: