unity3d 资源打包加密 整理
生活随笔
收集整理的這篇文章主要介紹了
unity3d 资源打包加密 整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
資源打包腳本,放到Assets\Editor 文件夾下
using UnityEngine; using System.Collections; using UnityEditor; using System.IO;public class assetPack : Editor {/* [MenuItem("Custom Editor/Build AssetBundle From Selection - Track dependencies")] static void ExportResource2() {// Bring up save panelstring path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");if (path.Length != 0){// Build the resource file from the active selection.Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);Selection.objects = selection;} }* */ /* [MenuItem("Custom Editor/Build AssetBundle Complited to bytes")] static void ExportResource5() {// Bring up save panelstring path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");if (path.Length != 0){// Build the resource file from the active selection.Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);Selection.objects = selection;FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);byte[] buff = new byte[fs.Length];fs.Read(buff, 0, (int)fs.Length);string password = "shanghaichaolan";packXor(buff,buff.Length,password);Debug.Log("filelength:"+ buff.Length);fs.Close();File.Delete(path);string BinPath = path.Substring(0, path.LastIndexOf('.')) + ".bytes";FileStream cfs = new FileStream(BinPath,FileMode.Create);cfs.Write(buff, 0, buff.Length);Debug.Log("filelength:" + buff.Length);buff = null;cfs.Close();} }*/[MenuItem("Custom Editor/Save Scene2")] static void ExportResource() {// Bring up save panelstring path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");if (path.Length != 0){// Build the resource file from the active selection.Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path,BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);Selection.objects = selection;} }[MenuItem("Custom Editor/Make unity3d file to bytes file")] static void ExportResourceNoTrackSS() {string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");if (path.Length != 0){FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);byte[] buff = new byte[fs.Length];fs.Read(buff, 0, (int)fs.Length);string password = "shanghaichaolan";packXor(buff, buff.Length, password);Debug.Log("filelength:" + buff.Length);fs.Close();File.Delete(path);string BinPath = path.Substring(0, path.LastIndexOf('.')) + ".bytes";FileStream cfs = new FileStream(BinPath, FileMode.Create);cfs.Write(buff, 0, buff.Length);Debug.Log("filelength:" + buff.Length);buff = null;cfs.Close();} }static void packXor(byte[] _data, int _len, string _pstr) {int length = _len;int strCount = 0;for (int i = 0; i < length; ++i){if (strCount >= _pstr.Length)strCount = 0;_data[i] ^= (byte)_pstr[strCount++];}}}?菜單上就會出現兩個, 把要打包的資源做成Prefab,選中資源,然后菜單Custom Editor/Save Scene2? 輸入名字
新生成的文件,再選中新生成的文件,點擊菜單Custom Editor/Make unity3d file to bytes file?? 輸入名字
又生成了一個文件,再點擊這個文件,菜單Custom Editor/Save Scene2? ,這樣就打包加密好了
using UnityEngine; using System.Collections;public class loadnew : MonoBehaviour {public string filename;private string BundleURL;private string AssetName;void Start(){//StartCoroutine(loadScenee());StartCoroutine(LoadResource());}IEnumerator loadScenee(){string path;path = "file://" + Application.dataPath + "/" + filename + ".unity3d";Debug.Log(path);WWW www = new WWW(path);yield return www;AssetBundle bundle = www.assetBundle;//GameObject go = bundle.Load("gCube",typeof(GameObject)) as GameObject;GameObject ObjScene = Instantiate(www.assetBundle.mainAsset) as GameObject;bundle.Unload(false);}IEnumerator LoadResource(){BundleURL = "file://" + Application.dataPath + "/" + filename + ".unity3d";Debug.Log("path:" + BundleURL);WWW m_Download = new WWW(BundleURL);yield return m_Download;if (m_Download.error != null){// Debug.LogError(m_Download.error);Debug.LogError("Warning errow: " + "NewScene");yield break;}TextAsset txt = m_Download.assetBundle.Load(filename, typeof(TextAsset)) as TextAsset;byte[] data = txt.bytes;byte[] decryptedData = Decryption(data);Debug.Log("decryptedData length:" + decryptedData.Length);StartCoroutine(LoadBundle(decryptedData));}IEnumerator LoadBundle(byte[] decryptedData){AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);yield return acr;AssetBundle bundle = acr.assetBundle;Instantiate(bundle.mainAsset);}byte[] Decryption(byte[] data){byte[] tmp = new byte[data.Length];for (int i = 0; i < data.Length; i++){tmp[i] = data[i];}// shanghaichaolanstring password ="shanghaichaolan";packXor(tmp,tmp.Length,password);return tmp;}static void packXor(byte[] _data, int _len, string _pstr){int length = _len;int strCount = 0;for (int i = 0; i < length; ++i){if (strCount >= _pstr.Length)strCount = 0;_data[i] ^= (byte)_pstr[strCount++];}}void update(){} }?加載加密和不加密的資源
原文地址:http://www.cnblogs.com/dragon2012/p/4087548.html
總結
以上是生活随笔為你收集整理的unity3d 资源打包加密 整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 创建日程到期提醒_日程管理工具
- 下一篇: String类的一些常见的获取方法(5)