SDK Guide
...
For Mobile Apps/Games
Unity

Unity (Android)

20min

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/, 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.

Document image


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 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.

Document image


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


Also, add wallet connect dependencies.

Kotlin


Open build.gradle file of Launcher module.

add the following dependencies.

Kotlin


Download Enefits SDK Unity Package

The EnefitsSDK Unity android package can be downloaded from this link.

Import Enefits SDK Unity Package

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

Document image


Select a downloaded Enefits package and choose 'Open'. Make sure all components are selected from the package and then click on 'Import'.

Document image


SDK Setup

Please make sure to call any SDK method inside 'UNITY_ANDROID && !UNITY_EDITOR' block, as it is only available for android device:

#if UNITY_ANDROID && !UNITY_EDITOR // call SDK method here #endif

Initialize The SDK

C#

C#

C#


<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/.

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

C#


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.

C#


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

C#


and if the unity client app is not able to connect to the wallet then,

C#


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

C#


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.

C#


you will receive offers related information in EnefitsOffersCallback.cs

C#


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:

C#


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

C#


Get the address of the connected account.

C#


Get the blockchain info for the connected account.

C#


Disconnect from the blockchain account.

C#


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