mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
修正同时对一个图片进行异步SetSprite的处理
修正同时对一个图片进行异步SetSprite的处理
This commit is contained in:
@@ -12,7 +12,7 @@ public static class SetSpriteExtensions
|
||||
/// <param name="setNativeSize">是否使用原始分辨率。</param>
|
||||
public static void SetSprite(this Image image, string location, bool setNativeSize = false)
|
||||
{
|
||||
GameModule.ResourceExt.SetAssetByResources<Sprite>(SetSpriteObject.Create(image, location, setNativeSize));
|
||||
GameModule.ResourceExt.SetAssetByResources<Sprite>(SetSpriteObject.Create(image, location, setNativeSize)).Forget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -22,6 +22,6 @@ public static class SetSpriteExtensions
|
||||
/// <param name="location">资源定位地址。</param>
|
||||
public static void SetSprite(this SpriteRenderer spriteRenderer, string location)
|
||||
{
|
||||
GameModule.ResourceExt.SetAssetByResources<Sprite>(SetSpriteObject.Create(spriteRenderer, location));
|
||||
GameModule.ResourceExt.SetAssetByResources<Sprite>(SetSpriteObject.Create(spriteRenderer, location)).Forget();
|
||||
}
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public partial class ResourceExtComponent
|
||||
@@ -17,11 +19,13 @@ namespace TEngine
|
||||
|
||||
private void OnLoadAssetFailure(string assetName, LoadResourceStatus status, string errormessage, object userdata)
|
||||
{
|
||||
_assetLoadingList.Remove(assetName);
|
||||
Log.Error("Can not load asset from '{1}' with error message '{2}'.", assetName, errormessage);
|
||||
}
|
||||
|
||||
private void OnLoadAssetSuccess(string assetName, object asset, float duration, object userdata)
|
||||
{
|
||||
_assetLoadingList.Remove(assetName);
|
||||
ISetAssetObject setAssetObject = (ISetAssetObject)userdata;
|
||||
UnityEngine.Object assetObject = asset as UnityEngine.Object;
|
||||
if (assetObject != null)
|
||||
@@ -39,8 +43,10 @@ namespace TEngine
|
||||
/// 通过资源系统设置资源。
|
||||
/// </summary>
|
||||
/// <param name="setAssetObject">需要设置的对象。</param>
|
||||
public void SetAssetByResources<T>(ISetAssetObject setAssetObject) where T : UnityEngine.Object
|
||||
public async UniTaskVoid SetAssetByResources<T>(ISetAssetObject setAssetObject) where T : UnityEngine.Object
|
||||
{
|
||||
await TryWaitingLoading(setAssetObject.Location);
|
||||
|
||||
if (m_AssetItemPool.CanSpawn(setAssetObject.Location))
|
||||
{
|
||||
var assetObject = (T)m_AssetItemPool.Spawn(setAssetObject.Location).Target;
|
||||
@@ -48,6 +54,7 @@ namespace TEngine
|
||||
}
|
||||
else
|
||||
{
|
||||
_assetLoadingList.Add(setAssetObject.Location);
|
||||
m_ResourceModule.LoadAssetAsync(setAssetObject.Location, typeof(T), m_LoadAssetCallbacks, setAssetObject);
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,9 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
#if ODIN_INSPECTOR
|
||||
using Sirenix.OdinInspector;
|
||||
#endif
|
||||
@@ -13,6 +16,13 @@ namespace TEngine
|
||||
[DisallowMultipleComponent]
|
||||
public partial class ResourceExtComponent : Module
|
||||
{
|
||||
private readonly TimeoutController _timeoutController = new TimeoutController();
|
||||
|
||||
/// <summary>
|
||||
/// 正在加载的资源列表。
|
||||
/// </summary>
|
||||
private readonly HashSet<string> _assetLoadingList = new HashSet<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 检查是否可以释放间隔
|
||||
/// </summary>
|
||||
@@ -104,5 +114,31 @@ namespace TEngine
|
||||
m_LoadAssetObjectsLinkedList.AddLast(new LoadAssetObject(setAssetObject, assetObject));
|
||||
setAssetObject.SetAsset(assetObject);
|
||||
}
|
||||
|
||||
private async UniTask TryWaitingLoading(string assetObjectKey)
|
||||
{
|
||||
if (_assetLoadingList.Contains(assetObjectKey))
|
||||
{
|
||||
try
|
||||
{
|
||||
await UniTask.WaitUntil(
|
||||
() => !_assetLoadingList.Contains(assetObjectKey))
|
||||
#if UNITY_EDITOR
|
||||
.AttachExternalCancellation(_timeoutController.Timeout(TimeSpan.FromSeconds(60)));
|
||||
_timeoutController.Reset();
|
||||
#else
|
||||
;
|
||||
#endif
|
||||
|
||||
}
|
||||
catch (OperationCanceledException ex)
|
||||
{
|
||||
if (_timeoutController.IsTimeout())
|
||||
{
|
||||
Log.Error($"LoadAssetAsync Waiting {assetObjectKey} timeout. reason:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user