Unity3d game resource packaging and encryption (image/XML/TXT, etc.) C # Coding (1 ),

Source: Internet
Author: User

Unity3d game resource packaging and encryption (image/XML/TXT, etc.) C # Coding (1 ),

This article only describes the process and uses a very simple packaging encryption method. As for the encryption result, Please modify it as needed. The Byte offset and the backward and backward encryption methods can all be used, however, it generally does not need to be so complex, and the encryption is too complex, which is also a burden for games that are extremely pursuing operational efficiency.


Unity itself is compressed and encrypted, but its decryption algorithm is everywhere on the Internet. If you think that the information in the game is confidential, please encrypt it on your own.


Principles of packaging encryption:

1. We all know that files are composed of bytes.

2. The reason why an image looks beautiful is that its data is arranged in a certain order.

Pretty jianling girl


We can use a text editor to open it.

It's garbled, or what else do you want to see?


3. What if we disrupt image data or add messy data to the front?


Well, the image is not displayed.

This is easy to understand, just as you have been mosaic when watching a video ..

Think about why mosaic?

It's not for confidentiality...


Okay, we have enabled the confidentiality-encryption feature.


4. I have another picture ...... Cute ..



Open the image in a text editor.

Copy all the content to the end of the first image file.

What will happen?

Will the two images be combined?



Unfortunately ......

However, this is in line with my topic. Encryption is hard to see.


Here is the principle. The following is the code.

The code is not for a single file, but for packaging and encryption of Multiple folders.

The encryption method is to package multiple files.


Bytes -----------------------------------------------------------------------------------

I am a cute split line

Bytes -----------------------------------------------------------------------------------


Knowledge points used:

1. Read and Write files

2. Operations on folders and files to obtain all files


Bytes -----------------------------------------------------------------------------------

I am a cute split line

Bytes -----------------------------------------------------------------------------------

The following is the main code:


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace MyPackRes{    class Helper    {        public static void Log(string str)        {            Console.Write(str+"\n\n");        }        public static void Pause()        {            Console.Write("Press any key to continue . . . ");            Console.ReadKey(true);        }    }}

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. IO; using System. diagnostics; namespace MyPackRes {class Program {private static int m_id = 0; private static int m_totalSize = 0; // private static List <int> m_idList = new List <int> (); // private static List <int> m_startPosList = new List <int> (); // private static List <int> m_size List = new List <int> (); // private static List <string> m_pathList = new List <string> (); private static Dictionary <int, oneFileInfor> m_allFileInfoDic = new Dictionary <int, OneFileInfor> (); private static string m_currentDir = ""; static void Main (string [] args) {m_currentDir = Environment. currentDirectory; Helper. log ("MyPackRes Start" + m_currentDir); List <string> folderToPackList = new List <string> ();/** Read configuration file **/Helper. log ("the folder to be packaged:"); StreamReader streamReader = new StreamReader ("MyPackRes. ini ", Encoding. default); string line; while (line = streamReader. readLine ())! = Null) {Helper. Log (line); if (! FolderToPackList. contains (line) {folderToPackList. add (line) ;}} streamReader. close ();/** traverse the packaging folder **/for (int index = 0; index <folderToPackList. count; index ++) {PackFolder (folderToPackList [index]);} Helper. log ("package finished! "); Helper. pause ();}/** retrieve all file information through the folder **/private static void TraverseFolder (string foldername) {Helper. log ("traverse folder" + foldername);/** read information of all files in the folder **/DirectoryInfo dirInfo = new DirectoryInfo (foldername); foreach (FileInfo fileinfo in dirInfo. getFiles ("*. * ", SearchOption. allDirectories) {string filename = (fileinfo. fullName. replace (m_currentDir + "\\","")). replace ("\", "/"); int filesize = (int) fileinfo. length; Helper. log (m_id + ":" + filename + "file size:" + filesize); OneFileInfor info = new OneFileInfor (); info. m_id = m_id; info. m_Size = filesize; info. m_Path = filename;/** read this file **/FileStream fileStreamRead = new FileStream (fileinfo. fullName, FileMode. open, FileAccess. read); if (fileStreamRead = null) {Helper. log ("failed to read file:" + fileinfo. fullName); Helper. pause (); return;} else {byte [] filedata = new byte [filesize]; fileStreamRead. read (filedata, 0, filesize); info. m_data = filedata;} fileStreamRead. close (); m_allFileInfoDic.Add (m_id, info); m_id ++; m_totalSize + = filesize ;}/ ** package a folder **/private static void PackFolder (string foldername) {TraverseFolder (foldername); Helper. log ("file quantity:" + m_id); Helper. log ("total file size:" + m_totalSize);/** update the start point of the file in UPK **/int firstfilestartpos = 4 + (4 + 4 + 4 + 256) * m_allFileInfoDic.Count; int startpos = 0; for (int index = 0; index <m_allFileInfoDic.Count; index ++) {if (index = 0) {startpos = firstfilestartpos ;} else {startpos = m_allFileInfoDic [index-1]. m_StartPos + m_allFileInfoDic [index-1]. m_Size; // start + file size of the previous file;} m_allFileInfoDic [index]. m_StartPos = startpos;}/** Write File **/FileStream fileStream = new FileStream (foldername + ". UPK ", FileMode. create);/** total number of files **/byte [] totaliddata = System. bitConverter. getBytes (m_id); fileStream. write (totaliddata, 0, totaliddata. length); for (int index = 0; index <m_allFileInfoDic.Count; index ++) {/** write ID **/byte [] iddata = System. bitConverter. getBytes (m_allFileInfoDic [index]. m_id); fileStream. write (iddata, 0, iddata. length);/** write StartPos **/byte [] startposdata = System. bitConverter. getBytes (m_allFileInfoDic [index]. m_StartPos); fileStream. write (startposdata, 0, startposdata. length);/** write size **/byte [] sizedata = System. bitConverter. getBytes (m_allFileInfoDic [index]. m_Size); fileStream. write (sizedata, 0, sizedata. length);/** write path **/byte [] pathdata = new byte [256]; byte [] mypathdata = new UTF8Encoding (). getBytes (m_allFileInfoDic [index]. m_Path); for (int I = 0; I <mypathdata. length; I ++) {pathdata [I] = mypathdata [I];} pathdata [mypathdata. length] = 0; fileStream. write (pathdata, 0, pathdata. length);}/** Write File data **/for (int index = 0; index <m_allFileInfoDic.Count; index ++) {fileStream. write (m_allFileInfoDic [index]. m_data, 0, m_allFileInfoDic [index]. m_Size);} fileStream. flush (); fileStream. close ();/** reset data **/m_id = 0; m_totalSize = 0; m_allFileInfoDic.Clear () ;}} public class OneFileInfor {public int m_id = 0; public int m_StartPos = 0; public int m_Size = 0; public string m_Path = ""; public byte [] m_data = null ;};}

Bytes -----------------------------------------------------------------------------------

I am a cute split line

Bytes -----------------------------------------------------------------------------------

Easy to understand (# coding #)


Create a configuration file

MyPackRes. ini


Add one folder to be packaged per line:


Let's take a look at our running results.

First, let's take a look at the picture I prepared. TP extraction from the path of Bi.


After packaging, an UPK file is generated.


UPK. This suffix is the default resource file format of the Unreal Engine.


Attached project:

http://download.csdn.net/detail/cp790621656/8393457


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.