Files
MinFt/Client/Assets/Scripts/Others/RewardFly/RewardFlyBatchController.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

122 lines
4.4 KiB
C#

using asap.core;
using asap.core.common;
using UnityEngine;
using UniRx;
using System.Threading.Tasks;
using System.Collections.Generic;
using Game;
using UnityEngine.UI;
using GameCore;
public class RewardFlyBatchController : MonoBehaviour
{
[Header("Configuration")]
[SerializeField] private BatchedRewardFly rewardFlyPrefab;
[SerializeField] private int initialPoolSize = 5;
[SerializeField] private int maxPoolSize = 20;
[SerializeField] private int maxIconsPerRequest = 8;
[Header("Animation Parameters")]
[SerializeField] public RewardFlyAnimationParams[] animationParamList;
// public IEventAggregator EventBus { get; private set; }
private IObjectPoolService _objectPoolService;
private IObjectPool _rewardFlyPool;
private void Awake()
{
_objectPoolService = GContext.container.Resolve<IObjectPoolService>();
_rewardFlyPool = _objectPoolService.CreatePool(rewardFlyPrefab, initialPoolSize, maxPoolSize);
GContext.OnEvent<BatchedRewardFlyRequest>().Subscribe(OnRewardFlyRequest).AddTo(this);
}
private void OnDestroy()
{
_objectPoolService?.DestroyPool(typeof(BatchedRewardFly));
_rewardFlyPool = null;
}
public async void OnRewardFlyRequest(BatchedRewardFlyRequest request)
{
try
{
await OnRewardFlyRequestAsync(request);
}
catch (System.Exception ex)
{
Debug.LogError(ex);
}
}
public async Task OnRewardFlyRequestAsync(BatchedRewardFlyRequest request)
{
var rewardFlyList = new List<BatchedRewardFly>();
try
{
int iconCount = 1;
if (request.Quantity.HasValue)
iconCount = Mathf.Clamp(request.Quantity.Value, 1, maxIconsPerRequest);
var taskList = new List<Task>();
var param = animationParamList[request.AnimationParamIndex];
// if (param.IsPlayOpen)
// GContext.Publish(new EventUISound("audio_ui_rewardfly_single_fly"));
string audioShow = param.GetAudioUrl(0, iconCount > 1);
if (audioShow != null && audioShow != "")
GContext.Publish(new EventRewardSound(audioShow));
BatchedRewardFly rewardFly;
//Show
if (iconCount <= 1)
{
rewardFly = _rewardFlyPool.SpawnObject() as BatchedRewardFly;
if (rewardFly == null)
{
Debug.LogError("[RewardFlyBatchController] Failed to spawn RewardFly from pool.");
return;
}
rewardFlyList.Add(rewardFly);
rewardFly.gameObject.SetActive(true);
await rewardFly.SingleFlyAsync(request, param);
}
else
{
rewardFly = _rewardFlyPool.SpawnObject() as BatchedRewardFly;
if (rewardFly == null)
{
Debug.LogError("[RewardFlyBatchController] Failed to spawn RewardFly from pool.");
return;
}
rewardFlyList.Add(rewardFly);
rewardFly.gameObject.SetActive(true);
await rewardFly.BatchShowAsync(request, param);
for (int i = 0; i < iconCount; i++)
{
rewardFly = _rewardFlyPool.SpawnObject() as BatchedRewardFly;
if (rewardFly == null)
{
Debug.LogError("[RewardFlyBatchController] Failed to spawn RewardFly from pool.");
continue;
}
rewardFlyList.Add(rewardFly);
rewardFly.gameObject.SetActive(true);
taskList.Add(rewardFly.BatchFlyAsync(request, param));
if (iconCount > 1)
{
await Task.Delay(System.TimeSpan.FromSeconds(param.SpawnInterval));
}
}
await Task.WhenAll(taskList.ToArray());
}
rewardFlyList.ForEach(rf => rf.ReturnPool());
rewardFlyList.Clear();
}
catch (System.Exception e)
{
Debug.Log($"[RewardFlyBatchController] {e.Message}\n{e.StackTrace}");
rewardFlyList.ForEach(rf => Destroy(rf.gameObject));
rewardFlyList.Clear();
}
// Debug.Log("[RewardFlyBatchController] Done.");
}
}