1. 程式人生 > >unity動態載入FBX模型(Http下載到Rescources檔案,場景Load直接呼叫):

unity動態載入FBX模型(Http下載到Rescources檔案,場景Load直接呼叫):

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重新整理.....