Files
back_cantanBuilding/Assets/Scripts/UI/Home/EventButtonResource.cs
2026-05-26 16:15:54 +08:00

84 lines
2.4 KiB
C#

using asap.core;
using System;
using System.Collections.Generic;
using UnityEngine;
using UniRx;
public abstract class EventButtonResource : MonoBehaviour
{
ILoadResourceService loadResourceService;
bool Checking = false;
IDisposable loadResourceIdis;
string resourceKey;
Animation anim;
public async void CheckResource(List<string> resourceName)
{
if (Checking || resourceName == null || resourceName.Count == 0)
{
return;
}
Checking = true;
loadResourceService = GContext.container.Resolve<ILoadResourceService>();
resourceKey = string.Join(",", resourceName);
Debug.Log("资源Key" + resourceKey);
bool isReady = await loadResourceService.CheckResourceLoadQueue(resourceKey, resourceName);
if (!isReady)
{
anim = GetComponent<Animation>();
gameObject.SetActive(false);
DisposeEvent();
loadResourceIdis = GContext.OnEvent<LoadEventResourceEvent>().Where(_ => _.key == resourceKey).Subscribe(LoadEventResourceEvent);
//会闪一下吗?
}
else
{
anim = null;
LoadEventResource(true);
}
}
void LoadEventResourceEvent(LoadEventResourceEvent loadEvent)
{
Debug.Log($"LoadEventResourceEvent {loadEvent.key} {loadEvent.isSucceeded} ");
DisposeEvent();
LoadEventResource(loadEvent.isSucceeded);
Checking = loadEvent.isSucceeded;
}
void DisposeEvent()
{
loadResourceIdis?.Dispose();
loadResourceIdis = null;
}
void LoadEventResource(bool succeeded)
{
if (gameObject == null)
{
Debug.Log("下载资源回调gameObject 是空");
return;
}
if (succeeded)
{
try
{
if (anim!=null)
{
anim.Play("btn_download_show");
}
OnLoadEventResource();
}
catch (System.Exception e)
{
Debug.LogError($"Load Event Resource Exception! == {gameObject.name} \n {e}");
}
}
else
{
Debug.LogError($"Load Event Resource Failed! == {gameObject.name}");
}
}
protected abstract void OnLoadEventResource();
private void OnDestroy()
{
DisposeEvent();
}
}