Handle Login and Logout #

In this section, we’ll create a MonoBehavior class and facilitate logging in and out of the platform from Unity.

Prerequisites:

Create a C++ MonoBehavior class #

  1. From your Unity project, use the Project pane to create a Scripts folder within the Assets folder.

  2. Right-click in the Scripts folder and create a new C++ script named PragmaManager. When you create the script directly from Unity, the file is populated with a Unity MonoBehaviour class.

  3. Replace the code with the following:

    using Pragma;
    using UnityEngine;
    
    public class PragmaManager : MonoBehaviour
    {
        public Pragma.Player Player { get; private set; }
    
        private void Awake()
        {
            // The Pragma Runtime is automatically initialized on first retrieval.
            var runtime = Pragma.Runtime.Get();
    
            // This returns the first Player if available, or creates it. The Runtime is the source of truth for Player objects.
            Player = runtime.Player();
        }
    }
    

Method details #

  • Awake() is called when the GameObject is created.

  • Pragma.Runtime.Get() creates and initializes the Pragma Runtime, which is the main entrypoint into Pragma. In this case we use it to set configuration and access the Player session.

  • runtime.Player() is a convenience accessor to get (or create) the first Player session.

Facilitate login and logout #

To create a Pragma login/logout flow for your Unity project, add the following methods to your PragmaManager file:

public void LogIn()
{
    Player.LogIn(Pragma.Account.IdProvider.Unsafe, "test01", HandleLoggedIn);
}

public void LogOut()
{
    Player.LogOut(HandleLoggedOut);
}

private void HandleLoggedIn(Pragma.Result result)
{
    if (result.IsFailure)
    {
        Debug.LogErrorFormat("Pragma -- Login failed: {0}", result.ErrorCode);
        return;
    }
    Debug.Log("Pragma -- Logged in.");
}

private void HandleLoggedOut()
{
    Debug.Log("Pragma -- Logged out.");
}

Method details #

The LogIn and LogOut events simply forward to the Player object’s equivalent LogIn and LogOut methods, including a callback that will be invoked when on completion.

In the case of LogIn, we get back a Pragma.Result, which is a fancy way of saying “did it work?” We can inspect Result.IsSuccessful or Result.IsFailure to answer that question. If it errors, we can use the ErrorCode method to get a string to log.

  1. Open your project the Unity editor.

  2. Right click in an empty part of the Hierarchy pane on the left side, click Create Empty, and name the GameObject PragmaManager.

  3. Drag the PragmaManager.cs script onto the PragmaManager GameObject.

  4. Right click in the Hierarchy pane and select UI then Button.

  5. Expand the Button object in the Hierarchy pane and select Text.

  6. In the Inspector pane, find the Text box with the default text of Button. Rename it by typing Login.

  7. In the Hierarchy pane, click on the Button object.

  8. In the On Click (), select the + sign, and drag PragmaManager from the Hierarchy pane onto None (Object) in the Inspector pane. Click No Function and select PragmaManager, then Login().

Try it out! #

In Unity, click play, then click the Login button to connect to your locally running Pragma Engine instance.

If succesfull, you should see a Pragma -- Logged in. message in the Unity console.

The Unsafe login method is disabled by default in production. It is ‘unsafe’ because it has no password or other security measures!
Once logged in, you can call other Pragma API calls like this: Player.*Service Name*.Action().