feifeid47/Unity-Async-UIFrame

建议将 OnAssetRequest 签名改一下,返回Task

Closed this issue · 2 comments

    public static Func<Type, Task<GameObject>> OnAssetRequest;

    private static async Task<GameObject> RequestInstance(Type type)
        {
            ...
            var refInstance = await OnAssetRequest?.Invoke(type);//此处可能需要进一步判空
            ...
            return instance;
        }

使用:

using Feif.UIFramework;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    Dictionary<Type, GameObject> handles=new Dictionary<Type, GameObject>();
    void Start()
    {
        UIFrame.OnAssetRequest += OnAssetRequest;
    }

    private async Task<GameObject> OnAssetRequest(Type type)
    {
        var handle = (GameObject)null;
        if (!handles.ContainsKey(type))
        {
            handle=handles[type] ;
        }
        await Task.Yield(); // 这个演示了调用异步,与签名的 async 呼应,此处可改为资源管理的 异步api
        return handle;
    }
}

Task 默认情况下在 webgl不支持,可以取巧支持“异步”
VolodymyrBS/WebGLThreadingPatcher#3

谢谢反馈,已采纳,后续更新会加上。保证Task的运行在主线程即可,可配合UniTask或其他第三方库使用

用 async await 就是为了避免回调地狱,大橘为重加油干!