Integrating the Braze Unity SDK
Learn how to integrate and customize the Braze Unity SDK. For a full list of types, functions, variables, and more, see Unity Declaration File. To learn more about the SDK in general, see Getting started: Integration overview.
If you already set up a manual Unity integration for iOS, you can choose to use an automated integration instead. For a full walkthrough, see Switch to an automated integration.
Prerequisites
Before you start, verify your environment is supported by the latest Braze Unity SDK version.
Integrating the SDK
Step 1: Choose your Braze Unity package
The Braze .unitypackage
bundles native bindings for the Android and iOS platforms, along with a C# interface.
There are several Braze Unity packages available for download on the Braze Unity releases page:
Appboy.unitypackage
- This package bundles the Braze Android and iOS SDKs and the SDWebImage dependency for the iOS SDK, which is required for the proper functionality of Braze in-app messaging, and Content Cards features on iOS. The SDWebImage framework is used for downloading and displaying images, including GIFs. If you intend on utilizing full Braze functionality, download and import this package.
Appboy-nodeps.unitypackage
- This package is similar to
Appboy.unitypackage
except for the SDWebImage framework is not present. This package is useful if you do not want the SDWebImage framework present in your iOS app.
- This package is similar to
As of Unity 2.6.0, the bundled Braze Android SDK artifact requires AndroidX dependencies. If you were previously using a jetified unitypackage
, then you can safely transition to the corresponding unitypackage
.
The Braze .unitypackage
bundles native bindings for the Android and iOS platforms, along with a C# interface.
The Braze Unity package is available for download on the Braze Unity releases page with two integration options:
Appboy.unitypackage
only- This package bundles the Braze Android and iOS SDKs without any additional dependencies. With this integration method, there will not be proper functionality of Braze in-app messaging, and Content Cards features on iOS. If you intend on utilizing full Braze functionality without custom code, use the option below instead.
- To use this integration option, ensure that the box next to
Import SDWebImage dependency
is unchecked in the Unity UI under “Braze Configuration”.
Appboy.unitypackage
withSDWebImage
- This integration option bundles the Braze Android and iOS SDKs and the SDWebImage dependency for the iOS SDK, which is required for the proper functionality of Braze in-app messaging, and Content Cards features on iOS. The
SDWebImage
framework is used for downloading and displaying images, including GIFs. If you intend on utilizing full Braze functionality, download and import this package. - To automatically import
SDWebImage
, be sure to check the box next toImport SDWebImage dependency
in the Unity UI under “Braze Configuration”.
- This integration option bundles the Braze Android and iOS SDKs and the SDWebImage dependency for the iOS SDK, which is required for the proper functionality of Braze in-app messaging, and Content Cards features on iOS. The
To see if you require the SDWebImage dependency for your iOS project, visit the iOS in-app message documentation.
Step 2: Import the package
In the Unity Editor, import the package into your Unity project by navigating to Assets > Import Package > Custom Package. Next, click Import.
Alternatively, follow the Unity asset package import instructions for a more detailed guide on importing custom Unity packages.
If you only wish to import the iOS or Android plugin, deselect the Plugins/Android
or Plugins/iOS
subdirectory when importing the Braze .unitypackage
.
In the Unity Editor, import the package into your Unity project by navigating to Assets > Import Package > Custom Package. Next, click Import.
Alternatively, follow the Unity asset package import instructions for a more detailed guide on importing custom Unity packages.
If you only wish to import the iOS or Android plugin, deselect the Plugins/Android
or Plugins/iOS
subdirectory when importing the Braze .unitypackage
.
Step 3: Configure the SDK
Step 3.1: Configure AndroidManifest.xml
To fullo AndroidManifest.xml
to function. If your app does not have an AndroidManifest.xml
, you can use the following as a template. Otherwise, if you already have an AndroidManifest.xml
, ensure that any of the following missing sections are added to your existing AndroidManifest.xml
.
- Go to the
Assets/Plugins/Android/
directory and open yourAndroidManifest.xml
file. This is the default location in the Unity editor. - In your
AndroidManifest.xml
, add the required permissions and activities from in the following template. - When you’re finished, your
AndroidManifest.xml
should only contain a single Activity with"android.intent.category.LAUNCHER"
present.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="REPLACE_WITH_YOUR_PACKAGE_NAME">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/app_icon"
android:label="@string/app_name">
<!-- Calls the necessary Braze methods to ensure that analytics are collected and that push notifications are properly forwarded to the Unity application. -->
<activity android:name="com.braze.unity.BrazeUnityPlayerActivity"
android:theme="@style/UnityThemeSelector"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:screenOrientation="sensor">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- A Braze specific FirebaseMessagingService used to handle push notifications. -->
<service android:name="com.braze.push.BrazeFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
All Activity classes registered in your AndroidManifest.xml
file should be fully integrated with the Braze Android SDK, otherwise your analytics won’t be collected. If you add your own Activity class, be sure you extend the Braze Unity player so you can prevent this.
Step 3.2: Update AndroidManifest.xml
with your package name
To find your package name, click File > Build Settings > Player Settings > Android Tab.
In your AndroidManifest.xml
, all instances of REPLACE_WITH_YOUR_PACKAGE_NAME
should be replaced with your Package Name
from the previous step.
Step 3.3: Add gradle dependencies
To add gradle dependencies to your Unity project, first enable “Custom Main Gradle Template” in your Publishing Settings. This will create a template gradle file that your project will use. A gradle file handles setting dependencies and other build-time project settings. For more information, check out the Braze Unity sample app’s mainTemplate.gradle.
The following dependencies are required:
1
2
3
4
5
6
implementation 'com.google.firebase:firebase-messaging:22.0.0'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation "androidx.recyclerview:recyclerview:1.2.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"
implementation 'androidx.core:core:1.6.0'
You may also set these dependencies using the External Dependency Manager.
Step 3.4: Automate the Unity Android integration
Braze provides a native Unity solution for automating the Unity Android integration.
- In the Unity Editor, open the Braze Configuration Settings by navigating to Braze > Braze Configuration.
- Check the Automate Unity Android Integration box.
- In the Braze API Key field, input your application’s API key found in Manage Settings from the Braze dashboard.
This automatic integration should not be used with a manually created braze.xml
file since the configuration values may conflict during project building. If you require a manual braze.xml
, disable the automatic integration.
Step 3.1: Set your API key
Braze provides a native Unity solution for automating the Unity iOS integration. This solution modifies the built Xcode project using Unity’s PostProcessBuildAttribute
and subclasses the UnityAppController
using the IMPL_APP_CONTROLLER_SUBCLASS
macro.
- In the Unity Editor, open the Braze Configuration Settings by navigating to Braze > Braze Configuration.
- Check the Automate Unity iOS Integration box.
- In the Braze API Key field, input your application’s API key found in Manage Settings.
If your application is already using another UnityAppController
subclass, you will need to merge your subclass implementation with AppboyAppDelegate.mm
.
Customizing the Unity package
Step 1: Clone the repository
In your terminal, clone the Braze Unity SDK GitHub repository, then navigate to that folder:
1
2
git clone [email protected]:braze-inc/braze-unity-sdk.git
cd ~/PATH/TO/DIRECTORY/braze-unity-sdk
1
2
git clone git@github.com:braze-inc/braze-unity-sdk.git
cd C:\PATH\TO\DIRECTORY\braze-unity-sdk
Step 2: Export package from repository
First, launch Unity and keep it running in the background. Then, in the repository root, run the following command to export the package to braze-unity-sdk/unity-package/
.
1
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -nographics -projectPath "$(pwd)" -executeMethod Appboy.Editor.Build.ExportAllPackages -quit
1
"%UNITY_PATH%" -batchmode -nographics -projectPath "%PROJECT_ROOT%" -executeMethod Appboy.Editor.Build.ExportAllPackages -quit
If you experience any issues after running these commands, refer to Unity: Command Line Arguments.
Step 3: Import package into Unity
- In Unity, import the desired package into your Unity project by navigating to Assets > Import Package > Custom Package.
- If there’s any files you don’t want want to import, deselect them now.
- Customize the exported Unity package located in
Assets/Editor/Build.cs
.
Switch to an automated integration (Swift only)
To take advantage of the automated iOS integration offered in the Braze Unity SDK, follow these steps on transitioning from a manual to an automated integration.
- Remove all Braze-related code from your Xcode project’s
UnityAppController
subclass. - Remove Braze iOS libraries from your Unity or Xcode project (such as
Appboy_iOS_SDK.framework
andSDWebImage.framework
). - Import the Braze Unity package into your project again. For a full walkthrough, see Step 2: Import the package.
- Set your API key again. For a full walkthrough, see Step 3.1: Set your API key.
Optional configurations
Verbose logging
To enable verbose logging in the Unity Editor, do the following:
- Open the Braze Configuration Settings by navigating to Braze > Braze Configuration.
- Click the Show Braze Android Settings dropdown.
- In the SDK Log Level field, input the value “0”.
Prime 31 compatibility
To use the Braze Unity plugin with Prime31 plugins, edit your project’s AndroidManifest.xml
to use the Prime31 compatible Activity classes. Change all references of
com.braze.unity.BrazeUnityPlayerActivity
to com.braze.unity.prime31compatible.BrazeUnityPlayerActivity
Amazon Device Messaging (ADM)
Braze supports integrating ADM push into Unity apps. If you want to integrate ADM push, create a file called api_key.txt
containing your ADM API key and place it in the Plugins/Android/assets/
folder. For more information on integrating ADM with Braze, visit our ADM push integration instructions.
Extending the Braze Unity player (Android only)
The example AndroidManifest.xml
file provided has one Activity class registered, BrazeUnityPlayerActivity
. This class is integrated with the Braze SDK and extends UnityPlayerActivity
with session handling, in-app message registration, push notification analytics logging, and more. See Unity for more information on extending the UnityPlayerActivity
class.
If you are creating your own custom UnityPlayerActivity
in a library or plugin project, you will need to extend our BrazeUnityPlayerActivity
to integrate your custom functionality with Braze. Before beginning work on extending BrazeUnityPlayerActivity
, follow our instructions for integrating Braze into your Unity project.
- Add the Braze Android SDK as a dependency to your library or plugin project as described in the Braze Android SDK integration instructions.
- Integrate our Unity
.aar
, which contains our Unity-specific functionality, to your Android library project you are building for Unity. Theappboy-unity.aar
is available from our public repo. After our Unity library is successfully integrated, modify yourUnityPlayerActivity
to extendBrazeUnityPlayerActivity
. - Export your library or plugin project and drop it into
/<your-project>/Assets/Plugins/Android
as normal. Do not include any Braze source code in your library or plugin as they will already be present in/<your-project>/Assets/Plugins/Android
. - Edit your
/<your-project>/Assets/Plugins/Android/AndroidManifest.xml
to specify yourBrazeUnityPlayerActivity
subclass as the main activity.
You should now be able to package an .apk
from the Unity IDE that is fully integrated with Braze and contains your custom UnityPlayerActivity
functionality.
Troubleshooting
Error: “File could not be read”
Errors resembling the following may be safely ignored. Apple software uses a proprietary PNG extension called CgBI, which Unity does not recognize. These errors will not affect your iOS build or the proper display of the associated images in the Braze bundle.
1
Could not create texture from Assets/Plugins/iOS/AppboyKit/Appboy.bundle/...png: File could not be read