Unity3d 遊戲資源打包加密(圖片/XML/TXT等) C#編碼 (一),

來源:互聯網
上載者:User

Unity3d 遊戲資源打包加密(圖片/XML/TXT等) C#編碼 (一),

本文只是講述一下過程,採用很簡單的打包加密方法,至於需要什麼樣的加密結果,請大家按照需求去修改,位元組位移、前後顛倒加演算法都可以,不過一般無需這麼複雜,而且太複雜的加密對於極其追求運行效率的遊戲來說,也是一重負擔。


對於Unity,雖然Unity自身會進行壓縮加密,但是其解密演算法在網上隨處可見,如果自己覺得遊戲裡面的資料具有保密性質,請對其進行自行加密。


打包加密的原理:

1、大家都知道檔案都是由位元組組成的。

2、一張圖片之所以看起來很漂亮,是因為其資料按照一定順序排列。

漂亮的劍靈妹子


我們可以用一個文字編輯器將其開啟。

是亂碼,不然你還想看到什麼呢?


3、如果我們把圖片資料打亂,或者在前面加一些很亂的資料,會怎麼樣呢?


嗯,圖片不顯示了。

這很容易理解,就像你看片的時候被打了馬賽克嘛。。

仔細想想,為什麼打馬賽克?

不就是為了保密嘛。。。


好的,上面我們就起到了保密-加密功能。


4、我又找來一張圖片……萌萌噠的。。



同樣用一個文字編輯器開啟這個圖片。

複製所有的內容到第一個圖片檔案後面。

結果會怎麼樣?

兩個圖片會拼在一起嗎?



真不幸……

不過這是符合我的主題的。加密嘛,就是要讓人看不出來。


原理就講到這裡,下面就是代碼了。

代碼不是針對單個檔案,而是對多個檔案夾進行打包加密。

加密方法僅僅是多個檔案打包。


-----------------------------------------------------------------------------------

我是可愛的分割線

-----------------------------------------------------------------------------------


用到的知識點:

1、讀寫檔案

2、對檔案夾、檔案的操作,擷取所有的檔案


-----------------------------------------------------------------------------------

我是可愛的分割線

-----------------------------------------------------------------------------------

下面是主要的代碼:


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_sizeList = 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>();            /**  讀取設定檔  **/            Helper.Log("需要打包的檔案夾:");            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();            /**  遍曆打包檔案夾 **/            for (int index = 0; index < folderToPackList.Count;index++ )            {                PackFolder(folderToPackList[index]);            }            Helper.Log("打包完畢!");            Helper.Pause();        }        /** 遍曆檔案夾擷取所有檔案資訊 **/        private static void TraverseFolder(string foldername)        {            Helper.Log("遍曆檔案夾 " + foldername);            /** 讀取檔案夾下面所有檔案的資訊 **/            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 + " 檔案大小: " + filesize);                OneFileInfor info = new OneFileInfor();                info.m_id = m_id;                info.m_Size = filesize;                info.m_Path = filename;                                /**  讀取這個檔案  **/                FileStream fileStreamRead = new FileStream(fileinfo.FullName, FileMode.Open, FileAccess.Read);                if (fileStreamRead == null)                {                    Helper.Log("讀取檔案失敗 : "+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;            }        }        /**  打包一個檔案夾  **/        private static void PackFolder(string foldername)        {            TraverseFolder(foldername);            Helper.Log("檔案數量 : " + m_id);            Helper.Log("檔案總大小 : " + m_totalSize);            /**  更新檔案在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;//上一個檔案的開始+檔案大小;                }                m_allFileInfoDic[index].m_StartPos = startpos;            }            /**  寫檔案  **/            FileStream fileStream = new FileStream(foldername + ".UPK", FileMode.Create);            /**  檔案總數量  **/            byte[] totaliddata=System.BitConverter.GetBytes(m_id);            fileStream.Write(totaliddata, 0, totaliddata.Length);            for (int index = 0; index < m_allFileInfoDic.Count;index++ )            {                /** 寫入ID **/                byte[] iddata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_id);                fileStream.Write(iddata, 0, iddata.Length);                /**  寫入StartPos  **/                byte[] startposdata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_StartPos);                fileStream.Write(startposdata, 0, startposdata.Length);                /**  寫入size  **/                byte[] sizedata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_Size);                fileStream.Write(sizedata, 0, sizedata.Length);                /**  寫入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);                            }            /**  寫入檔案資料  **/            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();            /** 重設資料 **/            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;    };}

-----------------------------------------------------------------------------------

我是可愛的分割線

-----------------------------------------------------------------------------------

代碼淺顯易懂 ( # ▽ # )


首先建立一個設定檔

MyPackRes.ini


裡面添加需要打包的檔案夾 每行一個:


看看我們運行結果。

首先看看我準備的圖。從碧之軌跡中TP抽取的哦。


打包結束後,會產生UPK檔案。


UPK,這個尾碼是虛幻引擎的預設資源檔格式哦。


最後附上工程:

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.