Unity SDK
CleverTap provides a Unity Plugin that enables app developers to track, segment, and engage their users.
Note
If you are using AndroidX dependencies in your project, refer v2.0.0
Install
-
Import the CleverTapUnityPlugin.unitypackage into your Unity Project (
Assets
>Import Package
>Custom Package
) or manually copyPlugin/CleverTapUnity
,Plugin/PlayServicesResolver
andPlugin/Plugins/Android
(or copy the files inPlugin/Plugins/Android
to your existingAssets/Plugins/Android
directory) into the Assets directory of your Unity Project. -
Create an empty game object (GameObject > Create Empty) and rename it as
CleverTapUnity
. AddAssets/CleverTapUnity/CleverTapUnity-Scripts/CleverTapUnity.cs
as a component of theCleverTapUnity GameObject
.


- Select the
CleverTapUnity GameObject
that you created in the Hierarchy pane and add your CleverTap settings inside the Inspector window. You must include yourCleverTap Account ID
andCleverTap Account Token
from your CleverTap Dashboard -> Settings.


- Edit
Assets/CleverTapUnity/CleverTapUnity-Scripts/CleverTapUnity.cs
to add your calls to the CleverTap SDK. See usage examples in example/CleverTapUnity.cs. For more information, see our developer guide.
From CleverTap Unity SDK 2.1.2 onwards, you can optionally disable the usage of IDFV for CleverTap ID by checking CLEVERTAP_DISABLE_IDFV. By default, the value of CLEVERTAP_DISABLE_IDFV is not checked.
This is recommended for apps that send data from different iOS apps to a common CleverTap account.
iOS Specific Instructions
- If you want to enable Push Notifications, be sure to add the Push Notifications capability to your Xcode project.


- Add a run script to your build phases. In Xcode, go to your Targets, under your appâs name, select Build Phases after embed frameworks, add a run script phase, and set it to use
/bin/sh
and the iOS script:


The script scans your built applicationâs Frameworks
folder and strips the unnecessary simulator architectures from the CleverTakSDK.framework prior to archiving/submitting the app store.
- Build and run your iOS project.
Android Specific Instructions
Steps for Android X
Use these additional steps only if you are using Android X dependencies.
Go to
File
>Build Settings
>Android
>Player Settings
>Publishing Settings
>Build
.
Ensure theCustom Gradle Template
option is checked.Go to
Assets
>Play Services Resolver
>Android Resolver
>Settings
.
Ensure thatUse Jetifier
andPatch mainTemplate.gradle
options are checked.
- To enable Push Notifications, add the Firebase Unity SDK to your app. For more information, see Firebase Unity Setup Docs.
Note
The Firebase Unity SDK may override your AndroidManifest.xml file. If that happens, revert to your original manifest file.
-
Run
Assets
>Play Services Resolver
>Android Resolver
>Resolve Client Jars
from the Unity menu bar to install the required google play services and android support library dependencies. -
Edit the
AndroidManifest.xml
file inAssets/Plugins/Android
to add your Bundle Identifier, FCM Sender ID, CleverTap Account Id, CleverTap Token and Deep Link url scheme (if applicable):
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="YOUR_BUNDLE_IDENTIFIER"
android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<meta-data
android:name="GCM_SENDER_ID"
android:value="id:YOUR_FCM_SENDER_ID"/>
<meta-data
android:name="CLEVERTAP_ACCOUNT_ID"
android:value="Your CleverTap Account ID"/>
<meta-data
android:name="CLEVERTAP_TOKEN"
android:value="Your CleverTap Account Token"/>
<!-- Deep Links uncomment and replace YOUR_URL_SCHEME, if applicable, or remove if not supporting deep links-->
<!--
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="YOUR_URL_SCHEME" />
</intent-filter>
-->
- Add the following in the
AndroidManifest.xml
file:
<service
android:name="com.clevertap.android.sdk.FcmTokenListenerService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.clevertap.android.sdk.FcmMessageListenerService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
-
Add your
google-services.json
file to the Assets folder of the project. -
To enable A/B UI editor, edit
Assets/CleverTapUnity/CleverTapUnity-Scripts/CleverTapBinding.cs
andCleverTapAPI.CallStatic("setUIEditorConnectionEnabled", true)
call just before getting the clevertap instance.
public static AndroidJavaObject CleverTap {
get {
if (clevertap == null) {
AndroidJavaObject context = unityCurrentActivity.Call<AndroidJavaObject>("getApplicationContext");
//set the UI editor flag before getting the Clevertap instance, defaults to false.
CleverTapAPI.CallStatic("setUIEditorConnectionEnabled", true);
clevertap = CleverTapAPI.CallStatic<AndroidJavaObject>("getInstance", context);
}
return clevertap;
}
}
- Build your app or Android project as usual.
Updated about 1 year ago