原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html

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

完。

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

  1. (转)unity3d加密资源并缓存加载

    http://www.haogongju.net/art/1931680 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止依然没有更新正确的示例代码. view source   pr ...

  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. Asp.net MVC在View里动态捆绑压缩引用的js

    前言 Asp.net MVC 4以上版本多了BundleConfig.RegisterBundles方法,可以把要捆绑的脚本或样式进行捆绑压缩,以减少客户端的请求次数从而提高了客户端的访问速度. 问题 ...

  2. Windows Phone8 中如何引用 SQLite 数据库

    SQLite数据库介绍 1.SQLite是一款轻型的嵌入式数据库,使用C++开发,使用非常广泛 2.SQLite是一款跨平台的数据库,支持Windows.Linux.Android.IOS.Windo ...

  3. FTP+SFTP工具类封装-springmore让开发更简单

    github地址:https://github.com/tangyanbo/springmore FTPUtil 该工具基于org.apache.commons.net.ftp.FTPClient进行 ...

  4. javascript基于原型实现面向对象

    传统的OO语言有类的概念,但js(ES5)却是基于原型实现的面向对象. 原型是?我们创建的每一个函数都会有一个原型(prototype)属性,这个属性是一个指针,指向函数的原型(prototype)对 ...

  5. 教你用netstat-实践案例

    netstat命令的功能是显示网络连接.路由表和网络接口信息,可以让用户得知目前都有哪些网络连接正在运作. 该命令的一般格式为: netstat [选项] 命令中各选项的含义如下: -a 显示所有so ...

  6. OpenCart 最新使用教学视频合集

    OpenCart 是一个很火的开源电商系统,国内越来越多的人开始使用 OpenCart 搭建自己的电商网站.OpenCart 的功能非常强大,当然功能也非常多.这里整理了 OpenCart 最重要的一 ...

  7. css3面试的时候需要记忆的东西

    1.响应式布局: <link rel="stylesheet" href="1.css" media="screen and (min-widt ...

  8. Linq之IQueryable接口与IEnumberable区别

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IEnumerable 包含 ...

  9. AngularJS开发指南16:AngularJS构建大型Web应用详解

    AngularJS是由Google创建的一种JS框架,使用它可以扩展应用程序中的HTML功能,从而在web应用程序中使用HTML声明动态内容.在该团队工作的软件工程师Brian Ford近日撰写了一篇 ...

  10. ECMAScript —— 学习笔记(思维导图版)

    导图