加载assetbundle协程并获取返回参数的程序举例

本文提供了一个Unity示例,演示如何通过协程(IEnumerator)加载AssetBundle资源,并获取返回的Dictionary<string, TextReader>内容。在Start()方法中调用LoadAssetBundle函数,该函数根据平台构建URL,加载AssetBundle,遍历所有资源,将TextAsset转换为StringReader并存储在字典中。最后,协程返回字典并显示其内容。" 128448129,16597425,研究生管理系统设计与实现 - JSP+SSM,"['java', '课程设计', 'mysql']
摘要由CSDN通过智能技术生成
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class sample : MonoBehaviour
{
    void Start()
    {
        var cache = LoadAssetBundle("csvconfig.assetbundle", "CsvConfig");
        while (cache.MoveNext()) { }//执行到结束
        //显示字典内容
        foreach (var dict in cache.Current as Dictionary<string, TextReader>)
        {
            Debug.Log(dict.Key);
        }
    }

    #region 加载资源协程
    static IEnumerator LoadAssetBundle(string abName, string keyword)
    {
        var cache = new Dictionary<string, TextReader>();
        var wwwPath = string.Empty;
#if UNITY_EDITOR_WIN
        wwwPath = string.Format("file://{0}/", Application.streamingAssetsPath);
#else
        wwwPath = string.Format("jar:file://{0}!/assets/", Application.dataPath);
#endif
        var loader = new WWW(wwwPath + abName);
        yield return new WaitUntil(() => loader.isDone);
        if (loader.error != null) yield break;
        var item = AssetBundle.LoadFromMemory(loader.bytes);
        var allAssetNames = item.GetAllAssetNames();
        var allAssets = item.LoadAllAssets<TextAsset>();
        for (var i = 0; i < allAssets.Length; i++)
        {
            var path = allAssetNames[i];
            keyword = keyword.ToLower();
            path = path.ToLower();
            path = path.Substring(path.LastIndexOf(keyword, StringComparison.Ordinal) + keyword.Length);
            path = path.Replace('\\', '_');
            path = path.Replace('/', '_');
            path = path.Trim('_');
            var keyName = path.Substring(0, path.LastIndexOf('.'));
            cache.Add(keyName, new StringReader(allAssets[i].text));
            //Debug.Log("正在加载:" + keyName);
        }
        item.Unload(true);
        loader.Dispose();
        Debug.Log("加载资源包[" + abName + "]完成,共计:" + cache.Count);
        yield return cache;
    }

    #endregion

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值