How data in the C # Unity game development--excel is in the game (iii)

Source: Internet
Author: User

This post is continuous: how the data in the C # Unity game development--excel is in the game (ii)

A few days ago something so did not continue to update, today we went on to say. In the last post we saw that the Excel data has been generated in the. bin file, but in fact it is still not available in the game. There are two main aspects, the 1.bin file suffix makes us casually take the name, but this file unity does not buy. Because the binaries in unity must be named with. Bytes. 2. Before writing a file, you can actually compress the binary so that you can maximize your device space savings. In other words, we need to do several things after we generate the data instance: serialization---compression--write the file.

The way is basically the same way as in the post, but because we want to compress the serialized data, we can't serialize the data into the file stream directly, the second is serialize into a memorystream and then take out the binary data compression and then write the file.

 MemoryStream ms = new   MemoryStream ();        Serializer.serialize  <staticdata> (MS, (Staticdata) staticdata);  byte  [] byts = Ms.        ToArray (); System.IO.FileStream stream1  = new  System.IO.FileStream ( Application.datapath +  /resources/ Staticdatas.bytes    = new          System.IO.BinaryWriter (STREAM1); Writer. Write (GZIP.        Encode (Byts)); Writer.        Close (); Stream1. Close ();  

Oh, plus these words will be OK. Explain that we use ICSHARPCODE.SHARPZIPLIB.GZIP this dll,gzip use method can refer to this address http://community.sharpdevelop.net/forums/t/11005. ASPX, here I wrote a gzip tool class on two ways

     Public Static byte[] Encode (byte[] bin) {MemoryStream ms=NewMemoryStream (); Gzipoutputstream Gzp=NewGzipoutputstream (MS); Gzp. Write (Bin,0, bin.        Length); Gzp.        Close (); returnMs.    ToArray (); }     Public Static byte[] Decode (byte[] bin) {Gzipinputstream Gzp=NewGzipinputstream (NewMemoryStream (bin)); MemoryStream re=NewMemoryStream (); intCount =0; byte[] data =New byte[4096];  while(Count = Gzp. Read (data,0, data. Length))! =0) {Re. Write (data,0, Count); }        returnRe.    ToArray (); }
View Code

Then, we're looking at how it's used in the game. The use is also very simple, because the front has already generated the class and so on, so, look at the code.

        byte [] bytes = (resources.load ("staticdatas" as textasset). bytes;         New MemoryStream (GZIP). Decode (bytes));         = Serializer.deserialize<staticdata> (ms);

Now we can see the data in the game, the use of the time direct data.

Oh, this is the end of the subject.

Everyone will go through the process of not meeting, if there are any flaws also ask you to advise! Thank you!

How data in the C # Unity game development--excel is in the game (iii)

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.