Skip to main content

Unity - Adapty SDK installation & configuration

Adapty SDK includes two key modules for seamless integration into your mobile app:

  • Core Adapty: This essential SDK module is required for Adapty to function properly in your app.
  • AdaptyUI: This optional module is needed if you use the Adapty Paywall Builder, a user-friendly, no-code tool for easily creating cross-platform paywalls. These paywalls are built with a visual constructor right in our dashboard, run natively on the device, and require minimal effort to create high-performing designs.

Install Adapty SDKs

To install the Adapty SDKs:

  1. Download the adapty-unity-plugin-*.unitypackage from GitHub and import it into your project.

  1. Download the adapty-ui-unity-plugin-*.unitypackage from GitHub and import it into your project.

  1. Download and import the External Dependency Manager plugin.

  2. The SDK uses the "External Dependency Manager" plugin to handle iOS Cocoapods dependencies and Android gradle dependencies. After the installation, you may need to invoke the dependency manager:

    Assets -> External Dependency Manager -> Android Resolver -> Force Resolve

    and

    Assets -> External Dependency Manager -> iOS Resolver -> Install Cocoapods

  3. When building your Unity project for iOS, you would get Unity-iPhone.xcworkspace file, which you have to open instead of Unity-iPhone.xcodeproj, otherwise, Cocoapods dependencies won't be used.

Configure Adapty SDK

To configure the Adapty SDK for Unity, start by initializing the Adapty Unity Plugin and then using it as described in the guidance below. Additionally, ensure to set up your logging system to receive errors and other important information from Adapty.

  1. Activate you Adapty SDK. You only need to activate it once, typically early in your app's lifecycle.

    C#
    using UnityEngine;
    using AdaptySDK;

    public class AdaptyListener : MonoBehaviour, AdaptyEventListener {
    void Start() {
    DontDestroyOnLoad(this.gameObject);
    Adapty.SetEventListener(this);


    var builder = new AdaptyConfiguration.Builder("YOUR_API_TOKEN")
    .SetCustomerUserId(null)
    .SetObserverMode(false)
    .SetServerCluster(AdaptyServerCluster.Default)
    .SetIPAddressCollectionDisabled(false)
    .SetIDFACollectionDisabled(false);
    .SetActivateUI(true)
    .SetAdaptyUIMediaCache(
    100 * 1024 * 1024, // MemoryStorageTotalCostLimit 100MB
    null, // MemoryStorageCountLimit
    100 * 1024 * 1024 // DiskStorageSizeLimit 100MB
    );

    Adapty.Activate(builder.Build(), (error) => {
    if (error != null) {
    // handle the error
    return;
    }
    });
    }

    public void OnLoadLatestProfile(Adapty.Profile profile) {
    // handle updated profile data
    }
    }
    }
    ParameterPresenceDescription
    YOUR_API_TOKENrequiredThe key you can find in the Public SDK key field of your app settings in Adapty: App settings-> General tab -> API keys subsection
    withLogLeveloptionalAdapty logs errors and other crucial information to provide insight into your app's functionality. There are the following available levels:
    • error: Only errors will be logged.
    • warn: Errors and messages from the SDK that do not cause critical errors, but are worth paying attention to will be logged.
    • info: Errors, warnings, and serious information messages, such as those that log the lifecycle of various modules will be logged.
    • verbose: Any additional information that may be useful during debugging, such as function calls, API queries, etc. will be logged.
    withObserverModeoptional

    A boolean value controlling Observer mode. Turn it on if you handle purchases and subscription status yourself and use Adapty for sending subscription events and analytics.

    The default value is false.

    🚧 When running in Observer mode, Adapty SDK won't close any transactions, so make sure you're handling it.

    withCustomerUserIdoptionalAn identifier of the user in your system. We send it in subscription and analytical events, to attribute events to the right profile. You can also find customers by customerUserId in the Profiles and Segments menu.
    withIdfaCollectionDisabledoptional

    Set to true to disable IDFA collection and sharing.

    For more details on IDFA collection, refer to the Analytics integration section.

    withIpAddressCollectionDisabledoptional

    Set to true to disable user IP address collection and sharing.

    The default value is false.

  2. Create a script to listen to Adapty events. Name it AdaptyListener in your scene. We suggest using the DontDestroyOnLoad method for this object to ensure it persists throughout the application's lifespan.

    Adapty uses AdaptySDK namespace. At the top of your script files that use the Adapty SDK, you may add

    C#
    using AdaptySDK;
  3. Subscribe to Adapty events:

    C#
    using UnityEngine;
    using AdaptySDK;

    public class AdaptyListener : MonoBehaviour, AdaptyEventListener {
    void Start() {
    DontDestroyOnLoad(this.gameObject);
    Adapty.SetEventListener(this);


    var builder = new AdaptyConfiguration.Builder("YOUR_API_TOKEN")
    .SetCustomerUserId(null)
    .SetObserverMode(false)
    .SetServerCluster(AdaptyServerCluster.Default)
    .SetIPAddressCollectionDisabled(false)
    .SetIDFACollectionDisabled(false);
    .SetActivateUI(true)
    .SetAdaptyUIMediaCache(
    100 * 1024 * 1024, // MemoryStorageTotalCostLimit 100MB
    null, // MemoryStorageCountLimit
    100 * 1024 * 1024 // DiskStorageSizeLimit 100MB
    );

    Adapty.Activate(builder.Build(), (error) => {
    if (error != null) {
    // handle the error
    return;
    }
    });
    }

    public void OnLoadLatestProfile(Adapty.Profile profile) {
    // handle updated profile data
    }
    }
    }
  4. We recommend adjusting the Script Execution Order to place the AdaptyListener before Default Time. This ensures that Adapty initializes as early as possible.

Please keep in mind that for paywalls and products to be displayed in your mobile application, and for analytics to work, you need to display the paywalls and, if you're using paywalls not created with the Paywall Builder, handle the purchase process within your app.

Add Kotlin Plugin to your project

If you're using the Paywall Builder, make sure to add the Kotlin Plugin.

warning

Skipping this step can cause your mobile app to crash when the paywall is displayed.

  1. In Player Settings, ensure that the Custom Launcher Gradle Template and Custom Base Gradle Template options are selected.

  2. Add the following line to /Assets/Plugins/Android/launcherTemplate.gradle:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply from: 'setupSymbols.gradle'
    apply from: '../shared/keepUnitySymbols.gradle'
  3. Add the following line to /Assets/Plugins/Android/baseProjectTemplate.gradle:

    plugins {
    // If you are changing the Android Gradle Plugin version, make sure it is compatible with the Gradle version preinstalled with Unity
    // See which Gradle version is preinstalled with Unity here https://docs.unity3d.com/Manual/android-gradle-overview.html
    // See official Gradle and Android Gradle Plugin compatibility table here https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
    // To specify a custom Gradle version in Unity, go do "Preferences > External Tools", uncheck "Gradle Installed with Unity (recommended)" and specify a path to a custom Gradle version
    id 'com.android.application' version '8.3.0' apply false
    id 'com.android.library' version '8.3.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
    **BUILD_SCRIPT_DEPS**
    }

Set up the logging system

Adapty logs errors and other important information to help you understand what is going on. There are three levels available:

errorOnly errors will be logged.
warnErrors and messages from the SDK that do not cause critical errors, but are worth paying attention to will be logged.
infoErrors, warnings, and serious information messages, such as those that log the lifecycle of various modules will be logged.
verboseAny additional information that may be useful during debugging, such as function calls, API queries, etc. will be logged.

You can call SetLogLevel() method in your app before configuring Adapty.