[. NET2.0] use of the Marshal class

Source: Internet
Author: User
Tags unity 5

Finally, I went back to Shanghai from Beijing. I heard about the new features of unity 5 at the Unity Developer Conference for the first time. In fact, I still need to find new functions on my own, it is mainly to add a new GUI system. It seems that NGUI is integrated into Unity, named UGUI, and new sound system and new animation system are integrated, I feel that the new sound system is still quite powerful and I look forward to the advent of unity5. Some common plug-ins and the use of Test Tools were introduced at the Conference. In general, there are many future prospects. Hope Unity will become more and more powerful! Not to mention anything. Next we will introduce a common method of client server communication-the Marshal class, which is a class in. NETFramework2.0, so we can use it in Unity. Similar to this class, there are also litjson, which may be used to save the byte space. The Marshal class only packs the value into a bytes stream, while json also contains the previous key value. Of course, you can also select the json method. Here I will only introduce the use of the Marshal class. Click here to go to the introduction and use of the Marshal class in MSDN. First of all, you need to know about the large-end and small-end modes of byte order. Click to read this article. Here, the window machine I use is in the small-end mode. Code Model class: [csharp] using System; using System. runtime. interopServices; namespace mershal {class Model {[Serializable] [StructLayout (LayoutKind. sequential, Pack = 1)] // One-byte alignment public struct Student {public UInt32 id; [financialasattribute (UnmanagedType. byValTStr, SizeConst = 20)] public string name; // name} class Method {// <summary> // convert the struct to bytes /// </summary> /// <param name = "structObj"> struct </param> /// <Param name = "decCount"> 0 by default, not truncated </param> /// <returns> </returns> public static byte [] StructToBytes (object structObj, int32 decCount) {Int32 size = Marshal. sizeOf (structObj); // open up the space IntPtr buffer = Marshal. allocHGlobal (size); try {Marshal. structureToPtr (structObj, buffer, false); byte [] bytes = new byte [size-decCount]; Marshal. copy (buffer, bytes, 0, size-decCount); return bytes;} finally {// Release space Marshal. freeHGlobal (buffer );}} /// <summary> /// byte to convert struct /// </summary> /// <param name = "bytes"> byte array </param> /// <param name = "type"> struct type </param> // <returns> </returns> public static object ByteToStruct (byte [] bytes, type type) {Int32 size = Marshal. sizeOf (type); // The length of the byte array is smaller than the size of the struct if (size> bytes. length) {// return null return;} // allocate the memory size of the structure IntPtr structPtr = Marshal. allocHGlobal (Size); // copy the byte array to the allocated memory space Marshal. copy (bytes, 0, structPtr, size); // converts the memory space to the Target Structure object obj = Marshal. ptrToStructure (structPtr, type); // release memory space Marshal. freeHGlobal (structPtr); // return structure return obj ;}} Main function: [csharp] using System; namespace mershal {class Program {static void Main (string [] args) {// instantiate the Model. student stu1 = new Model. student (); stu1.id = 1; stu1.name = "ding Xiaowei"; // package byte [] byte1 = Method. structToBytes (stu1, 0); Console. writeLine ("Byte Length:" + byte1.Length); // parse Model. student stu = (Model. student) Method. byteToStruct (byte1, typeof (Model. student); Console. writeLine ("\ n output student information \ nid:" + stu. id + "\ nname:" + stu. name); Console. read () ;}} for more information about communication, refer to the one I wrote earlier and use this article to create your own online game!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.