# Setup

Follow these steps to setup the Didomi Unity SDK:

* [Requirements](#add-the-sdk-to-your-project)
* ​[Add the SDK to your project​](#add-the-sdk-to-your-project)
* [​Initialize the SDK​](#initialize-the-sdk)
* ​[Setup the Didomi SDK UI​](#setup-the-sdk-ui)
* [​Configure the SDK​](#configure-the-sdk)

## Requirements <a href="#add-the-sdk-to-your-project" id="add-the-sdk-to-your-project"></a>

The Didomi Unity SDK currently supports Unity Version 2021.3.34f1 or newer.

For Android build, targeting Android SDK version 34 (Android 14) or newer is required (version required to publish apps on Google Play Store). When using Unity versions prior to 2022.3.38f1, this may require updating JDK and Gradle versions in Unity settings.

## Add the SDK to your project <a href="#add-the-sdk-to-your-project" id="add-the-sdk-to-your-project"></a>

Download the latest version of the `Didomi.unitypackage` from [our releases page on Github](https://github.com/didomi/unity/releases).

You can then import the package `Didomi.unitypackage` into your project:

![Install Didomi Unity SDK plugin](https://1703900661-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDh8ZWDZrXs8sc4QKEQ%2Fuploads%2Fgit-blob-2dcbe4a4c36270d20a55eef7987582b76b87a10e%2Fimage.png?alt=media)

{% hint style="info" %}
In case of errors after adding the SDK or at build time, check if issue is referenced in the [Troubleshooting](https://developers.didomi.io/cmp/mobile-sdk/unity-sdk/troubleshooting) section.
{% endhint %}

## Initialize the SDK

Once our SDK has been added to your project, you need to initialize it. The initialization process will prepare the SDK for interactions with the user and your application. It is important to launch the SDK initialization as soon as possible.

In your main entry code, call the `Initialize` method and pass your API key, and optionally your notice Id:

{% tabs %}
{% tab title="C#" %}

```swift
private void InitDidomi()
{
   // Setup a listener to know when the SDK is ready
   Didomi.GetInstance().OnReady( () => {
       // The Didomi SDK is ready to go, you can call other functions on the SDK
   });
   
   // Initialize the SDK
   Didomi.GetInstance().Initialize(
      new DidomiInitializeParameters(
         apiKey: "<Your API key>",
         noticeId: "<Your notice Id>"
   ));
}
```

{% endtab %}
{% endtabs %}

Keep in mind that the SDK initialization is an asynchronous process so you must avoid interacting with the `Didomi` object until it is actually ready to handle your requests. Use the `OnReady` event in C# to register a listener for the ready event.

Take a look at our [sample app](https://github.com/didomi/unity#sample-app) to see how the setup is done.

## Add a Proguard rule for Didomi (Android-only)

The Didomi SDK uses GSON to generate JSON-encoded API events sent back to the Didomi platform for analytics. We use `@SerializedName` to indicate the name of the JSON properties and ensure that they are correctly encoded into JSON.

In certain configurations, the host app configuration for Proguard will obfuscate some key SDK classes and break the API events sent to Didomi. This translates into incorrect analytics for the app on Android and is hard to catch as it usually only becomes apparent once in production (as debug/test builds usually do not run through Proguard).

If you are using Proguard in your Unity project and to ensure that this does not happen, add the following rule to your Proguard file:

```
-keep class io.didomi.sdk.apiEvents.** { *; })
```

If you want to check whether that issue applies to you, you can monitor the events sent by the Didomi SDK in your production build.

Correct events should look like:

![](https://1703900661-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDh8ZWDZrXs8sc4QKEQ%2Fuploads%2Fgit-blob-52092cf77e9e90c8a09a4443d45a2fcd280d9cc1%2Fimage.png?alt=media)

Notice how the `parameters` and `user.token` properties have a lot of data.

Incorrect events will look like this:

![](https://1703900661-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDh8ZWDZrXs8sc4QKEQ%2Fuploads%2Fgit-blob-be5674f678933b6c6f85daac80e1a95e6f11b930%2Fimage.png?alt=media)

Notice how the `parameters` and `user.token` properties are empty.

## Setup the SDK UI

{% hint style="info" %}
Note: the `SetupUI` method should be called only from your contexts where the application starts.

You do not need to call `OnReady`, `IsReady` or `ShouldConsentBeCollected` before calling `SetupUI` because they are called internally. Therefore, by calling this method the consent notice will be displayed if it is required and only once the SDK is ready.
{% endhint %}

Call the `SetupUI` function of the SDK in your context where the application starts:

{% tabs %}
{% tab title="C#" %}

```swift
public class AppStartup : MonoBehaviour 
{ 
    void Awake () 
    {
        Didomi.GetInstance().SetupUI();
    }
}
```

{% endtab %}
{% endtabs %}

### Deep links

If you are using deep links or have multiple main user interfaces in your Unity app, make sure that the `SetupUI` function is called on every context that the user can launch the app on.\
\
This will ensure that consent is always collected as needed and there is no path where the user can launch the app without consent being collected. If `SetupUI` is missing in some entry points, you will see lower consent rates as users will be using the app without giving consent.

## Configure the SDK

We support three options for configuring the UI and the behavior of the SDK:

* [Didomi Console](#from-the-console-recommended): the SDK is configured remotely from the Didomi Console
* [Local file](#local-file): the SDK is configured from a `didomi_config.json` file embedded in your app package
* [Remote file](#remote-file): the SDK is configured from a remote `didomi_config.json` file

### From the Console (Recommended)

You can configure the consent notice in your app by creating a notice in your Didomi Console. It will automatically be linked to your app through your API Key and, optionally, your app package name or notice id. You can access the Didomi console [here](http://console.didomi.io).

In order to enable this option, make sure to pass the `disableDidomiRemoteConfig` parameter as `false` when calling the initialize method as shown below.

{% tabs %}
{% tab title="C#" %}

```swift
Didomi.GetInstance().Initialize(
       apiKey: "<Your API key>",
       localConfigurationPath: null,
       remoteConfigurationURL: null,
       providerId: null,
       disableDidomiRemoteConfig: false,
       languageCode: null,
       noticeId: "<Your notice Id>");
```

{% endtab %}
{% endtabs %}

This is the default behavior when using the recommended parameter `DidomiInitializeParameters`:

{% tabs %}
{% tab title="C#" %}

```swift
Didomi.GetInstance().Initialize(
   new DidomiInitializeParameters(
      apiKey: "<Your API key>",
      noticeId: "<Your notice Id>"
));
```

{% endtab %}
{% endtabs %}

The SDK will automatically use the remote configuration hosted by Didomi and cache it locally. The cached version is refreshed every 60 minutes.\
If there is no connection available to download the remote configuration and no locally cached version, the SDK will try to use a local `didomi_config.json` configuration file as a fallback. See the Local option below for more information on how to configure the SDK through a local configuration file.

### Local file (Deprecated)

{% hint style="danger" %}
Using a local file automatically disables the TCF integration.\
If your app uses the TCF, you must use a configuration from the Didomi Console.
{% endhint %}

{% hint style="warning" %}
Using a local file will prevent you to support multiple regulations.
{% endhint %}

With this option, you create your own SDK configuration file and embed it in your app package.

The SDK behavior is configured in a `didomi_config.json` file that must be placed in the `Assets/DidomiConfig` folder of your Unity project.

You can create a file with the following content to get started:

{% tabs %}
{% tab title="didomi\_config.json" %}

```javascript
{
    "app": {
        "name": "My App Name",
        "privacyPolicyURL": "http://www.website.com/privacy",
        "vendors": {
            "iab": {
                "all": true
            }
        },
        "gdprAppliesGlobally": true,
        "gdprAppliesWhenUnknown": true
    }
}
```

{% endtab %}
{% endtabs %}

To be able set local configuration file for Didomi, you must create a `DidomiConfig` folder under the `Assets` folder of your Unity Project. Put your `didomi_config.json` file in the `Assets/DidomiConfig` folder.

Creating the `DidomiConfig` folder is **mandatory** for local configuration files as the configuration file must be moved to the correct directories when the Android and iOS projects are being generated.\
On the post process of the Unity project, the Didomi Plugin moves the files from `Assets/DidomiConfig` to the `assets/` folder of the Android apps and to the `Data/Resources` folder of iOS apps. For iOS projects, the configuration files will also be added to Copy Bundle Resources.

![DidomiConfig Folder for local configuration files.](https://1703900661-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LDh8ZWDZrXs8sc4QKEQ%2Fuploads%2Fgit-blob-8ec9581f1f7f1bcba1dc031d8859fb4db058e10c%2Fimage.png?alt=media)

You also need to disable loading the remote configuration to ensure that only the local file is loaded and that no HTTP request is sent. Update your [`initialize`](https://developers.didomi.io/cmp/mobile-sdk/unity-sdk/reference#initialize) call to set the `disableDidomiRemoteConfig` parameter to `true`:

{% tabs %}
{% tab title="C#" %}

```swift
Didomi.GetInstance().Initialize(
   new DidomiInitializeParameters(
       apiKey: "<Your API key>",
       disableDidomiRemoteConfig: true
));
```

{% endtab %}
{% endtabs %}

Your SDK is now setup. [Read the Getting started section](https://developers.didomi.io/cmp/mobile-sdk/consent-notice/getting-started) of our Mobile SDKs to learn more about how to configure it to match your app UI and requirements.

### Remote file

{% hint style="info" %}
Enabling this option will prevent the configuration from being loaded from the Didomi Console.
{% endhint %}

You can provide a remote URL for the SDK to download the `didomi_config.json` configuration file from. That allows you to update the SDK configuration without having to re-publish you mobile application.

When that configuration is enabled, the SDK will automatically use the remote configuration and cache it locally. The cached version is refreshed every 60 minutes. If there is no connection available to download the remote file and no locally cached version, the SDK will try to use the local `didomi_config.json` (provided in the app bundle) as a fallback.

To enable that option, change your call to [initialize ](https://developers.didomi.io/cmp/mobile-sdk/unity-sdk/reference#initialize)to provide the remote file URL:

{% tabs %}
{% tab title="C#" %}

```swift
Didomi.GetInstance().Initialize(
   new DidomiInitializeParameters(
       apiKey: "<Your API key>",
       remoteConfigurationURL: "http://www.website.com/didomi_config.json",
       disableDidomiRemoteConfig: true
));
```

{% endtab %}
{% endtabs %}

Also see the [reference documentation of the initialize](https://developers.didomi.io/cmp/mobile-sdk/reference#initialize) function for more information.
