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
}
加载assetbundle协程并获取返回参数的程序举例
最新推荐文章于 2023-05-07 17:19:28 发布
本文提供了一个Unity示例,演示如何通过协程(IEnumerator)加载AssetBundle资源,并获取返回的Dictionary<string, TextReader>内容。在Start()方法中调用LoadAssetBundle函数,该函数根据平台构建URL,加载AssetBundle,遍历所有资源,将TextAsset转换为StringReader并存储在字典中。最后,协程返回字典并显示其内容。"
128448129,16597425,研究生管理系统设计与实现 - JSP+SSM,"['java', '课程设计', 'mysql']
摘要由CSDN通过智能技术生成