*

Unity Authentication Example

This example shows how to use several of the authentication types in brainCloud. Specifically we illustrate how to authenticate using the following authentication types:

  • Email and password
  • Universal
  • Anonymous
  • Google (Android Devices Only)

Prerequisites

In order the get started you will need Unity installed. Please download the example project below here:

Unity Authentication Example Download

You can download the stand-alone brainCloud Unity library here:

brainCloud Unity Client

Running the project

To try out the example project you should just need to:

  • Unzip the project
  • Open the project in Unity
  • Open the Connect scene in the “Scenes” folder
  • Follow the steps in Pointing To Your Own Game below
  • Hit the play button

You should now see a screen like the following:

 

Feel free to try out the various authentication types. Note that email/password authentication will send you a validation email if it’s a new account being created. You will not be prevented from logging in right away however.

 

Pointing to your own game

In order to test out the authentication methods against your own brainCloud game follow these steps:

1) First, open the brainCloud Settings menu option:

 

2) Next, login to brainCloud and select your game:

 

3) Ensure that the correct platforms have been enabled in the brainCloud Portal, under Design | Core App Info | Platforms

 

4) Import the game data from the “ImportData” folder.

See Import/Export Game Data in the portal tutorial section for more info.

You should be all good to go!

Code Overview

Initializing the brainCloud Wrapper.

using UnityEngine;

public class BCConfig : MonoBehaviour {

  private BrainCloudWrapper _bc;

  public BrainCloudWrapper GetBrainCloud()
  {
    return _bc;
  }
  
  
  // Use this for initialization
  void Awake ()
  {
    DontDestroyOnLoad(gameObject);
    _bc = gameObject.AddComponent<BrainCloudWrapper>();
    
    _bc.WrapperName = gameObject.name;    // Optional: Set a wrapper name
    _bc.Init();      // Init data is taken from the brainCloud Unity Plugin
    
  }
}

 

Authenticating the user.

if (m_selectedAuth == (int) eAuthTypes.EMAIL)
        {
            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical();
            GUILayout.Label("Email:");
            GUILayout.Label("Password:");
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            m_emailId = GUILayout.TextField(m_emailId, GUILayout.MinWidth(200));
            m_emailPwd = GUILayout.PasswordField(m_emailPwd, '*', GUILayout.MinWidth(200));
            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            if (GUILayout.Button("Authenticate", GUILayout.ExpandWidth(false)))
            {
                m_authStatus = "Attempting to authenticate";

                // clear out any previous profile or anonymous ids
                _bc.ResetStoredProfileId();
                _bc.ResetStoredAnonymousId();
                _bc.AuthenticateEmailPassword(
                    m_emailId, m_emailPwd, true, OnSuccess_Authenticate, OnError_Authenticate);
            }
        }
        else if (m_selectedAuth == (int) eAuthTypes.UNIVERSAL)
        {
            GUILayout.BeginHorizontal();
            GUILayout.BeginVertical();
            GUILayout.Label("User Id:");
            GUILayout.Label("Password:");
            GUILayout.EndVertical();
            GUILayout.BeginVertical();
            m_universalUserId = GUILayout.TextField(m_universalUserId, GUILayout.MinWidth(200));
            m_universalPwd = GUILayout.PasswordField(m_universalPwd, '*', GUILayout.MinWidth(200));
            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
             
            if (GUILayout.Button("Authenticate", GUILayout.ExpandWidth(false)))
            {
                m_authStatus = "Attempting to authenticate";

                // clear out any previous profile or anonymous ids
                _bc.ResetStoredProfileId();
                _bc.ResetStoredAnonymousId();
                _bc.AuthenticateUniversal(
                    m_universalUserId, m_universalPwd, true, OnSuccess_Authenticate, OnError_Authenticate);
            }
        }        
        else if (m_selectedAuth == (int) eAuthTypes.ANONYMOUS)
        {
            GUILayout.Label("Profile Id: " + _bc.GetStoredProfileId());
            GUILayout.Label("Anonymous Id: " + _bc.GetStoredAnonymousId());
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Authenticate", GUILayout.ExpandWidth(false)))
            {
                m_authStatus = "Attempting to authenticate";
                _bc.AuthenticateAnonymous(OnSuccess_Authenticate, OnError_Authenticate);
            }
            if (GUILayout.Button("Reset Profile Id", GUILayout.ExpandWidth(false)))
            {
                _bc.ResetStoredProfileId();
            }
            if (GUILayout.Button("Reset Anonymous Id", GUILayout.ExpandWidth(false)))
            {
                _bc.ResetStoredAnonymousId();
            }
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
        }

Handling success of error on authentication.

public void OnSuccess_Authenticate(string responseData, object cbObject)
    {
        m_authStatus = "Authenticate successful!";
        gameObject.SetActive(false);
        MainScene.SetActive(true);
    }
    
  public void OnError_Authenticate(int statusCode, int reasonCode, string statusMessage, object cbObject)
    {
        m_authStatus = "Authenticate failed: " + statusMessage;
        Debug.LogError("OnError_Authenticate: " + statusMessage);
    }

 

Further Reading

You can find more information on the Authentication methods by visiting this section of the API reference:

Authentication APIs

The code within the example project that deals with authentication is located in “Scripts/ConnectScene.cs”.