unity3d加密資源並緩存加載

unity3d加密資源並緩存加載

轉自:http://www.haogongju.net/art/1931680

首先要鄙視下unity3d的文檔編寫人員極度不負責任,到發帖爲止依然沒有更新正確的示例代碼。

// C# Example
// Builds an asset bundle from the selected objects in the project view.
// Once compiled go to "Menu" -> "Assets" and select one of the choices
// to build the Asset Bundle
using UnityEngine;
using UnityEditor;
using System.IO;
public class ExportAssetBundles {
[MenuItem("Assets/Build AssetBundle Binary file From Selection - Track dependencies ")]
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;
FileStream fs = new FileStream(path,FileMode.Open,FileAccess.ReadWrite);
byte[] buff = new byte[fs.Length+1];
fs.Read(buff,0,(int)fs.Length);
buff[buff.Length-1] = 0;
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);
buff =null;
cfs.Close();
//              string AssetsPath = BinPath.Substring(BinPath.IndexOf("Assets"));
//              Object ta = AssetDatabase.LoadAssetAtPath(AssetsPath,typeof(Object));
//              BuildPipeline.BuildAssetBundle(ta, null, path);
}
}
[MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
static void ExportResourceNoTrack () {
// 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.
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
}
}
}

  把打包的文件轉換成了binary文件並多加了一個字節加密。

  當bytes文件生成好後再選中它,使用"Assets/Build AssetBundle From Selection - No dependency tracking"再次打包。

using UnityEngine; 
using System.Collections; 
using System; 
public class WWWLoadTest : MonoBehaviour 
{
public string BundleURL; 
public string AssetName; 
IEnumerator Start() 
{
 	cWWW www =WWW.LoadFromCacheOrDownload(BundleURL,2);
	yield return www; 
	TextAsset txt = www.assetBundle.Load("characters",typeof(TextAsset)) as TextAsset; 
	byte[] data = txt.bytes; byte[] decryptedData = Decryption(data); 
	Debug.LogError("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.Load(AssetName)); 
byte[] Decryption(byte[] data)
	byte[] tmp = new byte[data.Length-1];
	for(int i=0;i<data.Length-1;i++)
	
		tmp[i] = data[i]; 
	
	return tmp; 
}
 

WWW.LoadFromCacheOrDownload的作用就是下載並緩存資源,要注意後面的版本號參數,如果替換了資源卻沒有更新版本號,客戶端依然會加載緩存中的文件。

www.assetBundle.Load("characters",typeof(TextAsset)) as TextAsset  //characters是加密文件的名字

AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);           

這句是官網最坑爹的,AssetBundle.CreateFromMemory明明返回的是AssetBundleCreateRequest官網卻寫得是AssetBundle,而且AssetBundleCreateRequest是一個異步加載,必須用協程的方式加載官網也沒有提到。跟多兄弟就倒在了這裏

完。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章