setsuodu的博客

pied piper

unity3d使用Coroutines传递返回值(.NET 3.5)

一些必须通过异步处理,又需要赋值的情况下,可以用使用action委托来操作。一个简单的例子:

public Texture2D texture2D;

void Start()
{
    StartCoroutine(LoadBundleTexAsync((value) => texture2D = value, filepath, filename));
}

IEnumerator LoadTextureAsync(Action<Texture2D> result, string filepath, string filename)
{
    string bundleName = string.Format("{0}/{1}.unity3d", filepath, filename);
    string path = Path.Combine(dir, bundleName);

    byte[] bytes = File.ReadAllBytes(path);
    AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(bytes);
    yield return request;
    AssetBundle bundle = request.assetBundle;

    Texture2D texture2D = bundle.LoadAsset<Texture2D>(bundle.AllAssetNames()[0]);
    result(texture2D);

    //延迟到下一帧执行卸载
    yield return new WaitForEndOfFrame();
    bundle.Unload(false);
    bundle = null;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mseol/article/details/80319312
个人分类: unity3d
上一篇Unity打包Xcode,自动配置
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭