日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

[原]unity3d之http多线程异步资源下载

發布時間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [原]unity3d之http多线程异步资源下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鄭重聲明:轉載請注明出處 U_探索

本文誕生于樂元素面試過程,被面試官問到AssetBundle多線程異步下載時,愣了半天,同樣也被深深的鄙視一回(做了3年多u3d 這個都沒用過),所以發誓要實現出來填補一下自己的空白,同時分享給大家。說明:本人只在pc和Android下測試好使,其他平臺未知!

直接貼代碼,都是C# http的API,不懂得自己百科。

using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.Net;
using System.IO;internal class WebReqState
{public byte[] Buffer;public FileStream fs;public const int BufferSize = 1024;public Stream OrginalStream;public HttpWebResponse WebResponse;public WebReqState(string path){Buffer = new byte[1024];fs = new FileStream(path,FileMode.Create);}}public class HttpHelper {string path = null;string assetName;public HttpHelper(string path){this.path = path;}public  void AsyDownLoad(string url)
{Debug.Log(url);assetName = url.Split('/')[4];Debug.Log(assetName);HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;httpRequest.BeginGetResponse( new AsyncCallback(ResponseCallback), httpRequest);
}void ResponseCallback(IAsyncResult ar)
{HttpWebRequest req = ar.AsyncState as HttpWebRequest;if(req == null) return;HttpWebResponse response = req.EndGetResponse(ar) as HttpWebResponse;if(response.StatusCode != HttpStatusCode.OK){response.Close();return;}Debug.Log(path+ "/"+assetName);WebReqState st = new WebReqState(path+ "/"+assetName);st.WebResponse = response;Stream responseStream = response.GetResponseStream();st.OrginalStream = responseStream;responseStream.BeginRead(st.Buffer,0,WebReqState.BufferSize,new AsyncCallback(ReadDataCallback),st);
}void ReadDataCallback(IAsyncResult ar)
{WebReqState rs = ar.AsyncState as WebReqState;int read =rs.OrginalStream.EndRead(ar);if(read>0){rs.fs.Write(rs.Buffer,0,read);rs.fs.Flush();rs.OrginalStream.BeginRead(rs.Buffer, 0, WebReqState.BufferSize, new AsyncCallback(ReadDataCallback), rs);}else{rs.fs.Close();rs.OrginalStream.Close();rs.WebResponse.Close();Debug.Log(assetName+":::: success");}
}
}
View Code

下載部分:

if(GUI.Button(new Rect(0,0,100,30),"test"))
{string rootPath = Application.persistentDataPath;//android上保存到 /storage/sdcard0/Android/data/包名(例如:com.example.test)/filesfor(int i =0;i<str.Length;i++) //str是string型數組,存放部分assetbundle名字
    {Thread thread = new Thread(new ParameterizedThreadStart(DownAsset)); //ParameterizedThreadStart 多線程傳參 thread.Start(rootPath+"|"+str[i]); //只能帶一個object參數 所以使用字符串拼接
    }
} void DownAsset(System.Object file)
{string[] fileName = file.ToString().Split('|');HttpHelper help = new HttpHelper(fileName[0]);help.AsyDownLoad("http://192.168.0.103/unity/"+fileName[1]+".AssetBundle");//注意在手機上測試 該對Ip地址
}

?

下載完成后 可以去/storage/sdcard0/Android/data/包名(例如:com.example.test)/files查找對應文件

加載部分:

if(GUI.Button(new Rect(0,30,100,30),"load"))
{for(int i =0;i<str.Length;i++){StartCoroutine(LoadAsset(str[i],i));}
}
IEnumerator LoadAsset(string name,int i)
{WWW w = new WWW("file://"+Application.persistentDataPath+"/"+name+".AssetBundle");yield return w;Instantiate(w.assetBundle.mainAsset,new Vector3(i*2,0,0),Quaternion.identity);w.assetBundle.Unload(false);
}

?

注意事項:

1、pc測試 需要修改IP地址,本地測試改為127.0.0.1 同時Application.persistentDataPath最好做修改,因為在pc上Application.persistentDataPath:C:\Users\用戶名\AppData\LocalLow\DefaultCompany\u3d工程名,可以下載到此文件夾下,但是加載的時候會報錯,沒什么權限之類的

2、android上需要stripping level設置為Disabled

轉載于:https://www.cnblogs.com/U-tansuo/p/unity3d_Threading_AsyDown_HTTP.html

總結

以上是生活随笔為你收集整理的[原]unity3d之http多线程异步资源下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。