Files
MinFt/Client/LocalPackages/asap.playfab.async/Runtime/PlayFabClientInstanceAsyncExtensions.cs
Liubing\LB 3d8d4d18f3 first commit
先修复一下,错误的场景

删除不必要的钓场资产

修复into场景

update:更新meta文件,修复报错

update:修复资源

修复第一章节建造

修复建造第三章

update:删除多余内容

update:更新README.md

update:还原图标

update:更新README

update:更新配置
2026-04-28 02:17:22 +08:00

83 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#if !DISABLE_PLAYFABCLIENT_API
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
namespace asap.playfab.async
{
/// <summary>
/// 为 PlayFabClientInstanceAPI 提供异步 Task 封装及通用重试能力的扩展方法。
/// </summary>
public static class PlayFabClientInstanceAsyncExtensions
{
/// <summary>
/// 将 PlayFabClientInstanceAPI 的回调式方法转换为 Task可与 ExecuteWithRetryAsync 配合使用。
/// </summary>
/// <example>
/// var result = await api.ExecuteWithRetryAsync(
/// () => api.ToAsync(request, api.LoginWithCustomID)
/// );
/// </example>
public static Task<TResult> ToAsync<TRequest, TResult>(
this PlayFabClientInstanceAPI api,
TRequest request,
Action<TRequest, Action<TResult>, Action<PlayFabError>, object, Dictionary<string, string>> apiMethod)
where TResult : class
{
var task = new TaskCompletionSource<TResult>();
apiMethod(
request,
result => task.SetResult(result),
error =>
{
Debug.LogError(error.GenerateErrorReport());
task.SetResult(null);
},
null,
null);
return task.Task;
}
/// <summary>
/// 通用重试封装,将任意返回 Task&lt;T&gt; 的调用包裹在重试逻辑中。
/// 当调用返回 null即发生错误时触发重试采用指数退避策略。
/// </summary>
/// <typeparam name="T">API 返回结果类型</typeparam>
/// <param name="api">PlayFabClientInstanceAPI 实例</param>
/// <param name="apiCall">要执行的调用,形如 () => api.ToAsync(request, api.XxxMethod)</param>
/// <param name="maxRetries">最大重试次数,默认 3 次</param>
/// <param name="retryDelaySeconds">首次重试等待秒数,每次重试翻倍(指数退避),默认 1 秒</param>
/// <returns>成功时返回结果对象,所有重试均失败后返回 null</returns>
public static async Task<T> ExecuteWithRetryAsync<T>(
this PlayFabClientInstanceAPI api,
Func<Task<T>> apiCall,
int maxRetries = 3,
float retryDelaySeconds = 1f) where T : class
{
var attempts = 0;
while (true)
{
var result = await apiCall();
if (result != null)
return result;
if (attempts >= maxRetries)
{
Debug.LogWarning($"[PlayFabRetry] 已达到最大重试次数 {maxRetries},放弃重试。");
return null;
}
var delaySeconds = retryDelaySeconds * Mathf.Pow(2f, attempts);
Debug.LogWarning($"[PlayFabRetry] 第 {attempts + 1} 次重试,等待 {delaySeconds:F1} 秒后重试...");
await Task.Delay(TimeSpan.FromSeconds(delaySeconds));
attempts++;
}
}
}
}
#endif