调整 SpriteCache 的读取资源方式
将原来 UGF 事件回调风格的调用改成使用 UniTask 的异步操作
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@@ -10,7 +11,7 @@ namespace SepCore.UI
|
||||
|
||||
private RoleItemContext _context = null;
|
||||
|
||||
public void OnInit(RoleItemContext context)
|
||||
public async UniTaskVoid OnInit(RoleItemContext context)
|
||||
{
|
||||
_context = context;
|
||||
if (_context == null || string.IsNullOrEmpty(_context.IconName))
|
||||
@@ -18,7 +19,7 @@ namespace SepCore.UI
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.SpriteCache.GetSprite(_context.IconName, sprite => _roleImage.sprite = sprite);
|
||||
_roleImage.sprite = await GameEntry.SpriteCache.GetSprite(_context.IconName);
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.CustomUtility;
|
||||
using SepCore.DataTable;
|
||||
using SepCore.Definition;
|
||||
@@ -24,31 +25,37 @@ namespace SepCore.UI
|
||||
ItemType = ItemType.None;
|
||||
}
|
||||
|
||||
public GoodsItemContext(GoodsItemRawData rawData)
|
||||
public static async UniTask<GoodsItemContext> CreateAsync(GoodsItemRawData rawData)
|
||||
{
|
||||
Price = rawData.Price;
|
||||
Rarity = rawData.Rarity;
|
||||
ItemType = rawData.ItemType;
|
||||
if (ItemType == ItemType.None)
|
||||
var context = new GoodsItemContext
|
||||
{
|
||||
Description = string.Empty;
|
||||
Icon = null;
|
||||
Title = string.Empty;
|
||||
Price = rawData.Price,
|
||||
Rarity = rawData.Rarity,
|
||||
ItemType = rawData.ItemType
|
||||
};
|
||||
|
||||
if (context.ItemType == ItemType.None)
|
||||
{
|
||||
context.Description = string.Empty;
|
||||
context.Icon = null;
|
||||
context.Title = string.Empty;
|
||||
}
|
||||
else if (ItemType == ItemType.Weapon)
|
||||
else if (context.ItemType == ItemType.Weapon)
|
||||
{
|
||||
var weapon = (DRWeapon)rawData.DataRow;
|
||||
Title = weapon.Title;
|
||||
Description = ItemDescUtility.CreateWeaponDescription(weapon);
|
||||
GameEntry.SpriteCache.GetSprite(weapon.IconAssetName, sprite => Icon = sprite);
|
||||
context.Title = weapon.Title;
|
||||
context.Description = ItemDescUtility.CreateWeaponDescription(weapon);
|
||||
context.Icon = await GameEntry.SpriteCache.GetSprite(weapon.IconAssetName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var prop = (DRProp)rawData.DataRow;
|
||||
Title = prop.Title;
|
||||
Description = ItemDescUtility.CreatePropDescription(prop.Modifiers);
|
||||
GameEntry.SpriteCache.GetSprite(prop.IconAssetName, sprite => Icon = sprite);
|
||||
context.Title = prop.Title;
|
||||
context.Description = ItemDescUtility.CreatePropDescription(prop.Modifiers);
|
||||
context.Icon = await GameEntry.SpriteCache.GetSprite(prop.IconAssetName);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
@@ -36,7 +37,7 @@ namespace SepCore.UI
|
||||
|
||||
private Vector3 _targetPos;
|
||||
|
||||
public void RefreshUI(DisplayItemInfoContext context)
|
||||
public async UniTaskVoid RefreshUI(DisplayItemInfoContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
@@ -64,20 +65,15 @@ namespace SepCore.UI
|
||||
_recycleButton.gameObject.SetActive(_context.ItemType == ItemType.Weapon);
|
||||
}
|
||||
|
||||
if (_iconArea != null)
|
||||
if (_iconArea != null && !string.IsNullOrEmpty(_context.IconAssetName))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_context.IconAssetName))
|
||||
string iconAssetName = _context.IconAssetName;
|
||||
var sprite = await GameEntry.SpriteCache.GetSprite(iconAssetName);
|
||||
if (_context != null && _context.IconAssetName == iconAssetName)
|
||||
{
|
||||
string iconAssetName = _context.IconAssetName;
|
||||
GameEntry.SpriteCache.GetSprite(iconAssetName, sprite =>
|
||||
{
|
||||
if (_context != null && _context.IconAssetName == iconAssetName)
|
||||
{
|
||||
_iconArea.OnInit(sprite, context.Rarity);
|
||||
ResizeToFitContent();
|
||||
ApplyClampedTargetPos();
|
||||
}
|
||||
});
|
||||
_iconArea.OnInit(sprite, context.Rarity);
|
||||
ResizeToFitContent();
|
||||
ApplyClampedTargetPos();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace SepCore.UI
|
||||
|
||||
#region BuildContext
|
||||
|
||||
private ShopContext BuildContext(ShopRawData rawData)
|
||||
private async UniTask<ShopContext> BuildContext(ShopRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
@@ -55,7 +55,7 @@ namespace SepCore.UI
|
||||
List<GoodsItemContext> goodsItems = new List<GoodsItemContext>();
|
||||
foreach (var item in rawData.GoodsItems)
|
||||
{
|
||||
goodsItems.Add(new GoodsItemContext(item));
|
||||
goodsItems.Add(await GoodsItemContext.CreateAsync(item));
|
||||
}
|
||||
|
||||
return new ShopContext
|
||||
@@ -214,7 +214,7 @@ namespace SepCore.UI
|
||||
|
||||
public async UniTask<int?> OpenUIAsync(ShopRawData rawData, float timeout = 30f)
|
||||
{
|
||||
ShopContext context = BuildContext(rawData);
|
||||
ShopContext context = await BuildContext(rawData);
|
||||
return await OpenFormAsync(context, timeout);
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ namespace SepCore.UI
|
||||
|
||||
#region Service
|
||||
|
||||
private void RefreshGoodsItems(ShopRefreshResult result)
|
||||
private async UniTask RefreshGoodsItems(ShopRefreshResult result)
|
||||
{
|
||||
if (result == null)
|
||||
{
|
||||
@@ -272,8 +272,8 @@ namespace SepCore.UI
|
||||
|
||||
for (int i = 0; i < result.GoodsItems.Count; i++)
|
||||
{
|
||||
if (i < Context.GoodsItems.Count) Context.GoodsItems[i] = new GoodsItemContext(result.GoodsItems[i]);
|
||||
else Context.GoodsItems.Add(new GoodsItemContext(result.GoodsItems[i]));
|
||||
if (i < Context.GoodsItems.Count) Context.GoodsItems[i] = await GoodsItemContext.CreateAsync(result.GoodsItems[i]);
|
||||
else Context.GoodsItems.Add(await GoodsItemContext.CreateAsync(result.GoodsItems[i]));
|
||||
}
|
||||
|
||||
if (Context.GoodsItems.Count != result.GoodsItems.Count)
|
||||
@@ -425,7 +425,7 @@ namespace SepCore.UI
|
||||
|
||||
#region Event Handlers
|
||||
|
||||
private void Refresh(object sender, GameEventArgs e)
|
||||
private async void Refresh(object sender, GameEventArgs e)
|
||||
{
|
||||
if (sender is not ShopForm)
|
||||
{
|
||||
@@ -443,7 +443,7 @@ namespace SepCore.UI
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshGoodsItems(result);
|
||||
await RefreshGoodsItems(result);
|
||||
}
|
||||
|
||||
private void ShopPurchase(object sender, GameEventArgs e)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.Event;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@@ -19,7 +20,7 @@ namespace SepCore.UI
|
||||
_index = index;
|
||||
}
|
||||
|
||||
public void OnInit(DisplayItemContext context, int index)
|
||||
public async UniTaskVoid OnInit(DisplayItemContext context, int index)
|
||||
{
|
||||
if (context == null) return;
|
||||
|
||||
@@ -28,8 +29,8 @@ namespace SepCore.UI
|
||||
|
||||
if (_iconArea != null)
|
||||
{
|
||||
string iconName = _context.IconAssetName;
|
||||
GameEntry.SpriteCache.GetSprite(iconName, sprite => { _iconArea.OnInit(sprite, context.Rarity); });
|
||||
var sprite = await GameEntry.SpriteCache.GetSprite(_context.IconAssetName);
|
||||
_iconArea.OnInit(sprite, context.Rarity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
@@ -32,16 +33,17 @@ namespace SepCore.UI
|
||||
if (_descriptionText != null) _descriptionText.text = context.Description;
|
||||
if (_iconArea != null) _iconArea.OnInit(context.Icon, context.ItemRarity);
|
||||
|
||||
LoadIcon(_context.IconAssetName);
|
||||
LoadIcon(_context.IconAssetName).Forget();
|
||||
}
|
||||
|
||||
private void LoadIcon(string iconAssetName)
|
||||
private async UniTaskVoid LoadIcon(string iconAssetName)
|
||||
{
|
||||
if (_iconArea == null) return;
|
||||
|
||||
if (string.IsNullOrEmpty(iconAssetName)) return;
|
||||
|
||||
GameEntry.SpriteCache.GetSprite(iconAssetName, sprite => _iconArea.SetIcon(sprite));
|
||||
var sprite = await GameEntry.SpriteCache.GetSprite(iconAssetName);
|
||||
_iconArea.SetIcon(sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user