using UnityEngine;

using System.Collections;

using System.IO;

using System.Net;

using System;

using UnityEditor;   

public class WWWLoad : MonoBehaviour

{

    //string urlPath = "http://www.....";

    string urlPath = @"http://localhost:8080/fbx/test.FBX";//资源网络路径(自己写)

    string file_SaveUrl = @"D:\UnityProjects\TEST2\START\Assets\Resources\1比1.FBX";//资源保路径

    FileInfo file;

    bool isLoadModel=false;

    HttpDldFile httpDown_O;

 

    //初始化

    void Start()

    {

        file = new FileInfo(file_SaveUrl);

        httpDown_O = new HttpDldFile();

        Debug.Log(file_SaveUrl);

      

    }

    //启动下载

    public void WWWmodelsa()

    {

        bool run_f = httpDown_O.Download(urlPath, file_SaveUrl);

        AssetDatabase.Refresh();//unity刷新

        isLoadModel = true;

        Debug.Log(urlPath);

    }

    //实例化模型

    public void LoadModelas()

    {

        if (isLoadModel)

        {

              //GameObject objPrefab = (GameObject)Instantiate(AssetDatabase.LoadAssetAtPath(filePath,typeof(GameObject)));  

            //GameObject objPrefab = (MonoBehaviour.Instantiate(mPrefab, Vector3.zero, Quaternion.identity) as GameObject);

            GameObject objPrefab = (GameObject)Resources.Load("1比1");

            Instantiate(objPrefab);

            Debug.Log(objPrefab.GetType().ToString());

        }

       

    }

}

class HttpDldFile

{

    // Http方式下载文件

    public bool Download(string url, string localfile)

    {

        bool flag = false;

        long startPosition = 0; // 上次下载的文件起始位置

        FileStream writeStream; // 写入本地文件流对象

 

        // 判断要下载的文件夹是否存在

        if (File.Exists(localfile))

        {

            writeStream = File.OpenWrite(localfile);             // 存在则打开要下载的文件

            startPosition = writeStream.Length;                  // 获取已经下载的长度

            writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位

        }

        else

        {

            writeStream = new FileStream(localfile, FileMode.Create);// 文件不保存创建一个文件

            startPosition = 0;

        }

        try

        {

            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接

 

            if (startPosition > 0)

            {

                myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置

            }

            Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流

 

            byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容

            int contentSize = readStream.Read(btArray, 0, btArray.Length);// 向远程文件读第一次

 

            while (contentSize > 0)// 如果读取长度大于零则继续读

            {

                writeStream.Write(btArray, 0, contentSize);// 写入本地文件

                contentSize = readStream.Read(btArray, 0, btArray.Length);// 继续向远程文件读取

            }

 

            //关闭流

            writeStream.Close();

            readStream.Close();

 

            flag = true;        //返回true下载成功

        }

        catch (Exception e)

        {

            writeStream.Close();

            flag = false;       //返回false下载失败

        }

        return flag;

    }

}

注:命名空间,加载路径,数据流的接收与大小,会造成卡顿情况,是否打包Apk需要注意using UnityEditor;  AssetDatabase.Refresh();//unity刷新.....

posted on 2018-11-20 14:11  _萧朗  阅读(10740)  评论(3编辑  收藏  举报