// CashoutGuard for Unity (Android), calling the native Android SDK through JNI. Put this file anywhere under Assets/. // // 1. Add the Android SDK. With the External Dependency Manager (EDM4U, already in most games with ads), create // Assets/Editor/CashoutGuardDependencies.xml: // // // // https://cashoutguard.com/maven // // // // // Without EDM4U: Player Settings > Publishing Settings, tick "Custom Main Gradle Template" and "Custom Gradle Settings // Template", then add maven { url 'https://cashoutguard.com/maven' } to the repositories of settingsTemplate.gradle // and implementation 'com.cashoutguard:cashoutguard-android:1.1.0' to the dependencies of mainTemplate.gradle. // The SDK brings its own R8/ProGuard keep rules. // // 2. Once, at startup (public key only, never the secret key): // CashoutGuard.Init("pk_live_YOUR_PUBLIC_KEY"); // Before signup, offer clicks and cashouts: // CashoutGuard.Collect("cashout", requestId => { /* send requestId to your server with the request */ }); // Your server asks POST https://cashoutguard.com/v1/evaluate with that request_id and your secret key. // // Never throws: in the Editor, on iOS (no native SDK yet), on any error, or after 2 seconds the callback gets null and your // server evaluates without device data. The callback runs on Unity's main thread, so it can use UnityWebRequest directly. using System; using System.Threading; using UnityEngine; using UnityEngine.Scripting; public static class CashoutGuard { private const string Sdk = "com.cashoutguard.android.CashoutGuard"; /// Starts the SDK with your site's public key. Returns false when it could not (Editor, iOS, bad key). public static bool Init(string publicKey) { #if UNITY_ANDROID && !UNITY_EDITOR try { using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) using (var activity = player.GetStatic("currentActivity")) using (var sdk = new AndroidJavaClass(Sdk)) { return sdk.CallStatic("init", activity, publicKey); } } catch (Exception) { return false; // fail open: the game keeps working without device signals } #else return false; #endif } /// /// Scans the device for ("signup", "login", "offer_click", "cashout"...) and calls /// with the request id to send to your server, or null. Call it from Unity's main thread: the /// callback comes back on it. /// public static void Collect(string eventName, Action done) { #if UNITY_ANDROID && !UNITY_EDITOR try { using (var sdk = new AndroidJavaClass(Sdk)) { sdk.CallStatic("collect", eventName, new Callback(done, SynchronizationContext.Current)); } } catch (Exception) { done?.Invoke(null); } #else done?.Invoke(null); #endif } /// The SDK's com.cashoutguard.android.CollectCallback: answered on Android's UI thread, handed back to Unity's. /// [Preserve]: only Java calls onResult, so IL2CPP's code stripping would otherwise remove it. [Preserve] private sealed class Callback : AndroidJavaProxy { private readonly Action done; private readonly SynchronizationContext unity; public Callback(Action done, SynchronizationContext unity) : base("com.cashoutguard.android.CollectCallback") { this.done = done; this.unity = unity; } [Preserve] public void onResult(string requestId) { if (unity != null) { unity.Post(_ => done?.Invoke(requestId), null); } else { done?.Invoke(requestId); } } } }