---
title: Unity (Android)
slug: unity-android
createdAt: 2022-09-24T10:56:45.000Z
---

This guide is for anyone who wishes to integrate the Enefits Unity SDK for android o the Unity project.

We highly recommend reviewing the General Usage guide to familiarise yourself with general SDK workflow concepts before continuing.

You will need to generate an API key with your Enefits account before continuing with integration.

To obtain an API key, simply register at [https://x.enefits.co/](https://x.enefits.co/), and go to Developer in the account menu in the top right section. From there, generate an API key and save this for SDK initialization.

::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/dd3KiCpWNUSjvCl_hV_rZ_screenshot-2022-09-05-at-92346-am.png" size="36" width="624" height="1032" position="center" showCaption="false"}

We only show this once, so if you lose this API Key, you’ll have to re-generate it from the Developer section.

## Run the Demo

To run a live demo, download [](https://github.com/Enefits/enefits-unity-android-sdk/tree/main/unity-android-demo-app/EnefitsDemoApp)**[EnefitsDemoApp](https://github.com/Enefits/enefits-unity-android-sdk/tree/main/unity-android-demo-app/EnefitsDemoApp)**.

Open it with Unity and provide your own API key where requested.

Also, note that EnefitsDemoApp requires an android device to work. It will not work in the Unity Editor Or the android Emulator.

Make sure you have android Build options support available in Unity.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/Bw70wxmOgtv1dzlunMCXs_screenshot-2022-09-24-at-44435-pm.png)

Also, make sure you have support for android build OR android studio installed.

Go to Unity -> File -> Build Settings.

Make sure 'android' option is selected.

Select option 'Build', it will ask for a location where unity will generate Android Project.

Select a location, then it will generate Android Project at a specified location.

Open generated android project with android studio.

## Adding SDK dependencies to the Android project

Open build.gradle file of exportAndroid Project.

add jitpack maven to repositories.

```kotlin
maven { url 'https://jitpack.io' }
```

Also, add wallet connect dependencies.

```kotlin
classpath 'com.github.WalletConnect:kotlin-walletconnect-lib:0.9.9'
```

Open build.gradle file of Launcher module.

add the following dependencies.

```kotlin
implementation 'androidx.core:core-ktx:1.8.0'

implementation 'androidx.appcompat:appcompat:1.4.2'

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'


// WalletConnect

implementation 'com.github.walletconnect:kotlin-walletconnect-lib:0.9.8'

implementation 'com.github.komputing:khex:1.1.2'

// JSON

implementation 'com.squareup.moshi:moshi-adapters:1.13.0'

implementation 'com.squareup.moshi:moshi-kotlin:1.13.0'

// Http

implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.3'

implementation 'com.google.code.gson:gson:2.9.1'
```

## Download Enefits SDK Unity Package

The EnefitsSDK Unity android package can be downloaded from [this link.](https://github.com/Enefits/enefits-unity-android-sdk/tree/main/unity-package)


## Import Enefits SDK Unity Package

Select Project layout. Go to the Assets folder. Right-click and select Import Package -> Custom Package.


::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/-d3m0mr_OsPGWNWhUPUra_screenshot-2022-09-05-at-100521-pm.png" size="52" width="1082" height="912" position="center" showCaption="false"}

Select a downloaded Enefits package and choose 'Open'.

Make sure all components are selected from the package and then click on 'Import'.

::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/4oilLCUGqyZwZeFBkhg0g_screenshot-2022-09-24-at-113641-pm.png" size="50" width="924" height="1380" position="center" showCaption="false"}

## SDK Setup

Please make sure to call any SDK method inside 'UNITY\_ANDROID && !UNITY\_EDITOR' block, as it is only available for android device:

:::BlockQuote
\#if UNITY\_ANDROID && !UNITY\_EDITOR
&#x20;   // call SDK method here
\#endif
:::


Initialize The SDK
------------------

```csharp
    AndroidJavaClass unityPlayer;
    AndroidJavaObject unityActivity;
    AndroidJavaObject enefitsPlugin;

    EnefitsSessionCallback enefitsSessionCallback;
    EnefitsOffersCallback enefitsOffersCallback;
```

```csharp
private void Start()
{

    unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    enefitsPlugin = new AndroidJavaObject("com.enefits.connect.Enefits");

    #if !UNITY_EDITOR && UNITY_ANDROID
        unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    #endif
    enefitsSessionCallback = new EnefitsSessionCallback();
    enefitsOffersCallback = new EnefitsOffersCallback();
}
```

```csharp
 public void isSDKInit(){
     #if UNITY_ANDROID && !UNITY_EDITOR
        enefitsPlugin.Call("init", unityActivity, "<API_key>", "<App_Name>", enefitsSessionCallback);
    #endif
 }
```

\<App\_Name> - string type - describes your Unity application name

\<API\_key> - string type - API key provided to you during the registration at  [https://x.enefits.co/](https://x.enefits.co/).

**EnefitsSessionCallback.cs has a method 'onInitComplete', where you will get init call status.**

```csharp
  public void onInitComplete(bool status) {
   // status = true : sdk init success.
   // status = false: sdk init failed.
}
```


Connect Wallet
--------------

Display a button or call-to-action (for example: 'Connect Wallet') which when tapped will prompt the user to connect a wallet.

After the user selects a wallet and initiates a session, the returned address will be available to the Enefits SDK to check NFTs and any offers the user is eligible for.

This function will open a view with a list of supported providers

Enefits SDK fires the callback after successfully connecting with a blockchain account.


```csharp
 public void ConnectToWallet()
    {
     #if UNITY_ANDROID && !UNITY_EDITOR

        if (isSDKInitSuccess)
        {
            bool isAccountConnected = enefitsPlugin.Call<bool>("connectAccount");
        } else
        {
            // log message: Please init SDK first.
        }
    #endif
    }
```

**EnefitsSessionCallback.cs** has a method called '**onSessionConnected**', where you will get information about wallet connection address and info about blockchain.

```csharp
  public void onSessionConnected(string address, string blockchainInfo)
    {
        
    }
```

&#x20;and if the unity client app is not able to connect to the wallet then,

```csharp
  public void onSessionDisconnected()    {
        
    }
```

and if no supported wallet app is installed in device, then you will get **EnefitsSessionCallback.cs** callback at:

```csharp
      public void onAppInstalledOrNot(bool isAppInstalled) {
        //isAppInstalled = false : No supported wallet app installed
      }
```

## Get All Enefits Offers

This method returns all Offers that the user is eligible for based on the address they provided when they connected their wallet.

```csharp
 public void GetOffers() {
    #if UNITY_ANDROID && !UNITY_EDITOR
        if (isWalletConnected && isSDKInitSuccess) {
            enefitsPlugin.Call("getOffers", enefitsOffersCallback);
        }
    #endif
}
```

you will receive offers related information in **EnefitsOffersCallback.cs&#x20;**

```csharp
public void didfinishwithOffers(string error, string offers){

}
```

## Helper Functions

Use the functions provided below to create tighter integration with the Enefits SDK and manage user connectivity states.

### To Check if SDK init success or failed:

```csharp
 public void isSDKInit() {
    #if UNITY_ANDROID && !UNITY_EDITOR
        bool issuccess = enefitsPlugin.Call<bool>("isInitComplete");
        Debug.Log("is SDK init complete? : " + issuccess);
    #endif
}
```

### To Check if Enefits SDK was able to connect to a blockchain account.

```csharp
 public void isAccountConnnected() {
    #if UNITY_ANDROID && !UNITY_EDITOR
        bool issuccess = enefitsPlugin.Call<bool>("isAccountConnected");
        Debug.Log("is Account Connected ? : " + issuccess);
    #endif
}
```

### Get the address of the connected account.

```csharp
 public void ConnectedAccountInfo() {
    #if UNITY_ANDROID && !UNITY_EDITOR
        if (isWalletConnected && isSDKInitSuccess) {
            string accountInfo = enefitsPlugin.Call<string>("getConnectedAccount");
            Debug.Log("Connected Account info : " + accountInfo);
        }
    #endif
}
```

### Get the blockchain info for the connected account.

```csharp
public void GetChainData() {
    #if UNITY_ANDROID && !UNITY_EDITOR
        if (Logs.isWalletConnected && Logs.isSDKInitSuccess) {
            string chainDataInfo = enefitsPlugin.Call<string>("getChainData");
            Debug.Log("Chain Data info : " + chainDataInfo);
        }
    #endif           
}
```

### Disconnect from the blockchain account.

```csharp
 public void onSessionDisconnected(){
    Debug.Log("onSessionDisconnected");
}

```

Once the Wallet becomes disconnected, method **onSessionDisconnected** will be called from EnefitsSessionCallback.cs.




