Google Account Authentication in Unity Guide
In this article
To authenticate users with a Google account in Unity, the most reliable method is using Firebase Authentication, as it provides a managed SDK that handles the OAuth2 flow and token verification. This allows users to sign into your game using their existing Google credentials, removing the need for them to create a new username and password for your application.
What You'll Need
- A Unity project (2020.3 LTS or newer recommended).
- A Google Account.
- A Firebase project (created via the Firebase Console).
- The Firebase Unity SDK.
- The SHA-1 certificate fingerprint from your keystore (for Android builds).
Step-by-Step
- Create a Firebase Project: Go to the Firebase Console, click Add Project, and follow the prompts to name and create your project.
- Register Your App: In the Firebase Console project overview, click the Unity icon. Enter your Unity Project ID (found in Unity under Project Settings > Project) and register the app.
- Configure Platform Settings: For Android, enter your Android package name and your SHA-1 fingerprint. For iOS, enter your iOS Bundle ID. Labels may differ slightly depending on the version of the console.
- Download Configuration Files: Download the
google-services.json(for Android) orGoogleService-Info.plist(for iOS). Place these files anywhere inside your Unity project'sAssetsfolder. - Import the Firebase SDK: Download the Firebase Unity SDK. In Unity, go to Assets > Import Package > Custom Package and select the
FirebaseAuth.unitypackage. - Enable Google Sign-In in Firebase: In the Firebase Console, navigate to the Authentication section in the left sidebar. Click the Sign-in method tab, select Google, toggle the switch to Enable, and provide a support email. Save the changes.
- Implement the Sign-In Logic: Create a C# script to handle the authentication. You will need to use the
FirebaseAuthclass to initialize the instance and theGoogleAuthProviderto handle the credential exchange. - Handle the OAuth Flow: Since Unity does not have a built-in browser for OAuth, you must use a plugin (such as the Google Sign-In Unity plugin) to trigger the native Google login screen on the device. Once the plugin returns an ID token, pass that token to Firebase using
FirebaseAuth.DefaultInstance.SignInWithCredentialAsync(credential). - Verify User State: Use the
FirebaseAuth.DefaultInstance.AuthStateChangedevent to detect when a user has successfully logged in or logged out, and transition your game scene accordingly.
Common Mistakes
- Missing SHA-1 Fingerprint: Forgetting to add the SHA-1 fingerprint to the Firebase Console is the most common cause of authentication failure on Android.
- Incorrect Package Name: The package name in Unity's Player Settings must exactly match the package name registered in the Firebase Console.
- Wrong SDK Version: Using a version of the Firebase SDK that is incompatible with your Unity version can lead to build errors or crashes.
- Ignoring the Plist/JSON: Placing the configuration files in a subfolder that the SDK cannot find, or renaming them, will prevent the app from connecting to the backend.
Tips
- Use a Loading Screen: Authentication is an asynchronous process. Always implement a loading spinner or screen to prevent the user from interacting with the UI while the token is being verified.
- Test with Multiple Accounts: Test the flow with both a primary account and a secondary account to ensure the session management (signing out and signing back in) works correctly.
- Secure Your API Keys: While the
google-services.jsonis bundled with the app, ensure you restrict your API keys in the Google Cloud Console to prevent unauthorized use outside your app.
Troubleshooting
If the authentication process fails, try these steps in order:
- Check Internet Connection: Ensure the device has a stable connection to the Google and Firebase servers.
- Verify Console Status: Check the Firebase Status Dashboard to ensure there are no ongoing service outages.
- Update SDKs: Ensure both the Firebase Unity SDK and the Google Sign-In plugin are updated to the latest compatible versions.
- Re-check SHA-1: If you are using a different keystore for production than for development, you must add the production SHA-1 fingerprint to the Firebase Console.
- Check Logcat/Xcode Logs: Look at the Android Logcat or Xcode console for specific error codes (e.g.,
Developer Errorusually indicates a mismatch in SHA-1 or package name).
Frequently Asked Questions
Do I need a paid account to use Google authentication?
No, Firebase Authentication offers a generous free tier (Spark Plan) that is sufficient for most small to medium projects.
Can I use this for desktop builds?
While Firebase is primarily designed for mobile, you can use it for desktop, but the OAuth flow is more complex as you cannot use native mobile sign-in plugins.
Does this store the user's password in my database?
No, Google authentication uses OAuth2. Your app receives a token verifying the user's identity; the password remains securely with Google.
What happens if the user deletes their Google account?
The next time the user attempts to authenticate, the token will be invalid, and the authentication request will fail.
Can I combine Google login with other methods?
Yes, Firebase allows you to enable multiple providers (like Email/Password or Facebook) and link them to a single user account.