u3d 加密资源并缓存加载
2024-10-19 16:37:23
// 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 != ) {
// 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+];
fs.Read(buff,,(int)fs.Length);
buff[buff.Length-] = ;
fs.Close();
File.Delete(path); string BinPath = path.Substring(,path.LastIndexOf('.'))+".bytes";
// FileStream cfs = new FileStream(BinPath,FileMode.Create);
cfs.Write(buff,,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 != ) {
// 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,); 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-];
for(int i=;i<data.Length-;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是一个异步加载,必须用协程的方式加载官网也没有提到。跟多兄弟就倒在了这里
完。
最新文章
- 【翻译】Fluent NHibernate介绍和入门指南
- c++学习--继承与派生
- 如何实现上下左右键盘控制焦点使之落在相邻文本框或下拉框中-Web开发/JavaScript
- C语言动态内存管理
- onbeforeunload、beforeunload
- rub、sass和compass的安装
- (5)UIView常见属性
- CSS3详解:transform、transition
- java将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- 力扣算法题—079单词搜索【DFS】
- Git服务器安装详解及安装遇到问题解决方案
- top命令详解不完整的地方欢迎评论
- mysql学习3:mysql之my.cnf详解
- Nagios 监控windows server Apache 服务
- bootloader研究最后一关(中)
- android 网络广播 类似QQ动态检查网络
- CentOS之文档的压缩与打包
- DocumentType类型
- sqlplus与shell互相传值的几种情况
- Learning to Rank算法介绍:RankSVM 和 IR SVM
热门文章
- selenium(java)处理HTML5的视频播放
- OpenGL 用三角形模拟生成球面
- js 获取单选框和复选框的值和js dom方法给单选框和多选框赋值
- c++ 文件增加#ifndef、#define 和 #endif 语句的意义
- 【C#】自定义容器控件,设置界面控件,支持设计器拖入控件
- EF5+MVC4系列(1) Podwerdesigner15.1设计数据库;PD中间表和EF实体模型设计器生成中间表的区别;EF5.0 表关系插入数据(一对多,多对多)
- [转]ESP8266使用详解
- 令人惊叹的HTML5动画及源码分析下载
- Android Studio 项目中,哪些文件应该忽略而不提交到svn的服务器中?
- lakala proportion轨迹分析代码