Mobile Protect SDK to an Unity App

Mobile Protect SDK to an Unity App

Adding the SDK to your Unity Project

  1. If your project doesn’t already have it, install UDM4U: GitHub - googlesamples/unity-jar-resolver: Unity plugin which resolves Android & iOS dependencies and performs version management

  2. Clone the Mobile Protect repository
    git clone https://unity:MOBILEPROTECT_API_KEY@mobile-protect-repos.securetheorem.com/mobileprotect-unity

  3. In Unity

    1. Open Package Manager

    2. Select “Install package from disk…”

    3. Choose the package.json file from the cloned repository

Configure & launch MobileProtect

Create a C# script in Assets with the following content.

Change the calls to config.SetConfig as it best suits your app.

public class MobileProtectInitializer : MonoBehaviour { [RuntimeInitializeOnLoadMethod] static void InitOnAppStart() { var config = new MobileProtectConfiguration(); config.SetAuthenticationToken("YOUR_MOBILEPROTECT_AUTH_TOKEN"); config.SetConfig(MobileProtectConfiguration.ConfigKey.RootDetection, true); config.SetConfig(MobileProtectConfiguration.ConfigKey.DebuggerDetection, true); config.SetConfig(MobileProtectConfiguration.ConfigKey.CheatingDetection, true); // [...] MobileProtect.Initialize(config); } }

The AUTH_TOKEN can be found on our portal.

Not setting the values for a configuration means it will be in Observability mode.


Here are the Android API level supported by Mobile Protect for each Unity editor version:

Unity Version

API Level Supported

Unity Version

API Level Supported

6.X

24 and above

2022.3

24 and above

2021.3

24 and above

Further Configuration

Notification Callbacks

You can set Callbacks to be run when the SDK detects something in the app. There are termination callbacks and observability callbacks.

Termination callbacks are called when the SDK has detected something you have configured it to block (e.g.: App running with debugger enabled). They are called a few seconds before Mobile Protect terminates the App.

Observability callbacks are called for anything that SDK detects that is set to observability mode.

Use the following code to set the callbacks, modifying its content as it pleases you:

MobileProtect.SetAppTerminationCallback((reason) => { Debug.Log("The SDK detected " + reason + " and will now kill the App"); }); MobileProtect.SetAppObservabilityCallback((event) => { Debug.Log("New event detected : " + event); });

If you want to remove the callbacks, you can use the following code:

MobileProtect.ResetAppTerminationCallback(); MobileProtect.ResetAppObservabilityCallback();