Unity教程之-Unity中WWW加载AssetBundle(二)

 

上篇文章 Unity教程之-Unity3d打包Assetbundle并加载,这篇我们来学习下利用FileStream在读取我们的AssetBundle数据文件,并解决unity WWW中文路径问题。下面开始,因为FileStream是允许中文路径的,可以先通过文件流把AssetBundle读取到内存,在通过CreateFromMemory 创建AssetBundle资源,这样的话就避开了WWW不能加载中文路径的问题。贴上代码


using UnityEngine;
using System.Collections;
using System.IO;
public class LoadAsset : MonoBehaviour {

AssetBundleCreateRequest asset;//定义一个资源包创建请求
IEnumerator LoadAssetBundle()
{

FileStream AssetIO = new FileStream(Application.dataPath + @"/StreamingAssets/好孩子/001.dat", FileMode.Open, FileAccess.ReadWrite); //创建文件流(对象现含有中文)
byte[] assetbytes = new byte[AssetIO.Length];
AssetIO.Read(assetbytes, 0, (int)AssetIO.Length);
AssetIO.Close();

asset = AssetBundle.CreateFromMemory(assetbytes);//从内存中创建资源
yield return asset;

AssetBundle LoadAsset = asset.assetBundle;//这样就能得到我们需要的资源包了
if (asset.isDone)
{
Instantiate(LoadAsset.Load("Test_001"));
}
}

好了,本篇Unity3d教程到此结束!