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是一个异步加载,必须用协程的方式加载官网也没有提到。跟多兄弟就倒在了这里

完。

(转)unity3d加密资源并缓存加载的更多相关文章

  1. unity3d 加密资源并缓存加载

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...

  2. u3d 加密资源并缓存加载

    // C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...

  3. Unity3d Web3d资源的动态加载

    Unity3d Web3d资源的动态加载 @灰太龙 参考了宣雨松的博客,原文出处http://www.xuanyusong.com/archives/2405,如果涉及到侵权,请通知我! Unity3 ...

  4. 【Unity3D】Unity3D之 Resources.Load 动态加载资源

    [Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...

  5. UNITY_资源路径与加载外部文件

    UNITY_资源路径与加载外部文件 https://www.tuicool.com/articles/qMNnmm6https://blog.csdn.net/appppppen/article/de ...

  6. HTML页面处理以及资源文件的加载

    Javascript 异步加载详解 这篇文章很详细的介绍了HTML的页面处理以及资源文件的加载. 本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading ...

  7. Expo大作战(十三)--expo如何自定义状态了statusBar以及expo中如何处理脱机缓存加载 offline support

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  8. 下载某资源文件并加载其中的所有Prefab到场景中

    using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...

  9. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

随机推荐

  1. hnust 好友互动标识

    问题 A: 好友互动标识 时间限制: 1 Sec  内存限制: 128 MB提交: 897  解决: 122[提交][状态][讨论版] 题目描述       QQ有一个有趣的功能即显示好友互动标识,它 ...

  2. opencv3.1+cmake+mingw5.3+QT5编译

    太不容易了! 想要访问opencv的官网貌似要FQ才行.下载了opencv3.2版本,发现cmake在download opencv_ffmpeg.dll的地方超时了. 于是搜索一番,发现很多编译op ...

  3. # ML学习小笔记—Linear Regression

    Regression Output a scalar Model:a set of function 以Linear model为例 y = b+w * $x_cp$ parameters:b,W f ...

  4. reinterpret_cast and const_cast

    reinterpret_cast reinterpret意为“重新解释” reinterpret_cast是C++中与C风格类型转换最接近的类型转换运算符.它让程序员能够将一种对象类型转换为另一种,不 ...

  5. static_cast AND dynamic_cast

    类型转换是一种机制,让程序员能够暂时或永久性改变编译器对对象的解释.注意,这并不意味着程序员改变了对象本身,而只是改变了对对象的解释. 在很多情况下,类型转换是合理的需求,可解决重要的兼容问题.因此, ...

  6. LACP链路聚合控制协议

    LACP链路聚合控制协议 来源: https://www.cnblogs.com/taosim/articles/4378691.html http://storage.chinabyte.com/6 ...

  7. [poj] 3068 "Shortest" pair of paths || 最小费用最大流

    [原题](http://poj.org/problem?id=3068) 给一个有向带权图,求两条从0-N-1的路径,使它们没有公共点且边权和最小 . //是不是像传纸条啊- 是否可行只要判断最后最大 ...

  8. 【CZY选讲·次大公因数】

    题目描述 给定n个数ai,求sgcd(a1,a1),sgcd(a1,a2),…,sgcd(a1,an). 其中sgcd(x,y)表示x和y的次大公因数.若不存在次大公因数,sgcd(x,y)=-1 ...

  9. zoj 3822 概率dp

    /* 题目大意:一个n*m的棋盘,每天放一个棋子,每行每列至少有一个棋子时结束.求达到每行每列至少有一个棋子的天数的数学期望. */ #include <iostream> #includ ...

  10. 洛谷 P1343 地震逃生

    P1343地震逃生 题目描述 汶川地震发生时,四川**中学正在上课,一看地震发生,老师们立刻带领x名学生逃跑,整个学校可以抽象地看成一个有向图,图中有n个点,m条边.1号点为教室,n号点为安全地带,每 ...