---
title: "Display custom elements with the Unity SDK"
description: "Display a native iOS or Android view in each custom element of a flow, and test the flow on a device."
---

> For the complete documentation index, see [llms.txt](https://adapty.io/docs/llms.txt).

> **AI agents**: to search Adapty docs faster and with fewer tokens, install the Adapty skills: `npx adapty skills install --yes`

When a screen with a [custom element](flow-custom-elements) 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 as `welcome_wave`. The Types and IDs in your code must match the ones in the [Flow & Paywall Builder](flow-custom-elements#add-and-configure).

Use the same Types and IDs in your iOS and Android code. Unless you set up a separate [audience](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](ios-flow-custom-elements#create-views-for-custom-elements). Save the Swift file in `Assets/Plugins/iOS`.
- **Android**: Write the views in Java, as in the Java examples of the [Android guide](android-flow-custom-elements#create-views-for-custom-elements). Save each class in its own file in `Assets/Plugins/Android`. Start each file with `package 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](unity-custom-element-lottie-animation#2-add-lottie-to-your-project).

## 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](ios-flow-custom-elements#write-a-resolver-to-match-custom-elements-and-views), and save it in `Assets/Plugins/iOS`. The Unity SDK adds the `AdaptyUIBuilder` module to your iOS build, so your resolver can import it. Unity copies the files of `Assets/StreamingAssets` to the `Data/Raw` folder of the app bundle. To load a Lottie file from this folder, pass `subdirectory: "Data/Raw"`, as in step 4 of the [Lottie recipe](unity-custom-element-lottie-animation#4-add-the-native-code).
- **Android**: Write the resolver in Java, as in the Java examples of the [Android guide](android-flow-custom-elements#write-a-resolver-to-match-custom-elements-and-views), and save it in `Assets/Plugins/Android`. Start the file with `package 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 the `com.adapty.ui` classes.

### Set conditions for fallback content

To show users the [fallback content](flow-custom-elements#set-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](ios-flow-custom-elements#set-conditions-for-fallback-content) and [Android](android-flow-custom-elements#set-conditions-for-fallback-content) 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**](flow-permissions) action. Your C# [permission handler](unity-handle-permissions) 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](unity-custom-element-map#1-ask-for-location-access), 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:

```csharp showLineNumbers title="Assets/Scripts/CustomElements.cs"
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](ios-flow-custom-elements#write-a-resolver-to-match-custom-elements-and-views):

```swift showLineNumbers title="Assets/Plugins/iOS/CustomElements.swift"
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](android-flow-custom-elements#write-a-resolver-to-match-custom-elements-and-views). The constructor of `CustomElementsResolver` takes a `Context`, so the C# class passes the application context to `register`:

```java showLineNumbers title="Assets/Plugins/Android/CustomElements.java"
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](ios-flow-custom-elements#test-on-a-device) and [Android](android-flow-custom-elements#test-on-a-device) guides, with the following changes:

- To see the warnings of the Adapty SDK in the Xcode console and in Logcat, set the [log level](sdk-installation-unity#logging) to `AdaptyLogLevel.Warn` or 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](unity-custom-element-lottie-animation): A ring that fills as a streak grows.
- [Show an interactive weight goal chart in a paywall](unity-custom-element-interactive-chart): A weight goal chart, in the paywall flow that follows the onboarding flow.
- [Show nearby places on a map in a flow](unity-custom-element-map): A map of fitness centers near each user.

The [Unity SDK repository](https://github.com/adaptyteam/AdaptySDK-Unity) 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`](https://github.com/adaptyteam/AdaptySDK-Unity/blob/main/Assets/Scripts/DemoCustomElements.cs): The C# class that calls the native code at launch.
- [`Assets/Plugins/iOS/AdaptyDemoCustomElements.swift`](https://github.com/adaptyteam/AdaptySDK-Unity/blob/main/Assets/Plugins/iOS/AdaptyDemoCustomElements.swift): The resolver and the SwiftUI view for the `review_card` Type.
- [`Assets/Plugins/Android/DemoCustomElements.java`](https://github.com/adaptyteam/AdaptySDK-Unity/blob/main/Assets/Plugins/Android/DemoCustomElements.java): The resolver and the Android `View` for the `review_card` Type.