• Stars
    star
    407
  • Rank 106,183 (Top 3 %)
  • Language
    C++
  • License
    Other
  • Created about 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Google Sign-In API plugin for Unity game engine. Works with Android and iOS.

Google Sign-In Unity Plugin

Copyright (c) 2017 Google Inc. All rights reserved.

Overview

Google Sign-In API plugin for Unity game engine. Works with Android and iOS. This plugin exposes the Google Sign-In API within Unity. This is specifically intended to be used by Unity projects that require OAuth ID tokens or server auth codes.

It is cross-platform, supporting both Android and iOS.

See Google Sign-In for Android for more information.

Configuring the application on the API Console

To authenticate you need to create credentials on the API console for your application. The steps to do this are available on Google Sign-In for Android or as part of Firebase configuration. In order to access ID tokens or server auth codes, you also need to configure a web client ID.

How to build the sample

Get a Google Sign-In configuration file

This file contains the client-side information needed to use Google Sign-in. The details on how to do this are documented on the Developer website.

Once you have the configuration file, open it in a text editor. In the middle of the file you should see the oauth_client section:

      "oauth_client": [
        {
          "client_id": "411000067631-hmh4e210xxxxxxxxxx373t3icpju8ooi.apps.googleusercontent.com",
          "client_type": 3
        },
        {
          "client_id": "411000067631-udra361txxxxxxxxxx561o9u9hc0java.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "com.your.package.name.",
            "certificate_hash": "7ada045cccccccccc677a38c91474628d6c55d03"
          }
        }
      ]

There are 3 values you need for configuring your Unity project:

  1. The Web client ID. This is needed for generating a server auth code for your backend server, or for generating an ID token. This is the client_id value for the oauth client with client_type == 3.
  2. The package_name. The client entry with client_type == 1 is the Android client. The package_name must be entered in the Unity player settings.
  3. The keystore used to sign your application. This is configured in the publishing settings of the Android Player properties in the Unity editor. This must be the same keystore used to generate the SHA1 fingerprint when creating the application on the console. NOTE: The configutation file does not reference the keystore, you need to keep track of this yourself.

Create a new project and import the plugin

Create a new Unity project and import the GoogleSignIn-1.0.0.unitypackage (or the latest version). This contains native code, C# Unity code needed to call the Google Sign-In API for both Android and iOS.

Import the sample scene

Import the GoogleSignIn-sample.unitypackage which contains the sample scene and scripts. This package is not needed if you are integrating Google Sign-in into your own application.

Configure the web client id

  1. Open the sample scene in Assets/SignInSample/MainScene.
  2. Select the Canvas object in the hierarchy and enter the web client id in the SignInSampleScript component.

Building for Android

  1. Under Build Settings, select Android as the target platform.
  2. Set the package name in the player settings to the package_name you found in the configuration file.
  3. Select the keystore file, the key alias, and passwords.
  4. Resolve the Google Play Services SDK dependencies by selecting from the menu: Assets/Play Services Resolver/Android Resolver/Resolve. This will add the required .aar files to your project in Assets/Plugins/Android.

Building for iOS

For iOS, follow the instructions for creating a GoogleService-Info.plist file on https://developers.google.com/identity/sign-in/ios/start-integrating.

In Unity, after switching to the iOS player, make sure to run the Play Services Resolver. This will add the required frameworks and libraries to the XCode project via CocoPods.

After generating the XCode project from Unity, download the GoogleService-Info.plist file from the Google Developers website and add it to your XCode project.

Using the Games Profile to sign in on Android

To use the Play Games Services Gamer profile when signing in, you need to edit the dependencies in Assets/GoogleSignIn/Editor/GoogleSignInDependencies.xml.

Uncomment the play-services-games dependency and re-run the resolution.

Using this plugin with Firebase Auth

Follow the instructions to use Firebase Auth with Credentials on the Firebase developer website.

Make sure to copy the google-services.json and/or GoogleService-Info.plist to your Unity project.

Then to use Google SignIn with Firebase Auth, you need to request an ID token when authenticating. The steps are:

  1. Configure Google SignIn to request an id token and set the web client id as described above.
  2. Call SignIn() (or SignInSilently()).
  3. When handling the response, use the ID token to create a Firebase Credential.
  4. Call Firebase Auth method SignInWithCredential().
    GoogleSignIn.Configuration = new GoogleSignInConfiguration {
      RequestIdToken = true,
      // Copy this value from the google-service.json file.
      // oauth_client with type == 3
      WebClientId = "1072123000000-iacvb7489h55760s3o2nf1xxxxxxxx.apps.googleusercontent.com"
    };

    Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn ();

    TaskCompletionSource<FirebaseUser> signInCompleted = new TaskCompletionSource<FirebaseUser> ();
    signIn.ContinueWith (task => {
      if (task.IsCanceled) {
        signInCompleted.SetCanceled ();
      } else if (task.IsFaulted) {
        signInCompleted.SetException (task.Exception);
      } else {

        Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential (((Task<GoogleSignInUser>)task).Result.IdToken, null);
        auth.SignInWithCredentialAsync (credential).ContinueWith (authTask => {
          if (authTask.IsCanceled) {
            signInCompleted.SetCanceled();
          } else if (authTask.IsFaulted) {
            signInCompleted.SetException(authTask.Exception);
          } else {
            signInCompleted.SetResult(((Task<FirebaseUser>)authTask).Result);
          }
        });
      }
    });

Building the Plugin

To build the plugin run ./gradlew -PlintAbortOnError build_all. This builds the support aar library with lint warnings as errors and packages the plugin into a .unitypackage file. It also packages the sample scene and script in a separate package.

There's also a shortcut for linux/mac: ./build_all.

Questions? Problems?

Post questions to this Github project.

More Repositories

1

easypermissions

Simplify Android M system permissions
Java
9,846
star
2

mlkit

A collection of sample apps to demonstrate how to use Google's ML Kit APIs on Android and iOS
Java
3,534
star
3

google-services

A collection of quickstart samples demonstrating the Google APIs for Android and iOS
Java
3,045
star
4

android-vision

Deprecated: The Mobile Vision API is now a part of ML Kit: Check out this repo:
Java
2,928
star
5

android-testing-templates

Java
1,960
star
6

unity-jar-resolver

Unity plugin which resolves Android & iOS dependencies and performs version management
C#
1,275
star
7

mediapipe

Jupyter Notebook
1,253
star
8

android-custom-lint-rules

This sample demonstrates how to create a custom lint checks and corresponding lint tests
Kotlin
949
star
9

assistant-sdk-python

Samples and bindings for the Google Assistant API
Python
914
star
10

android-vulkan-tutorials

A set of samples to illustrate Vulkan API on Android
C++
849
star
11

arcore-depth-lab

ARCore Depth Lab is a set of Depth API samples that provides assets using depth for advanced geometry-aware features in AR interaction and rendering. (UIST 2020)
C#
777
star
12

android-testdpc

Test DPC is a sample device policy controller for use with Android Enterprise. It gives developers the ability to see how their app will behave in a managed context such as device owner or within a managed profile. Users can set up a work profile, enable work apps, set applications restrictions, manage security polices, and much more. The app also serves as a implementation reference for other DPCs
Java
751
star
13

io2015-codelabs

codelabs for Google I/O 2015
Java
517
star
14

google-photos

Samples for the Google Photos Library API 📸
JavaScript
495
star
15

vulkan-basic-samples

C++
494
star
16

android-play-publisher-api

Java
491
star
17

androidtv-sample-inputs

Sample Channel App (TV Input Service) on Android TV using TIF
Java
487
star
18

oauth-apps-for-windows

OAuth for Apps: Samples for Windows
C#
474
star
19

android-media-controller

Kotlin
448
star
20

android-dynamic-code-loading

Android dynamic code loading sample for Dynamic Feature Modules.
Kotlin
423
star
21

android-AppUsageStatistics

Java
366
star
22

web-fundamentals

Google Web Fundamentals
HTML
313
star
23

android-play-safetynet

Samples for the Google SafetyNet Attestation API
Java
287
star
24

io2014-codelabs

Google I/O 2014 Codelabs
Java
177
star
25

android-play-games-in-motion

Java
152
star
26

glass-enterprise-samples

Glass Enterprise Edition 2 Samples
Java
123
star
27

cloud-polymer-go

Sample App Engine application with Go, Cloud Endpoints, and Polymer
HTML
118
star
28

appauth-js-electron-sample

This is an Electron Application, which uses the AppAuth-JS library.
TypeScript
115
star
29

arcore-lightboard

C#
97
star
30

assistant-sdk-cpp

Example of Google Assistant gRPC in C++
C++
96
star
31

android-PermissionRequest

Java
95
star
32

ios-vision

Objective-C
93
star
33

assistant-sdk-nodejs

JavaScript
93
star
34

sceneform-samples

Sceneform samples for 3D rendering for ARCore in Java.
Java
87
star
35

arcore-ml-sample

Java
71
star
36

ios-nearby

Objective-C
68
star
37

arcore-illusive-images

C#
62
star
38

mugo

Sample on how to transpile a small subset of go to Arduino sketches
Go
47
star
39

identity-appflip-android

Lightweight Android app that simulates your native app role during App Flip
Java
34
star
40

identity-toolkit-node

JavaScript
29
star
41

functions-as-a-service

A demo showing Google Cloud Functions + Google Maps Platform
TypeScript
28
star
42

identity-toolkit-go

Identity toolkit sample code for Go
Go
25
star
43

identity-toolkit-java

Java
23
star
44

identity-appflip-tester-android

Lightweight Android app that simulates the Google app role during App Flip
Java
18
star
45

onetwoseven

Programmers debugging web server.
Go
16
star
46

identity-toolkit-php

PHP
15
star
47

android-TensorFlowCloudMachineLearningEngineStylizer

Java
14
star
48

Firebase-Plays-GCP-2016

JavaScript
14
star
49

identity-toolkit-ios

Objective-C
14
star
50

brillo-dragonboard-jacksmart

12
star
51

maps-deckgl-scatterplot-example

JavaScript
11
star
52

io19-sonic-boom

Live coding demo of "Sonic Boom!" talk in Google I/O 2019
C++
11
star
53

meet-live-sharing

Java
10
star
54

identity-appflip-ios

Lightweight iOS app that simulates your native app role during App Flip
Swift
10
star
55

dcp-parser-go

Go
9
star
56

identity-appflip-tester-ios

Lightweight iOS app that simulates the Google app role during App Flip
Swift
9
star
57

amapi

Kotlin
8
star
58

testloopmanager

Java
8
star
59

task-interop

Kotlin
6
star
60

sceneform-poly-browser

Java
6
star
61

subgraph_sdk_sample

Kotlin
6
star
62

identity-toolkit-ruby

HTML
4
star
63

engage-sdk-samples

Set of sample apps that demonstrate how to integrate the SDK in your app to publish different types of content. These apps are a great way to learn how to use the SDK, to get started with the integration in your own app, as well as some best practices.
Java
4
star
64

gboard-dev-samples

Java
4
star
65

identity-toolkit-django

2
star
66

snippets

Hosting for miscellaneous code snippets
2
star
67

pluscode-swift-demo

An example Plus Code service written in Swift
Swift
2
star
68

searchinapps-sample

Kotlin
2
star
69

.allstar

1
star
70

.github

1
star
71

zero-touch-enrollment-colabs

Jupyter Notebook
1
star