您的位置:首页 > 其它

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

2017-07-24 10:43 537 查看
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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息