LZMA C# SDK 子线程压缩与解压缩 Unity3d实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了LZMA C# SDK 子线程压缩与解压缩 Unity3d实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3861字,纯文字阅读大概需要6分钟。
内容图文
参考雨松的LZMA SDK使用方法:
http://www.xuanyusong.com/archives/3095
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
计划在项目中使用 不压缩的Assetbundle ,所以需要对Assetbundle 进行手动压缩打包,因为之前有对 十万个冷笑话的打包分析,所以这次坚定选择 LZMA压缩算法来压缩Assetbundle。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Note:
因为暂时不知道LZMA如何压缩与解压文件夹,所以目前是计划对文件夹进行UPK打包成UPK文件,然后LZMA压缩。解压的话先解压出来UPK文件,然后读取UPK文件来释放出源文件。
LZMA 官网下载SDK
http://www.7-zip.org/sdk.html
下载过来后直接把C#里面的7zip拖到Unity中,这时Setting.cs 会报错,删除它。
Note:
本文示例中的 LZMA SDK源代码被我修改了。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
代码如下:
using UnityEngine; using System.Collections; using SevenZip; using System.IO; using UnityEditor; using System; using System.Threading; using UnityEngine.UI; public class NewBehaviourScript : MonoBehaviour { [SerializeField] Slider m_Slider; CodeProgress m_CodeProgress=null;//进度; Thread m_CompressThread=null; //压缩线程; Thread m_DecompressThread=null; //解压缩线程; string ApplicationdataPath=string.Empty; private float m_Percent=0f; // Use this for initialization void Start () { ApplicationdataPath=Application.dataPath; m_CodeProgress=new CodeProgress(SetProgressPercent); } void Update() { if(m_Percent>0f) { m_Slider.value=m_Percent; if(m_Percent==1f) { Debug.Log("m_Percent==1f"); m_Percent=0f; AssetDatabase.Refresh(); } } } void OnGUI() { if(GUI.Button(new Rect(100,100,100,100),"Compress")) { m_Slider.value=0f; m_CompressThread=new Thread(new ThreadStart(TestCompress)); m_CompressThread.Start(); } if(GUI.Button(new Rect(300,100,100,100),"DeCompress")) { m_Slider.value=0f; m_DecompressThread=new Thread(new ThreadStart(TestDeCompress)); m_DecompressThread.Start(); } } void TestCompress() { try { Compress(ApplicationdataPath+"/lib.UPK",ApplicationdataPath+"/lib.ZUPK"); } catch(Exception ex) { Debug.Log(ex); } } void TestDeCompress() { try { DeCompress(ApplicationdataPath+"/lib.ZUPK",ApplicationdataPath+"/lib1.UPK"); } catch(Exception ex) { Debug.Log(ex); } } void SetProgressPercent(Int64 fileSize,Int64 processSize) { m_Percent=(float)processSize/fileSize; } void Compress(string inpath,string outpath) { SevenZip.Compression.LZMA.Encoder encoder=new SevenZip.Compression.LZMA.Encoder(); FileStream inputFS=new FileStream(inpath,FileMode.Open); FileStream outputFS=new FileStream(outpath,FileMode.Create); encoder.WriteCoderProperties(outputFS); outputFS.Write(System.BitConverter.GetBytes(inputFS.Length),0,8); encoder.Code(inputFS,outputFS,inputFS.Length,-1,m_CodeProgress); outputFS.Flush(); outputFS.Close(); inputFS.Close(); } void DeCompress(string inpath,string outpath) { SevenZip.Compression.LZMA.Decoder decoder=new SevenZip.Compression.LZMA.Decoder(); FileStream inputFS=new FileStream(inpath,FileMode.Open); FileStream outputFS=new FileStream(outpath,FileMode.Create); int propertiesSize=SevenZip.Compression.LZMA.Encoder.kPropSize; byte[] properties=new byte[propertiesSize]; inputFS.Read(properties,0,properties.Length); byte[] fileLengthBytes=new byte[8]; inputFS.Read(fileLengthBytes,0,8); long fileLength=System.BitConverter.ToInt64(fileLengthBytes,0); decoder.SetDecoderProperties(properties); decoder.Code(inputFS,outputFS,inputFS.Length,fileLength,m_CodeProgress); outputFS.Flush(); outputFS.Close(); inputFS.Close(); } } class CodeProgress:ICodeProgress { public delegate void ProgressDelegate(Int64 fileSize,Int64 processSize); public ProgressDelegate m_ProgressDelegate=null; public CodeProgress(ProgressDelegate del) { m_ProgressDelegate=del; } public void SetProgress(Int64 inSize, Int64 outSize) { } public void SetProgressPercent(Int64 fileSize,Int64 processSize) { m_ProgressDelegate(fileSize,processSize); } }转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
示例工程下载:
百度网盘
链接: http://pan.baidu.com/s/1jGlEDPO 密码: wure
CSDN下载
关于UPK打包
Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一)
原文:http://blog.csdn.net/huutu/article/details/46587303
内容总结
以上是互联网集市为您收集整理的LZMA C# SDK 子线程压缩与解压缩 Unity3d实例全部内容,希望文章能够帮你解决LZMA C# SDK 子线程压缩与解压缩 Unity3d实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。