SDK Guide
...
For Mobile Apps/Games
Unity
Unity (Android)
20 min
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 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 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 maven { url 'https //jitpack io' } also, add wallet connect dependencies classpath 'com github walletconnect\ kotlin walletconnect lib 0 9 9' open build gradle file of launcher module add the following dependencies 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 import enefits sdk unity package select project layout go to the assets folder right click and select import package > custom package select a downloaded enefits package and choose 'open' make sure all components are selected from the package and then click on 'import' 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 androidjavaclass unityplayer; androidjavaobject unityactivity; androidjavaobject enefitsplugin; enefitssessioncallback enefitssessioncallback; enefitsofferscallback enefitsofferscallback; 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(); } 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/ enefitssessioncallback cs has a method 'oninitcomplete', where you will get init call status 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 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 public void onsessionconnected(string address, string blockchaininfo) { } and if the unity client app is not able to connect to the wallet then, public void onsessiondisconnected() { } and if no supported wallet app is installed in device, then you will get enefitssessioncallback cs callback at 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 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 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 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 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 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 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 public void onsessiondisconnected(){ debug log("onsessiondisconnected"); } once the wallet becomes disconnected, method onsessiondisconnected will be called from enefitssessioncallback cs