The creator-centric platform for building live games in Unity

Easily add social, commerce, and content management features to your live game with low-code, drag-and-drop prefabs fully integrated with your Unity workflow.

Adding Google Sign-In

Overview

The purpose of this guide is for game makers to use Google Sign-In with the Beamable Accounts FeatureFeature - An individual aspect of the Beamable product used to create a great user experience.

Integration

Beamable integrates with this technology.

Google's Sign-In manages the OAuth 2.0 flow and token lifecycle, simplifying your integration with Google APIs. A user always has the option to revoke access to an application at any time.

This document describes how to complete a basic Google Sign-In integration.

User Experience

When setup properly, the user experience in the game project will be as follows.

Prerequisites

This guide assumes the following prerequisites have been completed.

Steps

Details

  1. Unity: Set up the Beamable SDK for Unity

• See Beamable's Getting Started for more info

  1. Unity: Switch platform to Android

• Unity → File → Build Settings
• Select Android then press Switch Platform

  1. Unity: Establish Keystore for Signing

• Unity → File → Build Settings
• Press Player Settings...
• In Inspector, go to Publishing Settings and create the keystore (or unlock an existing one).

  1. Android: Set up the corresponding Google Cloud Platform application with OAuth 2.0 credentials

• See Google's start-integrating#configure_a_project for more info

Note that you will need both Web application credentials AND platform specific credentials for Android or iOS or both.

Steps

Follow these steps to get started.

Step

Detail

  1. Retrieve the Client Id

• Go to https://console.cloud.google.com/apis/credentials
• Copy the "Web application" client ID

  1. Open Configuration Manager

• Unity → Window → Beamable → Open Toolbox • Click the "Config" Button
  1. Edit "Account Management" Configuration

• Set "Google" to true • Set "Google Client Id" to Client Id from Step 1 • (Optional) Set "Google iOS Client Id" to Client Id from Step 1
  1. Edit gradle file.
  • Open the .gradle file located in your project at Plugins/Android/mainTemplate.gradle
  • Look for the dependencies block and add play-services-auth.
    See the code sample below for how the block should look after adding the dependency.
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.gms:play-services-auth:18.1.0'
**DEPS**
}

Additional iOS Setup

When using Google Sign-In on Apple, the Login Flow depends on version 5 of Google's Sign-In SDK framework for iOS. Enabling sign-in on iOS also requires first-time setup regarding a custom URL scheme specific to your Google Cloud App, including overriding the openURL method on UnityAppController.

Step

Detail

  1. Install the Google Sign-In SDK

• Download Google Sign-In SDK version 5.0.0 or newer from https://developers.google.com/identity/sign-in/ios/sdk
• Create the Assets/Plugins/iOS folder in your Unity project if it does not already exist
• Extract the SDK archive, then copy GoogleSignIn.framework and GoogleSignInDependencies.framework to your Assets/Plugins/iOS folder (note that Beamable does not need GoogleSignIn.bundle as we source icon images independently)

  1. Add the URL scheme to your project

• As described in the link from Step 1, your custom URL scheme is a reversed version of your iOS Client ID: com.googleusercontent.apps.<your-app-id>
• Unity -> File -> Build Settings -> Player Settings... -> Other Settings
• Supported URL schemes: set size to 1 and enter your reversed ID as Element 0

  1. Ensure that your app delegate (UnityAppController in most cases) handles the openURL method

• The app delegate for your app should handle application:openURL:options: and invoke the handleURL method of GIDSignIn.
• See below for an example of how to use "method swizzling" to override the openURL method.

Method Swizzling for iOS URL Handling

The "Supported URL Schemes" in the Unity Inspector WindowThe "Supported URL Schemes" in the Unity Inspector Window

The "Supported URL Schemes" in the Unity Inspector Window

The response from the Google Sign-In flow on Apple uses the custom Supported URL Scheme we set up in the steps above. This requires that the app delegate (namely UnityAppController) implement the openURL method. In order to modify that method, we can use a technique called swizzling, wherein we define a new method with a different selector path and then use method_exchangeImplementations to swap one method for the other.

The code below swizzles the openURL method by adding a category named GoogleSignInAppController onto UnityAppController. When the class has been loaded, the swizzling in the load method will exchange the implementations. Note that in the implementation, we still call the old openURL in case any other handler had been similarly attached.

To use this code as-is, place both GoogleSignInAppController.h and GoogleSignInAppController.mm somewhere within a Plugins/iOS folder in your Unity project.

#import <GoogleSignIn/GIDSignIn.h>
#import <UnityAppController.h>

@interface UnityAppController (GoogleSignInAppController)

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
            options:(NSDictionary<NSString *, id> *)options;

@end
#import "GoogleSignInAppController.h"
#import <objc/runtime.h>
  
@implementation UnityAppController (GoogleSignInController)

+ (void)load {
  method_exchangeImplementations(
    class_getInstanceMethod(
      self,
      @selector(application:openURL:options:)),
    class_getInstanceMethod(
      self,
      @selector(GoogleSignInAppController:openURL:options:))
  );
}

- (BOOL)GoogleSignInAppController:(UIApplication *)app
                          openURL:(NSURL *)url
                          options:(NSDictionary *)options {
  BOOL handled = [self GoogleSignInAppController:application
                                         openURL:url
                                         options:options];
                            
  return [[GIDSignIn sharedInstance] handleURL:url] || handled;
}

@end

User Interface

When setup properly, the player's user interface in the game project will be as follows.

Account Management Flow (Default)

Account Management Flow (Google Sign-In)

Next Steps

From here, the player can edit account details including account name and account avatar.

The player can switch accounts and sign in with various methods. See the Accounts feature page for more info.

When finished, the player can close the Account Management Flow window and return to the game-specific content.

Updated 2 months ago


Adding Google Sign-In


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.