Unity3D網(wǎng)絡(luò)通信(精編版)_第1頁(yè)
Unity3D網(wǎng)絡(luò)通信(精編版)_第2頁(yè)
Unity3D網(wǎng)絡(luò)通信(精編版)_第3頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、unity3d 網(wǎng)絡(luò)通信自己寫(xiě)的測(cè)試demo ,一個(gè)功能一個(gè)功能測(cè)試著做的,沒(méi)有什么結(jié)構(gòu),湊合看吧。http 協(xié)議,在手機(jī)平臺(tái),url 必須必帶http:/ 頭。此腳本主要實(shí)現(xiàn)了分別用 pose 和 get 方式獲取天氣預(yù)報(bào)信息(xml 格式) 。解析 xml 獲取網(wǎng)絡(luò)圖片獲取網(wǎng)絡(luò)圖片(base64 格式)base64 與 byte互轉(zhuǎn) byte 與 texture2d (圖片)互轉(zhuǎn)更多常用 webservice :http:/ using unityengine; using system.collections; using system.collections.generic; usin

2、g system.xml; using system.io; public class httpdemo : monobehaviour public string hostname = http:/; /城市天氣預(yù)報(bào)服務(wù)public string urlpath = /webservices/weatherwebservice.asmx/getweatherbycityname; /獲得驗(yàn)證碼服務(wù)(直接獲得圖片)private string picturename = /webservices/validatecodewebservice.asmx/cnvalidateimage?bystr

3、ing=picture; /獲得驗(yàn)證碼服務(wù) (獲得圖片字節(jié)流)private string picturebytename = /webservices/validatecodewebservice.asmx/cnvalidatebyte?bystring=picbyte; private texture2d mpicture; private texture2d mpicturebyte; private texture2d mconvertpng; public string parameters = new string thecityname ; private string xmlc

4、ontent = null; public string testc = null; void ongui() /顯示測(cè)試信息gui.label(new rect(100, 10, 1000, 38), testc); /表單傳值if (gui.button(new rect(10, 50, 100, 60), post) postweatherbycityname(北京); gui.button(new rect(120, 80, 100 + getjindu() * 100, 20), (getjindu() * 100) + %); /get 傳值( android 平臺(tái)不支持中文參數(shù))

5、if (gui.button(new rect(10, 130, 100, 60), get) getweatherbycityname(58367);/上海 gui.button(new rect(120, 150, 100 + getjindu() * 100, 20), (getjindu() * 100) + %); /顯示讀取到的天氣預(yù)報(bào)原始信息(xml 格式)gui.label(new rect(10, 220, 380, 500), mcontent); /解析 xml if (gui.button(new rect(500, 200, 120, 60), analysisxml

6、) xmlcontent = analysisxml(); gui.label(new rect(410, 220, 380, 500), xmlcontent); /下載網(wǎng)絡(luò)圖片if (gui.button(new rect(10, 750, 80, 60), downpic) downloadpicture(picturename); gui.label(new rect(100, 760, 200, 200), mpicture); /下載網(wǎng)絡(luò)圖片(base64 格式)if (gui.button(new rect(350, 750, 80, 60), downpicbyte) down

7、loadpicturebyte(picturebytename); gui.label(new rect(450, 760, 200, 200), mpicturebyte); public void postweatherbycityname(string str) /將參數(shù)集合封裝到dictionary 集合方便傳值dictionary<string, string> dic = new dictionary<string, string>(); /參數(shù)dic.add(parameters0, str); startcoroutine

8、(post(hostname + urlpath , dic); public void getweatherbycityname(string str) /將參數(shù)集合封裝到dictionary集合方便傳值dictionary<string, string> dic = new dictionary<string, string>(); /參數(shù)dic.add(parameters0, str); startcoroutine(get(hostname + urlpath , dic); /下載圖片public void downloadp

9、icture(string picname) testc =picurl = + picname; startcoroutine(gettexture(hostname + picname); /下載圖片(字節(jié)流)public void downloadpicturebyte(string picname) startcoroutine(gettexturebyte(hostname + picname); /*-helper-*/ private float mjindu = 0; private string mcontent; public float getjindu() return

10、 mjindu; /post 請(qǐng)求 (form 表單傳值、 效率低、安全,) ienumerator post(string url, dictionary<string, string> post) /表單wwwform form = new wwwform(); /從集合中取出所有參數(shù),設(shè)置表單參數(shù)(addfield(). foreach (keyvaluepair<string, string> post_arg in post) form.addfield(post_arg.key, post_arg.value); /表單傳值,

11、就是post www www = new www(url, form); yield return www; mjindu = gress; if (www.error != null) /post 請(qǐng)求失敗mcontent = error : + www.error; else /post 請(qǐng)求成功mcontent = www.text; /get請(qǐng)求( url? 傳值、效率高、不安全)ienumerator get(string url, dictionary<string, string> get) string parameters; bool

12、 first; if (get.count > 0) first = true; parameters = ?; /從集合中取出所有參數(shù),設(shè)置表單參數(shù)( addfield(). foreach (keyvaluepair<string, string> post_arg in get) if (first) first = false; else parameters += & parameters += post_arg.key + = + post_arg.value; else parameters = ; testc =getu

13、rl : + parameters; /直接 url 傳值就是 get www www = new www(url + parameters); yield return www; mjindu = gress; if (www.error != null) /get 請(qǐng)求失敗mcontent = error : + www.error; else /get 請(qǐng)求成功mcontent = www.text; ienumerator gettexture(string picurl) www wwwtexture = new www(picurl); yield return ww

14、wtexture; if (wwwtexture.error != null) /get 請(qǐng)求失敗debug.log(error : + wwwtexture.error); else /get 請(qǐng)求成功mpicture = wwwtexture.texture; string picbyte; ienumerator gettexturebyte(string picurl) www www = new www(picurl); yield return www; if (www.error != null) /get 請(qǐng)求失敗debug.log(error : + www.error);

15、else /get 請(qǐng)求成功debug.log(picbytes text = + www.text); xmldocument xmldoc = new xmldocument(); xmldoc.load(new stringreader(www.text); /通過(guò)索引查找子節(jié)點(diǎn)picbyte = xmldoc.getelementsbytagname(base64binary).item(0).innertext; testc = picbyte; mpicturebyte = bttetopic(picbyte); /解析 xml string analysisxml() strin

16、g str = ; xmldocument xmldoc = new xmldocument(); xmldoc.load(new stringreader(mcontent); /得到文檔根節(jié)點(diǎn)的所有子節(jié)點(diǎn)集合/xmlnodelist nodes = xmldoc.documentelement.childnodes; /通過(guò)節(jié)點(diǎn)名得到節(jié)點(diǎn)集合x(chóng)mlnodelist nodes = xmldoc.getelementsbytagname(string); /通過(guò)索引查找子節(jié)點(diǎn)str += item1 = + xmldoc.getelementsbytagname(string).item(1

17、).innertext + nn; /遍歷所有子節(jié)點(diǎn)foreach (xmlelement element in nodes) if (element.name = string) str += element.innertext + n; return str; /圖片與 byte 互轉(zhuǎn)public void convertpng(texture2d pic) byte data = pic.encodetopng(); debug.log(data = + data.length + | + data0); mconvertpng = new texture2d(200, 200); mconvertpng.loadimage(data); /byte與 base64 互轉(zhuǎn)texture2d b

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論