unity3d 资源打包加密
2016-02-14 15:49
357 查看
资源打包脚本,放到Assets\Editor 文件夹下
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class assetPack : Editor
{
[MenuItem("Custom Editor/Save Scene2")]
static void ExportResource()
{
// Bring up save panel
string 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()
{
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach(Object obj in selection)
{
string path1 = AssetDatabase.GetAssetPath(obj);
FileStream fs = new FileStream(path1, FileMode.Open, FileAccess.ReadWrite);
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length);
string password = "llh";
packXor(buff, buff.Length, password);
Debug.Log("filelength:" + buff.Length);
fs.Close();
string BinPath = path1.Substring(0, path1.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 ,这样就打包加密好了即打包AssetBundle之后加密再重新打包AssetBundle(能否直接加密打包?显然是不行的,加载资源时,LoadBundle会通过解密之后的字节重新创建AssetBundle,所以必须先打包出AssetBundle)
加载打包资源using UnityEngine;
using System.Collections;
public class loadnew : MonoBehaviour
{
public string filename = "My Resource";
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;
GameObject obj = (GameObject)Instantiate(bundle.mainAsset);
//obj.SetActive (true);
}
byte[] Decryption(byte[] data)
{
byte[] tmp = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
tmp[i] = data[i];
}
// shanghaichaolan
string password ="llh";
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()
{
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class assetPack : Editor
{
[MenuItem("Custom Editor/Save Scene2")]
static void ExportResource()
{
// Bring up save panel
string 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()
{
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach(Object obj in selection)
{
string path1 = AssetDatabase.GetAssetPath(obj);
FileStream fs = new FileStream(path1, FileMode.Open, FileAccess.ReadWrite);
byte[] buff = new byte[fs.Length];
fs.Read(buff, 0, (int)fs.Length);
string password = "llh";
packXor(buff, buff.Length, password);
Debug.Log("filelength:" + buff.Length);
fs.Close();
string BinPath = path1.Substring(0, path1.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 ,这样就打包加密好了即打包AssetBundle之后加密再重新打包AssetBundle(能否直接加密打包?显然是不行的,加载资源时,LoadBundle会通过解密之后的字节重新创建AssetBundle,所以必须先打包出AssetBundle)
加载打包资源using UnityEngine;
using System.Collections;
public class loadnew : MonoBehaviour
{
public string filename = "My Resource";
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;
GameObject obj = (GameObject)Instantiate(bundle.mainAsset);
//obj.SetActive (true);
}
byte[] Decryption(byte[] data)
{
byte[] tmp = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
tmp[i] = data[i];
}
// shanghaichaolan
string password ="llh";
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()
{
}
}
相关文章推荐
- Kinect结合Unity3D引擎开发体感游戏(一)
- 自学成才的秘密:115个 web Develop 资源
- C#中使用资源的方法分析
- Shell脚本实现Linux系统和进程资源监控
- Android资源命名规范 分享
- C#使用Dispose模式实现手动对资源的释放
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作
- Unity3d获取系统时间
- .net非托管资源的回收方法
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Unity3D游戏引擎实现在Android中打开WebView的实例
- unity3d调用手机或电脑摄像头
- Unity3d发布IOS9应用时出现中文乱码的解决方法
- Smarty使用自定义资源的方法
- python实现计算资源图标crc值的方法
- 分享一个开源的网络游戏服务器架构—HouHai