Display custom elements with the Unity SDK
When a screen with a custom element opens, the Adapty SDK passes the element’s Type and ID to a resolver in your app code. The resolver returns one of the following:
- The view that matches the Type and ID: Users see the native view, such as a Lottie animation, an interactive chart, or a camera preview.
- No view: Users see the fallback, which is the content inside the custom element in the Flow & Paywall Builder.
Users of app versions without the resolver also see the fallback. Therefore, you can publish the flow before you release the app.
Before you start
You need:
- Adapty Unity SDK 4.4 or later: Earlier versions show the fallback for every custom element.
- Native code for iOS and Android: The Unity SDK has no C# API for custom elements. Write the views and the resolver in Swift for iOS and in Java for Android.
- A naming scheme for custom elements: Give each native view a Type, such as
lottie_animation. Use only lowercase letters, numbers, and underscores in Types. To distinguish between custom elements of the same Type, give each one an ID, such aswelcome_wave. The Types and IDs in your code must match the ones in the Flow & Paywall Builder.
Use the same Types and IDs in your iOS and Android code. Unless you set up a separate audience for each platform, iOS and Android users see the same flows.
Create views for custom elements
Write the native views for each platform:
- iOS: Write the views in Swift, as in the iOS guide. Save the Swift file in
Assets/Plugins/iOS. - Android: Write the views in Java, as in the Java examples of the Android guide. Save each class in its own file in
Assets/Plugins/Android. Start each file withpackage com.example.app;and the imports that its class uses.
The views in these guides show Lottie animations. To add Lottie and the animation files to your Unity project, follow steps 2 and 3 of Show user progress with a Lottie animation.
Write a resolver to match custom elements and views
Write the resolver for each platform:
- iOS: Write the resolver in Swift, as in the iOS guide, and save it in
Assets/Plugins/iOS. The Unity SDK adds theAdaptyUIBuildermodule to your iOS build, so your resolver can import it. Unity copies the files ofAssets/StreamingAssetsto theData/Rawfolder of the app bundle. To load a Lottie file from this folder, passsubdirectory: "Data/Raw", as in step 4 of the Lottie recipe. - Android: Write the resolver in Java, as in the Java examples of the Android guide, and save it in
Assets/Plugins/Android. Start the file withpackage com.example.app;and the imports that the resolver uses. The Unity SDK adds the Adapty UI library to your Gradle build, so your resolver can use thecom.adapty.uiclasses.
Set conditions for fallback content
To show users the fallback content instead of an empty custom element, return no view from the native resolver in the following situations:
- Nothing to visualize: The animation file or the data that the view needs is missing.
- Nothing to process: The user skipped a quiz question, and the data is incomplete.
- Access prohibited: The user denied a permission that the view needs.
Write these conditions in the native resolver, as in the iOS and Android guides.
To show a native view that needs a permission, ask for the permission on an earlier screen of the flow, with the Request permission action. Your C# permission handler shows the dialog and passes the result to the flow. In the resolver, return the native view if the user granted the permission, and no view otherwise. A permission change doesn’t make the SDK call your resolver again, so after a request on the same screen, users still see the fallback. For example, in Show nearby places on a map in a flow, users grant or decline location access on the screen before the map.
Pass the resolver to the SDK
To display your native views in every flow, set the resolver once, at app launch, before you create the first flow view. Your C# code calls the native code that sets the resolver:
- iOS: A C function that your Swift file exports.
- Android: A static method of your Java class.
In the following C# class, the RuntimeInitializeOnLoadMethod attribute makes Unity call the Register method at launch, before the first scene loads:
using UnityEngine;
public static class CustomElements
{
#if UNITY_IOS && !UNITY_EDITOR
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void RegisterCustomElements();
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Register()
{
#if UNITY_IOS && !UNITY_EDITOR
RegisterCustomElements();
#elif UNITY_ANDROID && !UNITY_EDITOR
using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity"))
using (var context = activity.Call<AndroidJavaObject>("getApplicationContext"))
using (var resolver = new AndroidJavaClass("com.example.app.CustomElements"))
{
resolver.CallStatic("register", context);
}
#endif
}
}iOS
In the Swift file, add a function that passes your resolver to AdaptyUI.setCustomElementsResolver. To make this function callable from C#, export it with the @_cdecl attribute, under the name that the C# class declares. In the following function, CustomElementsResolver is the resolver from the iOS guide:
import AdaptyUI
// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
Task { @MainActor in
AdaptyUI.setCustomElementsResolver(CustomElementsResolver())
}
}Android
In the Java file, add a static register method that passes your resolver to AdaptyUI.setCustomElementsResolver. C# calls this method by the full name of the class, com.example.app.CustomElements. If you change the package or the class name, update the full name in the C# code too.
In the following method, CustomElementsResolver is the resolver from the Android guide. The constructor of CustomElementsResolver takes a Context, so the C# class passes the application context to register:
package com.example.app;
import android.content.Context;
import com.adapty.ui.AdaptyUI;
public final class CustomElements {
// Called once from C# at launch, before any flow is created.
public static void register(Context context) {
AdaptyUI.setCustomElementsResolver(new CustomElementsResolver(context));
}
}When Minify is on, Unity removes unused Java code from the Android build. Only your C# code calls CustomElements, so Unity treats this class as unused. Without it, your native views don’t appear in flows. To keep the class in the build, in Project Settings > Player > Publishing Settings, select Custom Proguard File. Then add the following line to the file that Unity creates in Assets/Plugins/Android:
-keep class com.example.app.CustomElements { *; }Test on a device
The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. An error in the native view can crash the app. Before you release the app, test every screen that has a custom element on an iOS device and on an Android device. Follow the steps in the iOS and Android guides, with the following changes:
- To see the warnings of the Adapty SDK in the Xcode console and in Logcat, set the log level to
AdaptyLogLevel.Warnor a more detailed level. - If the fallback appears instead of the native view, verify that your C# class calls the native code at launch. In an Android build with Minify on, also verify that your ProGuard rules keep the Java class.
Examples
Each recipe covers the setup in the builder and the code for your app:
- Show user progress with a Lottie animation: A ring that fills as a streak grows.
- Show an interactive weight goal chart in a paywall: A weight goal chart, in the paywall flow that follows the onboarding flow.
- Show nearby places on a map in a flow: A map of fitness centers near each user.
The Unity SDK repository has a demo app with a custom element. To see a practical application of this guide in live code, look at these repo files:
Assets/Scripts/DemoCustomElements.cs: The C# class that calls the native code at launch.Assets/Plugins/iOS/AdaptyDemoCustomElements.swift: The resolver and the SwiftUI view for thereview_cardType.Assets/Plugins/Android/DemoCustomElements.java: The resolver and the AndroidViewfor thereview_cardType.