Show user progress with a Lottie animation

Follow this recipe to display the user’s progress as a Lottie animation — for example, a ring that fills up to indicate their daily streak. Your app renders the animation as a native Lottie view in a custom element.

This recipe reuses the flow setup and the native views of the iOS recipe and the Android recipe. Each step of this recipe says what to copy from them, and what to do differently in a Unity project.

Before you start

Prepare a Lottie animation in .json format, or download the sample streak_ring.json. The app plays the animation from the start and stops it in proportion to the progress, such as halfway through for 50%. To show the exact progress, the animation needs:

  • No progress on its first frame, and full progress on its last frame.
  • A steady speed, with no easing. If the animation starts slowly and then speeds up, it shows less than half of the progress halfway through.

1. Add the custom element to the screen

Add the custom element to the flow screen as in step 1 of the iOS recipe.

2. Add Lottie to your project

iOS

External Dependency Manager adds Swift packages to the Xcode project that Unity generates, as it does for the Adapty SDK. To add Lottie to your iOS build, declare its Swift package in a file with a name that ends in Dependencies.xml, in an Editor folder:

<dependencies>
  <remoteSwiftPackage url="https://github.com/airbnb/lottie-spm.git" version="4.6.1">
    <swiftPackage name="Lottie"/>
  </remoteSwiftPackage>
</dependencies>

Android

To add Lottie to your Android build, create the Assets/Plugins/Android/CustomElementsDependencies.androidlib folder. Unity adds this folder to the Gradle build as an Android library. Put the following files in it:

  • AndroidManifest.xml: One line, <manifest xmlns:android="http://schemas.android.com/apk/res/android" />.
  • project.properties: One line, android.library=true.
  • build.gradle:
apply plugin: 'com.android.library'

def unityLibraryAndroid = project(':unityLibrary').android

dependencies {
    // api: the Java files in Assets/Plugins/Android can use Lottie
    api 'com.airbnb.android:lottie:6.7.1'
}

android {
    namespace "com.example.app.dependencies"

    compileSdk unityLibraryAndroid.compileSdk
    buildToolsVersion = unityLibraryAndroid.buildToolsVersion

    defaultConfig {
        minSdk unityLibraryAndroid.defaultConfig.minSdk
        targetSdk unityLibraryAndroid.defaultConfig.targetSdk
    }
}

3. Add the animation file

Put the animation file in the Assets/StreamingAssets folder of your Unity project. Unity copies the files of this folder to the Data/Raw folder of the iOS app bundle, and to the assets folder of the Android app.

The file name without .json must match the ID of the custom element, such as streak_ring.json for streak_ring.

4. Add the native code

iOS

In a Swift file in Assets/Plugins/iOS, add the streak model, the animation view, and the resolver from steps 3 and 4 of the iOS recipe. In the resolver, load the animation file from the Data/Raw folder:

case "lottie_animation" where id == "streak_ring":
    // the files of Assets/StreamingAssets are in the Data/Raw folder of the app bundle
    guard let animation = LottieAnimation.named(id, subdirectory: "Data/Raw") else {
        return nil // no animation file: show the fallback
    }

Instead of the code that sets the resolver in step 4 of the iOS recipe, add two functions that your C# code calls. RegisterCustomElements sets the resolver at launch, and SetStreak updates the streak model:

import AdaptyUI

// the streak that your C# code sends
@MainActor let streakStore = StreakStore()

// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
    Task { @MainActor in
        AdaptyUI.setCustomElementsResolver(CustomElementsResolver(streakStore: streakStore))
    }
}

// Called from C# at launch and each time the streak changes.
@_cdecl("SetStreak")
public func SetStreak(_ days: Int32) {
    Task { @MainActor in
        streakStore.currentStreak = Int(days)
    }
}

Android

Copy the StreakStore and StreakRingView classes from the Java tab of step 3 of the Android recipe. Copy the CustomElementsResolver class from the Java tab of step 4 of the same recipe. Save each class in its own file in Assets/Plugins/Android, such as StreakRingView.java. Start each file with package com.example.app; and the imports that its class uses.

In CustomElements.java, pass the resolver to the SDK, and add a setStreak method for your C# code:

package com.example.app;

import android.content.Context;
import com.adapty.ui.AdaptyUI;

public final class CustomElements {
    // the streak that your C# code sends
    private static final StreakStore streakStore = new StreakStore();

    // Called once from C# at launch, before any flow is created.
    public static void register(Context context) {
        AdaptyUI.setCustomElementsResolver(new CustomElementsResolver(context, streakStore));
    }

    // Called from C# at launch and each time the streak changes.
    public static void setStreak(int days) {
        // C# calls from Unity's main thread, not from the Android main thread
        streakStore.currentStreak.postValue(days);
    }
}

5. Send the streak from your C# code

In the C# CustomElements class, add a SendStreak method that passes the streak to your native code. The highlighted lines show what to add to the class from the Unity guide:

using UnityEngine;

public static class CustomElements
{
#if UNITY_IOS && !UNITY_EDITOR
    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void RegisterCustomElements();

    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void SetStreak(int days);
#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
    }

    // call at app launch, and each time the streak changes
    public static void SendStreak(int days)
    {
#if UNITY_IOS && !UNITY_EDITOR
        SetStreak(days);
#elif UNITY_ANDROID && !UNITY_EDITOR
        using (var customElements = new AndroidJavaClass("com.example.app.CustomElements"))
        {
            customElements.CallStatic("setStreak", days);
        }
#endif
    }
}

Call CustomElements.SendStreak at app launch, and each time the streak changes.

6. Test on a device

Test the flow as in step 5 of the iOS and Android recipes. To set the streak to 0, 4, and 7 days, call CustomElements.SendStreak.

To test the fallback, make the resolver return no view for the streak_ring ID, and open the screen again. On Android, keep the animation file in the build: the resolver of this recipe doesn’t verify that the file exists.

To find out why the fallback appears instead of the animation, follow the instructions in the Test on a device section of the Unity guide.