Playfab Unity



Playfab-->

Get started with the PlayFab Party Unity plugin. Follow steps below to install the package and try out example code for a basic task.

The Process — Revealed. In this chapter, I’ll show you the entire process from a very high-level view. Our Unity3d PlayFab SDK provides everything you need to access the PlayFab API. This includes models, methods, an HTTP wrapper for sending and receiving web requests, and JSON serialization. This SDK is auto-generated using our open-sourced tool— SDKGenerator. We generally build SDKs every other week to stay current with the latest API changes. ###Expired Tokens: PlayFab party unity plugin uses the entity token of the logged-in user to access PlayFab services such as creating a network, speech-to-text and text-to-speech. However this Entity token has a finite expiration time typically 24 hrs after which the Party Unity plugin might experience issues accessing these services.

This quickstart helps you make your first API calls using the Party SDK for Unity. Before continuing, make sure you have completed Getting started for developers and Quickstart: PlayFab Client library for C# in Unity, which ensures you have a PlayFab account and are familiar with logging into PlayFab from your game and the PlayFab Game Manager.

Playfab Unity

Note

If you intend to use this plugin to develop games based on the Microsoft Game Development Kit (GDK) you need to acquire and install the GDK separately. Please also see details about Unity add-on for Game Core on Xbox consoles.

Requirements

  • A PlayFab developer account.

  • An installed copy of the Unity Editor. To install Unity for personal use via Unity Hub, or Unity+ for professional use, see Download Unity. Check on Unity support in documentation of your specific platform if needed. The minimum supported Unity version is Unity 2017 LTS.

  • A Unity Project – this can be any of the following:

    • A brand new project: For more information, see Starting Unity for the first time.
    • A guided tutorial project. For more information, see Getting Started with Unity.
    • An existing project.
  • The PlayFab 'core' Unity3D SDK. For information about installing the Unity3D SDK, see the 'Download and install PlayFab SDK' section of Quickstart: PlayFab Client library for C# in Unity.

Download and install the PlayFab Party Unity plugin

Following the steps to download and install the PlayFab Party Unity plugin.

  1. Download the PlayFab Party Unity Plugin Asset Package (use a distribution point depending on your platform).
  2. Important! Please see information in README file published with plugin. It is tailored to each particular version and may include important instructions specific to your platform.
  3. Open your Unity Project.
  4. Navigate to the location where you saved the .unitypackage and double-click it to open the import dialog.
  5. To import the PlayFab Party Unity Plugin into your project, select Import.

Set up your scene

This part of the guide shows you how to add the PlayFabMultiplayerManager to your scene to enable you to call PlayFab Party APIs from Unity.

Before you can create a network, you must have a PlayFab player logged in. For information about logging in a player, see Making your first API call in Quickstart: PlayFab Client library for C# in Unity.

  1. In the Unity editor, in the Project window, navigate to Assets > PlayFabPartySDK > Prefabs.

  2. From the Prefabs folder, drag and drop the PlayFabMultiplayerManager into your scene in the Hierachy window.

  3. Create an empty Game Object in your scene called 'HelloPartyLogic'.

  4. Select the HelloPartyLogic Game Object to open the Inspector.

  5. Select Add Component.

  6. Type 'HelloPartyLogic' and press enter to show the new script menu.

  7. Press enter again to create the new script, HelloPartyLogic.cs.

  8. Find the script in the Project window and double-click it to edit the script.

  9. Add the following using statement to your script:

  10. Add the following code in the Start method to log into PlayFab.

  11. Add the following methods to the class.

The Microsoft GDK and Windows only support x64.

To resolve these issues:

  1. In the Unity Editor, select File > Build Settings.
  2. Select your platform. Then from the Architecture dropdown, select either x86_64 or x64.
  3. Select Player Settings.
  4. In the right pane, select Other Setting.
  5. Find the Allow unsafe Code setting and select it.
  6. Close the Build Settings and Project Settings windows.

Playfab Unity Sdk

Connecting to a network

Unity Playfab Sdk

This part of the guide shows you how to Create and Join a network.

  1. Open the HelloPartyLogic.cs script. In the OnLoginSuccess method, add the following code to create and join a network:

  2. To define the OnNetworkJoined event handler, add the following code to the class:

  3. Save and click Play in the Unity Editor. The Network ID displays in the Console window.

Joining an existing network

This part of the guide shows you how to join an existing network that another client created.

Playfab Unity
  1. Open the HelloPartyLogic.cs script. In the OnLoginSuccess method, add the following code to Join a network:

  2. To define an event that fires when your local client joins the network, add the following code to the OnLoginSuccess method:

  3. To define the OnNetworkJoined event handler, add the following code to the class:.

    There are many ways to get the Network ID from the host to other players who want to join. Please refer to the sample in this Unity plugin for an example of how to do that.

  4. Save and select Play in the Unity Editor. The string 'Network joined!' displays in the Console window.

Accessing other players

This part of the guide shows you how to access other players on the network, including the local player.

To listen for new players joining and leaving the network, register for the OnRemotePlayerJoined and OnRemotePlayerLeft events.

  1. Open the HelloPartyLogic.cs script. In the OnLoginSuccess method, add the following code to Create and Join a network:

  2. Add the following methods to the class:

  3. To access the local player, add the following code to the OnRemotePlayerJoined method:

The PlayFabPlayer class contains properties for identifying the player, muting, and rendering their chat state in a chat UI.

Sending and receiving data messages

This part of the guide shows you how to send and receive data messages. Before you can start sending and receiving data messages, you must join a network.

  1. Open the HelloPartyLogic.cs script. In the OnLoginSuccess method, add the following code to listen for data messages:

  2. Add the OnDataMessageRecieved event handler to the class:

  3. To send a data message, add the following code to the Update method:

Playfab unity server

Save HelloPartyLogic.cs and select Play in the Unity Editor.

  1. In a second client, use the code shown above in Connect to a network to create and join a network.
  2. Copy the Network ID returned to your first client and connect to the network.
  3. Click on the scene to send a message. 'Hello (data message)' displays in the Console window.

Playfab Unity

Sending and receiving chat messages

This part of the guide shows you how to send and receive chat messages and mute remote players. In addition to text chat, Party automatically enables voice chat between players.

Something

Before you can start sending and receiving chat messages, you must join a network.

  1. Open the HelloPartyLogic.cs script. In the OnLoginSuccess method, add the following code to listen for chat messages:

  2. Add the event handler OnChatMessageReceived to the class;

Playfab Sdk Unity

Save HelloPartyLogic.cs and press Play in the Unity Editor.

  1. In a second client, use the code shown above in Connect to a network to create and join a network.

  2. Copy the Network ID returned to your first client and connect to the network.

  3. When you click on your scene, it sends a message and which displays in the Console window.

  4. If you want to give your players the option to mute other players, set the IsMuted property to true.