Hiển thị biểu đồ mục tiêu cân nặng tương tác trong paywall
Hãy làm theo hướng dẫn này để hiển thị biểu đồ cân nặng dự đoán của người dùng trong một paywall flow. Người dùng nhập cân nặng hiện tại và mục tiêu trong onboarding flow. Code C# của bạn thu thập cả hai giá trị và gửi chúng đến code native. Biểu đồ hiển thị tiến độ hàng tuần mà người dùng có thể kỳ vọng khi dùng app của bạn, từ giá trị này đến giá trị kia. App của bạn render biểu đồ dưới dạng native view trong một custom element. Trên iOS, view sử dụng Swift Charts. Trên Android, view tự vẽ biểu đồ mà không cần thư viện chart.
Công thức này tái sử dụng thiết lập flow và các native view từ công thức iOS và công thức Android. Mỗi bước trong công thức 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 trong một dự án Unity.
Trước khi bắt đầu
- Cập nhật ứng dụng của bạn lên Adapty Unity SDK 4.4 trở lên. Các phiên bản SDK cũ hơn sẽ hiển thị nội dung dự phòng cho mọi phần tử tùy chỉnh.
- Thiết lập ứng dụng để tải và hiển thị các 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. Recipe này bổ sung code vào các class C# và Java
CustomElementstừ hướng dẫn đó. - Trên iOS, tính năng kéo dọc theo biểu đồ yêu cầu iOS 17 trở lên. Trên iOS 16, người dùng vẫn thấy đường kẻ nhưng không thể kéo dọc theo nó.
Tạo hai flow riêng biệt — một cho onboarding và một cho paywall. Thêm mỗi flow vào một placement. Code trong hướng dẫn này lấy paywall flow từ placement có ID weight_paywall.
1. Thiết lập flow onboarding và flow paywall
Thiết lập cả hai flow như ở bước 1 và 2 trong hướng dẫn iOS: hai ô nhập Number trong flow onboarding, và một custom element cho biểu đồ trong flow paywall.
2. Thêm code gốc
Các hướng dẫn cho iOS và Android gắn một resolver vào paywall flow. Unity SDK không có API cho resolver của một flow. Thay vào đó, resolver toàn ứng dụng của bạn đọc weight plan từ một store, và code C# của bạn điền vào store trước khi paywall flow mở ra.
iOS
Trong một file Swift ở Assets/Plugins/iOS, thêm weight plan và chart view từ bước 4 của iOS recipe. Sau đó thêm một store cho weight plan, một resolver đọc store đó, và hai hàm mà code C# của bạn gọi. RegisterCustomElements thiết lập resolver khi khởi động, và SetWeightPlan điền dữ liệu vào store:
// the weight plan that your C# code sends before the paywall flow opens
@MainActor
final class WeightPlanStore {
var plan: WeightPlan?
}
struct CustomElementsResolver: AdaptyUICustomElementsResolver {
let weightPlanStore: WeightPlanStore
func view(
forType type: String,
id: String,
context: AdaptyUICustomElementContext
) -> AnyView? {
switch type {
case "line_chart" where id == "weight_goal":
guard #available(iOS 16.0, *), let plan = weightPlanStore.plan else {
return nil // no weight plan: show the fallback
}
return AnyView(WeightGoalChart(entries: plan.entries))
// the other cases of your resolver
default:
return nil
}
}
}
@MainActor let weightPlanStore = WeightPlanStore()
// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
Task { @MainActor in
AdaptyUI.setCustomElementsResolver(CustomElementsResolver(weightPlanStore: weightPlanStore))
}
}
// Called from C# before the paywall flow opens.
@_cdecl("SetWeightPlan")
public func SetWeightPlan(_ currentWeight: Double, _ goalWeight: Double) {
Task { @MainActor in
weightPlanStore.plan = WeightPlan(currentWeight: currentWeight, goalWeight: goalWeight)
}
}Android
Sao chép các class WeightPlan, WeightEntry, và WeightGoalChartView từ tab Java ở bước 4 của Android recipe. Lưu mỗi class vào một file riêng trong Assets/Plugins/Android, chẳng hạn như WeightPlan.java. Bắt đầu mỗi file với package com.example.app; và các import mà class đó sử dụng.
Trong CustomElements.java, thiết lập một resolver để đọc weight plan, và thêm phương thức setWeightPlan cho code C# của bạn:
package com.example.app;
public final class CustomElements {
// the weight plan that your C# code sends before the paywall flow opens
private static volatile WeightPlan weightPlan;
// Called once from C# at launch, before any flow is created.
// This resolver doesn't use the context.
public static void register(Context context) {
AdaptyUI.setCustomElementsResolver((type, id) -> {
WeightPlan plan = weightPlan;
if ("line_chart".equals(type) && "weight_goal".equals(id) && plan != null) {
return AdaptyCustomElementContent.view(
(viewContext, elementContext) -> new WeightGoalChartView(viewContext, plan.entries)
);
}
return null; // no weight plan, or another custom element: show the fallback
});
}
// Called from C# before the paywall flow opens.
public static void setWeightPlan(double currentWeight, double goalWeight) {
weightPlan = new WeightPlan(currentWeight, goalWeight);
}
}Cho đến khi code C# của bạn gửi trọng lượng hiện tại và mục tiêu, resolver sẽ không trả về view nào, và người dùng sẽ thấy paywall dự phòng.
3. Gửi các trọng số từ code C# của bạn
Trong class CustomElements của C#, thêm phương thức SendWeightPlan để truyền cả hai trọng số vào code native của bạn. 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:
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 SetWeightPlan(double currentWeight, double goalWeight);
#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 before the paywall flow opens
public static void SendWeightPlan(double currentWeight, double goalWeight)
{
#if UNITY_IOS && !UNITY_EDITOR
SetWeightPlan(currentWeight, goalWeight);
#elif UNITY_ANDROID && !UNITY_EDITOR
using (var customElements = new AndroidJavaClass("com.example.app.CustomElements"))
{
customElements.CallStatic("setWeightPlan", currentWeight, goalWeight);
}
#endif
}
}Thu thập cân nặng hiện tại và mục tiêu từ flow onboarding bằng phương thức FlowViewDidReceiveAnalyticEvent của ví dụ BMI trong Truyền dữ liệu vào flows. FlowViewDidDisappear chỉ chạy sau khi ứng dụng của bạn đóng flow onboarding, vì vậy hãy đóng nó ở hành động Close flow, như trong Đóng flows và paywalls. Khi người dùng đóng flow onboarding sau màn hình cuối cùng, hãy gửi cả hai giá trị cân nặng đến code gốc của bạn và mở flow paywall:
public void FlowViewDidDisappear(AdaptyUIFlowView view) {
// The listener receives events from every flow. Reset `bodyAnswers` and
// `reachedLastScreen` whenever a flow disappears, so that the paywall
// doesn't open again when the user closes it.
var answers = new Dictionary<string, string>(bodyAnswers);
var finished = reachedLastScreen;
bodyAnswers.Clear();
reachedLastScreen = false;
// Number inputs send their values as strings.
if (!finished) return;
if (!answers.TryGetValue("weight", out var currentText)
|| !double.TryParse(currentText, out var currentWeight)) return;
if (!answers.TryGetValue("goal_weight", out var goalText)
|| !double.TryParse(goalText, out var goalWeight)) return;
// the resolver reads the weights when the paywall flow opens
CustomElements.SendWeightPlan(currentWeight, goalWeight);
ShowWeightPaywall();
}
private void ShowWeightPaywall() {
Adapty.GetFlow("weight_paywall", (flow, flowError) => {
if (flowError != null) {
// handle the error
return;
}
AdaptyUI.CreateFlowView(flow, new AdaptyUICreateFlowViewParameters(), (view, viewError) => {
if (viewError != null) {
// handle the error
return;
}
view.Present((presentError) => {
if (presentError != null) {
// handle the error
}
});
});
});
}4. Kiểm tra trên thiết bị
Kiểm tra cả hai flow như ở bước 6 trong hướng dẫn iOS và Android. Để kiểm tra fallback, hãy để resolver không trả về view nào cho ID weight_goal, rồi mở lại flow của paywall.
Để tìm hiểu lý do tại sao fallback xuất hiện thay vì biểu đồ, hãy làm theo hướng dẫn trong phần Kiểm tra trên thiết bị của hướng dẫn Unity.