---
title: "Hiển thị tiến trình người dùng bằng hoạt ảnh Lottie"
description: "Hiển thị tiến trình của từng người dùng, chẳng hạn như chuỗi ngày liên tiếp, dưới dạng hoạt ảnh Lottie trên màn hình flow."
---

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

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

Làm theo hướng dẫn này để hiển thị tiến trình của người dùng dưới dạng animation Lottie — ví dụ: một vòng tròn tô dần để hiển thị chuỗi ngày liên tiếp. Ứng dụng của bạn sẽ render animation dưới dạng native Lottie view trong một [custom element](flow-custom-elements).

<DocVideo id="custom-element-lottie-demo.mp4" width="300px" alt="Flow hoàn chỉnh: người dùng nhấn See my streak, và ở màn hình tiếp theo, vòng tròn tô dần lên 4 trong 7 ngày." />

Hướng dẫn này tái sử dụng cấu hình flow và các native view từ [hướng dẫn iOS](custom-element-lottie-animation) và [hướng dẫn Android](android-custom-element-lottie-animation). Mỗi bước trong hướng dẫn này sẽ chỉ rõ những gì cần sao chép từ chúng và những gì cần thực hiện khác đi trong dự án Unity.

## Trước khi bắt đầu \{#before-you-start\}

- Cập nhật ứng dụng lên Adapty Unity SDK 4.4 hoặc cao hơn. Các phiên bản SDK cũ hơn sẽ hiển thị [nội dung dự phòng](flow-custom-elements#set-fallback-content) cho mọi phần tử tùy chỉnh.
- Thiết lập ứng dụng để [tải](unity-get-pb-paywalls) và [hiển thị](unity-present-paywalls) flow.
- Thiết lập ứng dụng cho các phần tử tùy chỉnh, như hướng dẫn trong [Hiển thị phần tử tùy chỉnh với Unity SDK](unity-flow-custom-elements). Hướng dẫn này bổ sung code vào các lớp C# và Java `CustomElements` từ tài liệu đó.

Chuẩn bị một animation Lottie ở định dạng `.json`, hoặc tải xuống file mẫu [`streak_ring.json`](https://raw.githubusercontent.com/adaptyteam/adapty-docs/refs/heads/main/Downloads/streak_ring.json). Ứng dụng phát animation từ đầu và dừng lại theo tỷ lệ tiến độ, chẳng hạn dừng ở giữa nếu tiến độ là 50%. Để hiển thị đúng tiến độ, animation cần đáp ứng:

- Không có tiến độ ở frame đầu tiên, và tiến độ đầy đủ ở frame cuối cùng.
- Tốc độ đều, không có easing. Nếu animation bắt đầu chậm rồi tăng tốc, nó sẽ hiển thị ít hơn một nửa tiến độ khi đã đi được nửa chừng.

## 1. Thêm phần tử tùy chỉnh vào màn hình \{#1-add-the-custom-element-to-the-screen\}

Thêm phần tử tùy chỉnh vào màn hình flow như trong bước 1 của [hướng dẫn iOS](custom-element-lottie-animation#1-add-the-custom-element-to-the-screen).

## 2. Thêm Lottie vào dự án của bạn \{#2-add-lottie-to-your-project\}

### iOS

External Dependency Manager thêm các Swift package vào dự án Xcode mà Unity tạo ra, tương tự như cách nó thực hiện với Adapty SDK. Để thêm Lottie vào bản build iOS của bạn, hãy khai báo Swift package của nó trong một file có tên kết thúc bằng `Dependencies.xml`, trong thư mục `Editor`:

```xml showLineNumbers title="Assets/Editor/CustomElementsDependencies.xml"
<dependencies>
  <remoteSwiftPackage url="https://github.com/airbnb/lottie-spm.git" version="4.6.1">
    <swiftPackage name="Lottie"/>
  </remoteSwiftPackage>
</dependencies>
```

### Android

Để thêm Lottie vào bản build Android, hãy tạo thư mục `Assets/Plugins/Android/CustomElementsDependencies.androidlib`. Unity sẽ thêm thư mục này vào Gradle build như một thư viện Android. Đặt các file sau vào đó:

- **`AndroidManifest.xml`**: Một dòng, `<manifest xmlns:android="http://schemas.android.com/apk/res/android" />`.
- **`project.properties`**: Một dòng, `android.library=true`.
- **`build.gradle`**:

```groovy showLineNumbers title="Assets/Plugins/Android/CustomElementsDependencies.androidlib/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. Thêm file animation \{#add-the-animation-file\}

Đặt file animation vào thư mục `Assets/StreamingAssets` trong project Unity của bạn. Unity sẽ sao chép các file trong thư mục này vào thư mục `Data/Raw` của app bundle iOS, và vào thư mục `assets` của app Android.

Tên file (không bao gồm `.json`) phải khớp với ID của custom element, ví dụ `streak_ring.json` tương ứng với `streak_ring`.

## 4. Thêm mã native \{#4-add-the-native-code\}

### iOS

Trong một file Swift trong `Assets/Plugins/iOS`, thêm model streak, animation view, và resolver từ bước 3 và 4 của [hướng dẫn iOS](custom-element-lottie-animation#3-create-the-animation-view). Trong resolver, tải file animation từ thư mục `Data/Raw`:

```swift showLineNumbers title="Swift" {3}
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
    }
```

Thay vì đoạn code thiết lập resolver ở bước 4 trong hướng dẫn iOS, hãy thêm hai hàm mà code C# của bạn gọi. `RegisterCustomElements` thiết lập resolver khi khởi chạy, và `SetStreak` cập nhật model streak:

```swift showLineNumbers title="Assets/Plugins/iOS/CustomElements.swift"

// 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

Sao chép các class `StreakStore` và `StreakRingView` từ tab **Java** ở bước 3 của [hướng dẫn Android](android-custom-element-lottie-animation#3-create-the-animation-view). Sao chép class `CustomElementsResolver` từ tab **Java** ở [bước 4](android-custom-element-lottie-animation#4-match-the-animation-to-the-custom-element) của cùng hướng dẫn đó. Lưu mỗi class vào một file riêng trong `Assets/Plugins/Android`, ví dụ `StreakRingView.java`. Bắt đầu mỗi file với `package com.example.app;` và các import mà class đó sử dụng.

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

```java showLineNumbers title="Assets/Plugins/Android/CustomElements.java"
package com.example.app;

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. Gửi streak từ code C# của bạn \{#5-send-the-streak-from-your-c-code\}

Trong class `CustomElements` của C#, thêm một phương thức `SendStreak` để truyền streak sang code native. Các dòng được tô sáng cho thấy những gì cần thêm vào class từ [hướng dẫn Unity](unity-flow-custom-elements#pass-the-resolver-to-the-sdk):

```csharp showLineNumbers title="Assets/Scripts/CustomElements.cs" {9-10,29-40}
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
    }
}
```

Gọi `CustomElements.SendStreak` khi khởi động ứng dụng và mỗi khi streak thay đổi.

## 6. Kiểm tra trên thiết bị \{#6-test-on-a-device\}

Kiểm tra flow như ở bước 5 trong hướng dẫn [iOS](custom-element-lottie-animation#5-test-on-a-device) và [Android](android-custom-element-lottie-animation#5-test-on-a-device). Để đặt streak về 0, 4 và 7 ngày, hãy gọi `CustomElements.SendStreak`.

Để kiểm tra paywall dự phòng, hãy khiến resolver không trả về view nào cho ID `streak_ring`, rồi mở lại màn hình. Trên Android, hãy giữ file animation trong bản build: resolver trong hướng dẫn này không kiểm tra xem file có tồn tại hay không.

Để tìm hiểu lý do tại sao paywall dự phòng xuất hiện thay vì animation, hãy làm theo hướng dẫn trong phần [Kiểm tra trên thiết bị](unity-flow-custom-elements#test-on-a-device) của hướng dẫn Unity.