您的位置:首页 > 移动开发 > Unity3D

unity3d远程加载资源模型到本地并加载(二)第一次加载资源后写到本地后从本地加载。

2016-01-13 15:11 671 查看
using UnityEngine;

using System.Collections;

using System.IO;

public class Resource : MonoBehaviour

{

private string sceneUrl = “file:///Z:/场景.assetbundle”;

private string modleUrl = “file:///Z:/modle/modle.assetbundle”;

public Transform gameobject_dajiaolu;

public Transform gameobject_jianchihu;

public Transform gameobject_qingting;

public Transform gameobject_shizuniao;

public Transform gameobject_jialong;

public Transform gameobject_dodoniao;

public Transform gameobject_sanjiaolong;

public string LocalModleAssetUrl;

bool isDone = false;

// Use this for initialization

void Start()

{

StartCoroutine(GetResource());

Debug.Log(LocalModleAssetUrl);

}

// Update is called once per frame
void Update()
{

}
//流的形式写文件


//创建流

//判断文件是否存在,没有则创建

//写入信息

public void createAsset(string name ,byte[] info, int length)
{

Stream sw; //声明流
FileInfo f = new FileInfo(Application.persistentDataPath + "/" + name);//文件信息

Debug.Log(Application.persistentDataPath + "/" + name);

if (!f.Exists)//如果文件不存在
{
Debug.Log("文件已写入本地");
sw = f.Create();//创建文件

}
else {
Debug.Log("文件已存在");
StartCoroutine(loadLocalAsset());
return;
}
sw.Write(info,0,length);
sw.Close();
sw.Dispose();
Debug.Log("文件已写入本地,请重启客户端谢谢!!!!!!!!!!!");

}
//加载模型文件
IEnumerator GetResource()
{

WWW www = new WWW(modleUrl);
yield return www;
//写入本地!!!
if (www.isDone)
{
byte[] modle = www.bytes;
int length = modle.Length;
createAsset("modleAsset.assetbundle", modle, length);
Debug.Log("正要写入模型");
}
}
private IEnumerator loadLocalAsset()
{

string url = "file:///" + Application.persistentDataPath + "/" + "modleAsset.assetbundle";
WWW local = new WWW(url);

yield return local;
if (local.isDone)
{
Debug.Log("从本地获取完成!!!");

AssetBundle modleBundle = local.assetBundle;

// GameObject  cube= modleBundle.LoadAsset("Cube")as GameObject;

GameObject dajiaolu = Instantiate(modleBundle.LoadAsset("dajiaolu")) as GameObject;//!!!!!!!!!

GameObject qingting = Instantiate(modleBundle.LoadAsset("qingting")) as GameObject;//!!!!!!!!!
GameObject dodoniao = Instantiate(modleBundle.LoadAsset("dodoniao")) as GameObject;//!!!!!!!!!

GameObject jianchihu = Instantiate(modleBundle.LoadAsset("jianchihu")) as GameObject;//!!!!!!!!!
GameObject shizuniao = Instantiate(modleBundle.LoadAsset("shizuniao")) as GameObject;//!!!!!!!!!
GameObject jialong = Instantiate(modleBundle.LoadAsset("jialong")) as GameObject;//!!!!!!!!!
GameObject sanjiaolong = Instantiate(modleBundle.LoadAsset("sanjiaolong")) as GameObject;//!!!!!!!!!

dajiaolu.transform.SetParent(gameobject_dajiaolu.transform);
dajiaolu.transform.localPosition = new Vector3(0, 0, 0);
dajiaolu.transform.localScale = new Vector3(1, 1, 1);

qingting.transform.SetParent(gameobject_qingting.transform);
qingting.transform.localPosition = new Vector3(0, 0, 0);
qingting.transform.localScale = new Vector3(1, 1, 1);

dodoniao.transform.SetParent(gameobject_dodoniao.transform);
dodoniao.transform.localPosition = new Vector3(0, 0, 0);
dodoniao.transform.localScale = new Vector3(1, 1, 1);

jianchihu.transform.SetParent(gameobject_jianchihu.transform);
jianchihu.transform.localPosition = new Vector3(0, 0, 0);
jianchihu.transform.localScale = new Vector3(1, 1, 1);

shizuniao.transform.SetParent(gameobject_shizuniao.transform);
shizuniao.transform.localPosition = new Vector3(0, 0, 0);
shizuniao.transform.localScale = new Vector3(1, 1, 1);

jialong.transform.SetParent(gameobject_jialong.transform);
jialong.transform.localPosition = new Vector3(0, 0, 0);
jialong.transform.localScale = new Vector3(1, 1, 1);

sanjiaolong.transform.SetParent(gameobject_sanjiaolong.transform);
sanjiaolong.transform.localPosition = new Vector3(0, 0, 0);
sanjiaolong.transform.localScale = new Vector3(1, 1, 1);

modleBundle.Unload(false);//释放占用的内存!

yield return new WaitForSeconds(3);

//WWW scene = WWW.LoadFromCacheOrDownload(sceneUrl, 1);

////WWW scene = new WWW(sceneUrl);
//yield return scene;
//AssetBundle sceneBundle = scene.assetBundle;
//Application.LoadLevel("123");

}
}


}

ps:文件保存到Application.persistentDataPath后 在用WWW读取的话要用

string url = “file:///” + Application.persistentDataPath + “/” + “modleAsset.assetbundle”;路径

参考:http://blog.csdn.net/dingxiaowei2013/article/details/19084859
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity3d 远程加载 WWW