unity3d加密资源并缓存加载
来源:互联网 发布:炸金花棋牌游戏源码 编辑:程序博客网 时间:2024/05/23 23:27
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(){WWW 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是一个异步加载,必须用协程的方式加载官网也没有提到。跟多兄弟就倒在了这里
完。
0 0
- unity3d加密资源并缓存加载
- unity3d加密资源并缓存加载
- unity3d 加密资源并缓存加载
- unity3d远程加载资源模型到本地并加载(一)打包资源
- unity3d WWW加载资源
- Unity3D 加载资源
- [unity3d]加载模型资源
- 【Unity3D】 资源加载方式
- Unity3D加载外部资源
- Unity3D-动态加载资源
- unity3d远程加载资源模型到本地并加载(二)第一次加载资源后写到本地后从本地加载。
- unity3d 资源打包加密 整理
- [Unity3d]打包Assetbundle并加载
- [Unity3d]打包Assetbundle并加载
- [Unity3d]打包Assetbundle并加载
- [Unity3d]打包Assetbundle并加载
- [Unity3d]打包Assetbundle并加载
- [Unity3d]打包Assetbundle并加载
- 百灵欧拓O2O移动广告平台
- 日期函数
- AMD OpenCL例子阅读笔记系列之DeviceFission
- jquery 中跳出each循环
- .NET 数据类型之动态类型(Dynamic)
- unity3d加密资源并缓存加载
- android edittext在输入手机号码的时候自动添加分隔符
- android中的style部分属性值介绍
- Android—UI之DatePicker、TimePicker
- LCD LVDS的一些术语定义
- IOS6新特性之下拉刷新<UIRefreshControl>
- 日本研制出用于电动汽车的锂电池高耐热技术
- C# 中的委托和事件
- Coder-Strike 2014 - Finals (online edition, Div. 1)