unity下载模型到本地并加载

WWW类的下载与加载

从服务器上下载模型

//下载网络模型并缓存到本地
IEnumerator LoadAndSaveAsset(string name)  
{
    string url = null;
    string progress = null;
#if UNITY_ANDROID && !UNITY_EDITOR
    url = @"http://localhost/AssetBundles/android/"+name+"/assetbundle.model";
#elif UNITY_IPHONE && !UNITY_EDITOR
    url = @"http://localhost/AssetBundles/ios/"+name+"/assetbundle.model";
#else
    url = @"http://localhost/AssetBundles/windows/"+name+"/assetbundle.model";
#endif
    Debug.Log(url);
    Debug.Log("开始下载模型。");
    WWW w = new WWW(url);
    while (!w.isDone)
    {
        progress = (((int)(w.progress * 100)) % 100) + "%";
        loadText.text = "下载模型中" + progress;
        yield return null;
    }
    yield return w;
    if (w.isDone)
    {
        loadText.text = "保存模型中";
        byte[] model = w.bytes;
        int length = model.Length;

        //文件流信息  
        Stream sw;
        
        DirectoryInfo t = new DirectoryInfo(Application.persistentDataPath + "/model/" + name);
        if (!t.Exists)
        {
            //如果此文件夹不存在则创建  
            t.Create();
        }
        FileInfo j = new FileInfo(Application.persistentDataPath + "/model/" + name + "/" + "assetbundle.model");
        if (!j.Exists)
        {
            //如果此文件不存在则创建  
            sw = j.Create();
        }
        else
        {
            //如果此文件存在则打开  
            sw = j.OpenWrite();
        }
        sw.Write(model, 0, length);
        //关闭流  
        sw.Close();
        //销毁流  
        sw.Dispose();
    }
}

DirectoryInfo实例的对象来判断文件夹是否存在,FileInfo实例的对象来判断文件是否存在,文件夹创建的方法无返回值,而文件的创建方法返回值是Stream的派生类FileStream类型。

加载网络模型

IEnumerator LoadModel(string url)
{
    string progress = null;
    WWW w = new WWW(url);
    while (!w.isDone)
    {
        progress = (((int)(w.progress * 100)) % 100) + "%";
        loadText.text = "加载模型:" + progress;
        yield return null;
    }
    yield return w;
    if (w.error != null)
    {
        Debug.Log("error:" + w.url + "\n" + w.error);
    }
    else
    {
        AssetBundle bundle = w.assetBundle;
        GameObject modelPre = bundle.LoadAsset<GameObject>("model");
        GameObject modelClone = Instantiate(modelPre);
    }
}

加载本地模型

Application.persistentDataPath路径是一个可读可写的物理路径,是真实存在的,是安装后生成的路径。

IEnumerator LoadModel(string name)
{
    string s = null;
    string progress = null;
#if UNITY_ANDROID && !UNITY_EDITOR
    s = "file://" + Application.persistentDataPath + "/" + name;
#elif UNITY_IPHONE && !UNITY_EDITOR
    s = "file://" + Application.persistentDataPath + "/" + name;  
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    //windows的file协议可能从"file://"变成了"file:///"三斜杠
    s = "file://" + Application.persistentDataPath + "/" + name;  
#endif
    WWW w = new WWW(s);
    while (!w.isDone)
    {
        progress = (((int)(w.progress * 100)) % 100) + "%";
        loadText.text = "加载模型:" + progress;
        yield return null;
    }
    yield return w;
    if (w.error != null)
    {
        Debug.Log("error:" + w.url + "\n" + w.error);
    }
    else
    {
        AssetBundle bundle = w.assetBundle;
        GameObject modelPre = bundle.LoadAsset<GameObject>("model");
        GameObject modelClone = Instantiate(modelPre);
    }
    Debug.Log(s);
}

Application.StreamingAssetsPath路径在移动端是一个只读的路径,是StreamingAssets文件夹编译后的路径。

IEnumerator LoadModel(string name)
{
    string s = null;
    string progress = null;
#if UNITY_ANDROID && !UNITY_EDITOR
    s = "jar:file://" + Application.streamingAssetsPath + "/" + name;
#elif UNITY_IPHONE && !UNITY_EDITOR
    s = "file://" + Application.streamingAssetsPath + "/" + name;  
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
    //windows的file协议可能从"file://"变成了"file:///"三斜杠
    s = "file://" + Application.streamingAssetsPath + "/" + name;  
#endif
    WWW w = new WWW(s);
    while (!w.isDone)
    {
        progress = (((int)(w.progress * 100)) % 100) + "%";
        loadText.text = "加载模型:" + progress;
        yield return null;
    }
    yield return w;
    if (w.error != null)
    {
        Debug.Log("error:" + w.url + "\n" + w.error);
    }
    else
    {
        AssetBundle bundle = w.assetBundle;
        GameObject modelPre = bundle.LoadAsset<GameObject>("model");
        GameObject modelClone = Instantiate(modelPre);
    }
    Debug.Log(s);
}

值得注意的是安卓端Application.persistentDataPath前面没有加"jar:",而Application.StreamingAssetsPath路径前面却是加了"jar:"的。

2019年3.8修正 :最近重新打开旧发布的商业项目,准备第二期开发 ,换了电脑,重新装的unity发现,Windows加载本地文件的方式改了,file协议好像从"file://"变成了"file:///"三斜杠开头,其他平台没变,很大可能是我没免转义,不深究了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值