溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Marshal類(lèi)的簡(jiǎn)單介紹

發(fā)布時(shí)間:2020-07-12 03:06:21 來(lái)源:網(wǎng)絡(luò) 閱讀:7416 作者:蓬萊仙羽 欄目:編程語(yǔ)言

終于從北京回上海了,第一次聽(tīng)unity開(kāi)發(fā)者大會(huì),感覺(jué)講的都是一些Unity 5新功能的介紹,其實(shí)主要還是要靠自己去摸索那些新的功能,主要就是添加了新的GUI系統(tǒng),貌似集成了NGUI到Unity中,取名UGUI,還有就是集成了新的聲音系統(tǒng)和新的動(dòng)畫(huà)系統(tǒng),我感覺(jué)新的聲音系統(tǒng)還是比較強(qiáng)大的,期待unity5的問(wèn)世。大會(huì)上介紹了一些常用的插件以及Test Tools的使用,總體而言都是一些展望未來(lái)性質(zhì)多點(diǎn)哈。希望Unity越來(lái)越強(qiáng)大!

廢話(huà)不多說(shuō),接下來(lái)介紹一下客戶(hù)端服務(wù)器通訊常用的一種方法——Marshal類(lèi),這個(gè)類(lèi)是.NETFramework2.0中的類(lèi),所以我們能夠?qū)⑵溆糜赨nity中。與這個(gè)類(lèi)類(lèi)似的還有l(wèi)itjson等,可能是為了節(jié)省字節(jié)空間,Marshal類(lèi)只僅僅將值進(jìn)行打包成bytes流,而json還包含前面的key值。當(dāng)然你也可以選擇json的方式,我這里僅僅介紹Marshal類(lèi)的使用。點(diǎn)擊進(jìn)入MSDN中Marshal類(lèi)的介紹和使用

在這之前首先要了解一下關(guān)于字節(jié)序的大端和小端模式,點(diǎn)擊閱讀,可以參考這篇文章了解一下。這里我用的window的機(jī)器是小端模式。

效果圖

Marshal類(lèi)的簡(jiǎn)單介紹

Marshal類(lèi)的簡(jiǎn)單介紹

代碼

Model類(lèi):


[csharp]view plaincopyprint?Marshal類(lèi)的簡(jiǎn)單介紹Marshal類(lèi)的簡(jiǎn)單介紹
  1. using System;  

  2. using System.Runtime.InteropServices;  

  3. namespace mershal  

  4. {  

  5. class Model  

  6.    {  

  7.        [Serializable]  

  8.        [StructLayout(LayoutKind.Sequential,Pack = 1)]//按1字節(jié)對(duì)齊

  9. publicstruct Student  

  10.        {  

  11. public UInt32 id;  

  12.            [MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst=20)]  

  13. publicstring name;//姓名

  14.        }  

  15.    }  

  16. class Method  

  17.    {  

  18. /// <summary>

  19. /// 結(jié)構(gòu)體轉(zhuǎn)bytes

  20. /// </summary>

  21. /// <param name="structObj">結(jié)構(gòu)體</param>

  22. /// <param name="decCount">默認(rèn)0,不截取</param>

  23. /// <returns></returns>

  24. publicstaticbyte[] StructToBytes(object structObj, Int32 decCount)  

  25.        {  

  26.            Int32 size = Marshal.SizeOf(structObj);  

  27. //開(kāi)辟空間

  28.            IntPtr buffer = Marshal.AllocHGlobal(size);  

  29. try

  30.            {  

  31.                Marshal.StructureToPtr(structObj, buffer, false);  

  32. byte[] bytes = newbyte[size - decCount];  

  33.                Marshal.Copy(buffer, bytes, 0, size - decCount);  

  34. return bytes;  

  35.            }  

  36. finally

  37.            {  

  38. //釋放空間

  39.                Marshal.FreeHGlobal(buffer);  

  40.            }  

  41.        }  

  42. /// <summary>

  43. /// byte轉(zhuǎn)結(jié)構(gòu)體

  44. /// </summary>

  45. /// <param name="bytes">byte數(shù)組</param>

  46. /// <param name="type">結(jié)構(gòu)體類(lèi)型</param>

  47. /// <returns></returns>

  48. publicstaticobject ByteToStruct(byte[] bytes, Type type)  

  49.        {  

  50.            Int32 size = Marshal.SizeOf(type);  

  51. //byte數(shù)組長(zhǎng)度小于結(jié)構(gòu)體大小

  52. if (size > bytes.Length)  

  53.            {  

  54. //返回空

  55. returnnull;  

  56.            }  

  57. //分配結(jié)構(gòu)大小的內(nèi)存空間

  58.            IntPtr structPtr = Marshal.AllocHGlobal(size);  

  59. //將byte數(shù)組拷貝到分配好的內(nèi)存空間

  60.            Marshal.Copy(bytes, 0, structPtr, size);  

  61. //將內(nèi)存空間轉(zhuǎn)換成目標(biāo)結(jié)構(gòu)

  62. object obj = Marshal.PtrToStructure(structPtr, type);  

  63. //釋放內(nèi)存空間

  64.            Marshal.FreeHGlobal(structPtr);  

  65. //返回結(jié)構(gòu)

  66. return obj;  

  67.        }  

  68.    }  

  69. }  


主函數(shù):

[csharp]view plaincopyprint?Marshal類(lèi)的簡(jiǎn)單介紹Marshal類(lèi)的簡(jiǎn)單介紹
  1. using System;  

  2. namespace mershal  

  3. {  

  4. class Program  

  5.    {  

  6. staticvoid Main(string[] args)  

  7.        {  

  8. //實(shí)例化

  9.            Model.Student stu1 = new Model.Student();  

  10.            stu1.id = 1;  

  11.            stu1.name = "丁小未";  

  12. //打包

  13. byte[] byte1 = Method.StructToBytes(stu1,0);  

  14.            Console.WriteLine("字節(jié)長(zhǎng)度:"+byte1.Length);  

  15. //解析

  16.            Model.Student stu =  (Model.Student)Method.ByteToStruct(byte1, typeof(Model.Student));  

  17.            Console.WriteLine("\n輸出的學(xué)生信息\nid:" + stu.id+"\nname:"+stu.name);  

  18.            Console.Read();  

  19.        }  

  20.    }  

  21. }  


通信方面可以參考我之前寫(xiě)的,然后結(jié)合此文,來(lái)做自己的網(wǎng)絡(luò)游戲!

socket通訊

更多教程,歡迎關(guān)注我的微博 !


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI