Skip to content

๐Ÿ”’ hCaptcha integration for Unity Android | Drop-in captcha verification | Invisible captcha | Bot protection | Unity plugin | Android AAR library | MIT License

License

Notifications You must be signed in to change notification settings

gtrqa/hcaptcha-unity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

36 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”’ hCaptcha for Unity

Unity License Platform hCaptcha

Simple and powerful hCaptcha integration for Unity Android games and apps.

hCaptcha Demo

โœจ Features

  • ๐ŸŽฎ Drop-in Integration - Import and use in minutes
  • ๐Ÿ‘ป Invisible Captcha - Seamless user experience
  • ๐Ÿ”ง Automatic Setup - Gradle configured automatically
  • ๐Ÿ“ฆ Pre-built AAR - No Android Studio required (Unity 6+)
  • ๐ŸŽฏ Simple API - Easy C# interface
  • ๐ŸŽจ Two UI Examples - OnGUI test scene + Canvas-ready prefab
  • ๐Ÿ†“ MIT Licensed - Free for commercial use
  • ๐ŸŒ Open Source - Full source code available

๐Ÿš€ Quick Start

1. Installation

Download the latest HCaptcha-Unity.unitypackage and import into Unity:

Assets โ†’ Import Package โ†’ Custom Package โ†’ Select downloaded file

2. Get hCaptcha Site Key

  1. Sign up at hCaptcha.com (free)
  2. Create a new site
  3. Copy your Site Key

3. Setup in Unity

โš ๏ธ IMPORTANT: Custom Gradle Templates Required!

After importing, Unity must use the included Gradle templates:

  1. Edit โ†’ Project Settings โ†’ Player โ†’ Publishing Settings โ†’ Build
  2. Enable BOTH:
    • โœ“ Custom Main Gradle Template
    • โœ“ Custom Gradle Settings Template

Without these, the build will fail! These templates configure JitPack repository and hCaptcha dependencies.

Then add to scene:

  1. Drag Prefabs/HCaptchaManager into your scene
  2. Select the prefab in Inspector
  3. Paste your Site Key
  4. (Optional) Drag Prefabs/TestUI for instant test button
  5. Replace Unity's generated templates with package templates:
    • Copy Assets/HCaptcha/Runtime/Plugins/Android/mainTemplate.gradle โ†’ Assets/Plugins/Android/mainTemplate.gradle
    • Copy Assets/HCaptcha/Runtime/Plugins/Android/settingsTemplate.gradle โ†’ Assets/Plugins/Android/settingsTemplate.gradle
    • Overwrite when prompted

4. Use in Code

using UnityEngine;
using HCaptcha;

public class LoginManager : MonoBehaviour
{
    void Start()
    {
        HCaptchaManager.Instance.OnSuccess.AddListener(OnCaptchaSuccess);
        HCaptchaManager.Instance.OnFailure.AddListener(OnCaptchaFailure);
    }

    public void OnLoginButtonClick()
    {
        HCaptchaManager.Instance.Verify();
    }

    void OnCaptchaSuccess(string token)
    {
        Debug.Log("โœ“ Captcha verified! Token: " + token);
        // TODO: Send token to your server for validation
        AllowLogin();
    }

    void OnCaptchaFailure(string error)
    {
        Debug.LogError("โœ— Captcha failed: " + error);
        ShowErrorMessage("Verification failed. Please try again.");
    }
}

Or connect directly in Unity UI:

  1. Select your Button
  2. OnClick() โ†’ Drag HCaptchaManager prefab
  3. Select function: HCaptchaManager โ†’ Verify()

๐Ÿ“ฆ What's Included

HCaptcha/
โ”œโ”€โ”€ Runtime/
โ”‚   โ”œโ”€โ”€ Prefabs/
โ”‚   โ”‚   โ”œโ”€โ”€ HCaptchaManager.prefab    # Main captcha handler
โ”‚   โ”‚   โ””โ”€โ”€ TestUI.prefab              # Ready-to-use test button
โ”‚   โ”œโ”€โ”€ Scripts/
โ”‚   โ”‚   โ”œโ”€โ”€ HCaptchaManager.cs         # High-level API
โ”‚   โ”‚   โ””โ”€โ”€ HCaptchaUnityBridge.cs     # Android bridge
โ”‚   โ””โ”€โ”€ Plugins/Android/
โ”‚       โ”œโ”€โ”€ unithhcapt-lib.aar         # Pre-compiled library
โ”‚       โ”œโ”€โ”€ mainTemplate.gradle        # hCaptcha dependencies
โ”‚       โ””โ”€โ”€ settingsTemplate.gradle    # JitPack repository
โ”œโ”€โ”€ Editor/
โ”‚   โ””โ”€โ”€ HCaptchaSetup.cs               # First-time setup helper
โ””โ”€โ”€ Samples/
    โ””โ”€โ”€ TestHCaptcha.cs                # Example OnGUI test scene

๐Ÿ“‹ Requirements

Requirement Version
Unity 2023.3 LTS or newer
Platform Android only
Min SDK API 22 (Android 5.1)
Target SDK API 34 (Android 14)
Gradle Templates REQUIRED (Custom Main + Settings)

โš ๏ธ Unity Version Compatibility

Unity 6.x (2023.3+) - โœ… Fully Supported

  • Pre-built AAR works out of the box
  • Enable Custom Gradle Templates and replace with package templates (step 5 above)

Unity 2022.3 LTS - โš ๏ธ Requires Rebuild

The included AAR is compiled with Java 17. Unity 2022.3 uses Java 11, causing build errors.

Solution:

  1. Clone the Android library source
  2. In Android Studio, set Java 11:
   compileOptions {
       sourceCompatibility JavaVersion.VERSION_11
       targetCompatibility JavaVersion.VERSION_11
   }
   kotlinOptions {
       jvmTarget = "11"
   }
  1. Rebuild AAR and replace in Plugins/Android/

๐Ÿงช Testing

In Unity Editor

Press Play โ†’ You'll see "Platform not supported" (expected - hCaptcha works only on Android)

On Android Device

  1. Build APK: File โ†’ Build Settings โ†’ Android โ†’ Build
  2. Install on device
  3. Run app and press verify button
  4. Complete captcha
  5. Check logcat for success token

๐Ÿ”ง Building from Source

Want to customize the Android library?

See Android/README.md for build instructions.

๐Ÿ› Troubleshooting

"Platform not supported"

  • Normal in Unity Editor - hCaptcha only works on Android devices

"Site Key not configured"

  • Set your site key in HCaptchaManager Inspector

Gradle build fails with "keepUnitySymbols.gradle does not exist"

  • Enable Custom Gradle Templates in Player Settings (see setup step 3)
  • Replace Unity's templates with package templates (see setup step 5)

"D8: java.lang.NullPointerException" or class version errors

  • Unity 2022.3: AAR needs Java 11 rebuild (see compatibility section above)
  • Unity 6+: Make sure you replaced templates (step 5)

"compileSdkVersion is not specified"

  • Replace Unity's generated templates with package templates (setup step 5)

๐Ÿ“– API Reference

HCaptchaManager

Methods:

  • Verify() - Start captcha verification
  • Reset() - Reset captcha state

Events:

  • OnSuccess(string token) - Fired when user passes captcha
  • OnFailure(string error) - Fired on error or user cancellation

Properties:

  • Instance - Singleton instance (auto-created from prefab)

๐Ÿค Contributing

Contributions welcome!

  1. Fork the repo
  2. Create feature branch: git checkout -b feature/amazing
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing
  5. Open Pull Request

๐Ÿ“ License

MIT License - see LICENSE

This project uses hCaptcha Android SDK (also MIT licensed).

๐Ÿ™ Credits

๐Ÿ“ง Support


โญ Star this repo if you find it useful!

Made with โค๏ธ for Unity developers