Skip to content

openfort-xyz/unity-recaptcha-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Openfort Unity reCAPTCHA V3 Sample

Overview

This is a sample project to showcase the Openfort integration with Google reCAPTCHA V3, a system to help you protect your sites from fraudulent activities, spam, and abuse. In this sample, we will activate reCAPTCHA verification every time the user tries to perform a key action like authenticating or minting an NFT.

The sample includes:

Workflow diagram

Openfort Unity reCAPTCHA V3 Sample Workflow

Prerequisites

Setup Openfort dashboard

Set up reCAPTCHA V3

Go to reCAPTCHA v3 Admin Console to register a new site. Choose Switch to create a classic key if the option is available:

Set up reCAPTCHA V3: Switch to create a classic key

Enter a Label name, select the reCAPTCHA type and enter your GitHub Pages URL as a Domain:

Set up reCAPTCHA V3: Fill options

Accept the terms of service and choose Submit:

Set up reCAPTCHA V3: Submit

Copy the Site Key and the Secret Key and save them somewhere safe:

Set up reCAPTCHA V3: Copy keys
  • Link to UGS project

    Follow the official documentation steps to link the unity-client to your UGS Project.

  • Fill in needed variables

    On the Main scene, select the ReCaptchaController game object and fill in your reCAPTCHA site key:

    Set up Unity client: Fill in reCAPTCHA site key

Build to WebGL

IMPORTANT: You need to build this sample to the root folder of the GitHub Pages repository.

In Unity go to File --> Build Settings, select Web platform and choose Build:

Build to WebGL

Find your GitHub Pages repository root folder and choose Select Folder:

Build to WebGL: Select folder

After the build is completed, go to your GitHub Pages repository URL and you should see the Sign in panel:

Build to WebGL: Sign in panel

Open the browser console to check that reCAPTCHA V3 is loaded and ready:

Build to WebGL: reCAPTCHA ready

Click the Sign in button and after some authentication-related logs, the Mint panel should appear. In the console logs, we should see a positive reCAPTCHA verification score. Because we passed the reCAPTCHA verification, an Openfort player should have been created:

Build to WebGL: positive score

If you click the Mint button, the game will go through another reCAPTCHA verification. After a brief period, you should see the NFT minted panel with the Openfort player account address in a button:

Build to WebGL: NFT minted

Click on the Address button to open Etherscan and see that the transaction is confirmed:

Etherscan

Code walkthrough

All this is thanks to the ReCaptcha.jslib plugin located in the Unity client, which allows the execution of reCAPTCHA V3 from the client. The .jslib methods are called from the ReCaptchaController.cs.

Then, when reCAPTCHA V3 gets the response token from the execution, we validate it in the backend through VerifyReCaptcha.js

Conclusion

Upon completing the above steps, your Unity game will be fully integrated with Openfort and Google reCAPTCHA V3. Always remember to test every feature before deploying to guarantee a flawless player experience.

Get support

If you found a bug or want to suggest a new [feature/use case/sample], please file an issue.

If you have questions, or comments, or need help with code, we're here to help: