Skip to content

Commit 94e2207

Browse files
committed
```优化AvatarUI中的异步加载
将AvatarUI.cs中的异步加载方法更新为使用async/await,以提高代码的可读性和维护性。现在,`UpdataUI`和`OnUpdataAvatarUIEvent`方法都使用`LoadAssetAsync`进行异步加载,并通过`await`关键字等待加载完成,从而简化了回调结构。```
1 parent fb616ee commit 94e2207

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

CrazyCarClient/Assets/Scripts/UI/Avatar/AvatarUI.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ private async void OnEnable() {
3030
}
3131
}
3232

33-
private void UpdataUI() {
34-
this.GetSystem<IAddressableSystem>().LoadAsset<Sprite>(Util.GetAvatarUrl(curAid), (obj) => {
35-
if (obj.Status == AsyncOperationStatus.Succeeded) {
36-
curAvatar.sprite = Instantiate(obj.Result, transform, false);
37-
}
38-
});
33+
private async void UpdataUI() {
34+
var obj = await this.GetSystem<IAddressableSystem>().LoadAssetAsync<Sprite>(Util.GetAvatarUrl(curAid));
35+
if (obj.Status == AsyncOperationStatus.Succeeded) {
36+
curAvatar.sprite = Instantiate(obj.Result, transform, false);
37+
}
3938
curAvatarName.text = avatarModel.AvatarDic[curAid].name;
4039
Util.DeleteChildren(avatarItemParent);
4140
foreach (var kvp in avatarModel.AvatarDic) {
@@ -61,19 +60,18 @@ private void Start() {
6160
this.RegisterEvent<UpdataAvatarUIEvent>(OnUpdataAvatarUIEvent).UnRegisterWhenGameObjectDestroyed(gameObject);
6261
}
6362

64-
private void OnUpdataAvatarUIEvent(UpdataAvatarUIEvent e) {
63+
private async void OnUpdataAvatarUIEvent(UpdataAvatarUIEvent e) {
6564
if (e.aid == this.GetModel<IUserModel>().Aid) {
6665
applyBtn.interactable = false;
6766
} else {
6867
applyBtn.interactable = true;
6968
}
70-
this.GetSystem<IAddressableSystem>().LoadAsset<Sprite>(Util.GetAvatarUrl(e.aid), (obj) => {
71-
if (obj.Status == AsyncOperationStatus.Succeeded) {
72-
curAvatar.sprite = Instantiate(obj.Result, transform, false);
73-
}
74-
});
7569
curAvatarName.text = avatarModel.AvatarDic[e.aid].name;
7670
curAid = e.aid;
71+
var obj = await this.GetSystem<IAddressableSystem>().LoadAssetAsync<Sprite>(Util.GetAvatarUrl(e.aid));
72+
if (obj.Status == AsyncOperationStatus.Succeeded) {
73+
curAvatar.sprite = Instantiate(obj.Result, transform, false);
74+
}
7775
}
7876

7977
public IArchitecture GetArchitecture() {

0 commit comments

Comments
 (0)