您好,登錄后才能下訂單哦!
終于從北京回上海了,第一次聽(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ī)器是小端模式。
效果圖
代碼
Model類(lèi):
using System;
using System.Runtime.InteropServices;
namespace mershal
{
class Model
{
[Serializable]
[StructLayout(LayoutKind.Sequential,Pack = 1)]//按1字節(jié)對(duì)齊
publicstruct Student
{
public UInt32 id;
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst=20)]
publicstring name;//姓名
}
}
class Method
{
/// <summary>
/// 結(jié)構(gòu)體轉(zhuǎn)bytes
/// </summary>
/// <param name="structObj">結(jié)構(gòu)體</param>
/// <param name="decCount">默認(rèn)0,不截取</param>
/// <returns></returns>
publicstaticbyte[] StructToBytes(object structObj, Int32 decCount)
{
Int32 size = Marshal.SizeOf(structObj);
//開(kāi)辟空間
IntPtr buffer = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structObj, buffer, false);
byte[] bytes = newbyte[size - decCount];
Marshal.Copy(buffer, bytes, 0, size - decCount);
return bytes;
}
finally
{
//釋放空間
Marshal.FreeHGlobal(buffer);
}
}
/// <summary>
/// byte轉(zhuǎn)結(jié)構(gòu)體
/// </summary>
/// <param name="bytes">byte數(shù)組</param>
/// <param name="type">結(jié)構(gòu)體類(lèi)型</param>
/// <returns></returns>
publicstaticobject ByteToStruct(byte[] bytes, Type type)
{
Int32 size = Marshal.SizeOf(type);
//byte數(shù)組長(zhǎng)度小于結(jié)構(gòu)體大小
if (size > bytes.Length)
{
//返回空
returnnull;
}
//分配結(jié)構(gòu)大小的內(nèi)存空間
IntPtr structPtr = Marshal.AllocHGlobal(size);
//將byte數(shù)組拷貝到分配好的內(nèi)存空間
Marshal.Copy(bytes, 0, structPtr, size);
//將內(nèi)存空間轉(zhuǎn)換成目標(biāo)結(jié)構(gòu)
object obj = Marshal.PtrToStructure(structPtr, type);
//釋放內(nèi)存空間
Marshal.FreeHGlobal(structPtr);
//返回結(jié)構(gòu)
return obj;
}
}
}
主函數(shù):
using System;
namespace mershal
{
class Program
{
staticvoid Main(string[] args)
{
//實(shí)例化
Model.Student stu1 = new Model.Student();
stu1.id = 1;
stu1.name = "丁小未";
//打包
byte[] byte1 = Method.StructToBytes(stu1,0);
Console.WriteLine("字節(jié)長(zhǎng)度:"+byte1.Length);
//解析
Model.Student stu = (Model.Student)Method.ByteToStruct(byte1, typeof(Model.Student));
Console.WriteLine("\n輸出的學(xué)生信息\nid:" + stu.id+"\nname:"+stu.name);
Console.Read();
}
}
}
通信方面可以參考我之前寫(xiě)的,然后結(jié)合此文,來(lái)做自己的網(wǎng)絡(luò)游戲!
socket通訊
更多教程,歡迎關(guān)注我的微博 !
免責(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)容。