http://www.haogongju.net/art/1931680
首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止依然没有更新正确的示例代码。
// 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 |
public class ExportAssetBundles { |
[MenuItem("Assets/Build AssetBundle Binary file From Selection - Track dependencies ")] |
static void ExportResource () { |
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d"); |
// 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); |
string BinPath = path.Substring(0,path.LastIndexOf('.'))+".bytes"; |
// FileStream cfs = new FileStream(BinPath,FileMode.Create); |
cfs.Write(buff,0,buff.Length); |
// 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 () { |
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d"); |
// 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 加密资源并缓存加载
原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...
- u3d 加密资源并缓存加载
// C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...
- Unity3d Web3d资源的动态加载
Unity3d Web3d资源的动态加载 @灰太龙 参考了宣雨松的博客,原文出处http://www.xuanyusong.com/archives/2405,如果涉及到侵权,请通知我! Unity3 ...
- 【Unity3D】Unity3D之 Resources.Load 动态加载资源
[Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...
- UNITY_资源路径与加载外部文件
UNITY_资源路径与加载外部文件 https://www.tuicool.com/articles/qMNnmm6https://blog.csdn.net/appppppen/article/de ...
- HTML页面处理以及资源文件的加载
Javascript 异步加载详解 这篇文章很详细的介绍了HTML的页面处理以及资源文件的加载. 本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading ...
- Expo大作战(十三)--expo如何自定义状态了statusBar以及expo中如何处理脱机缓存加载 offline support
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- 下载某资源文件并加载其中的所有Prefab到场景中
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...
- 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
[源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...
随机推荐
- day06_01 上节回顾
1.0 extend 扩展方法及"+"的对比 "+"不会改变数组的内容,而extend会改变数组的内容 2.0 sort扩展sorted() a = [1,2, ...
- winform小知识
1.得到控制台或者winform程序在debug目录下生成的exe路径 建议使用:AppDomain.CurrentDomain.BaseDirectory或者Assembly.GetExecutin ...
- Spring Boot多数据源配置(二)MongoDB
在Spring Boot多数据源配置(一)durid.mysql.jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源.本篇文章讲一下Spring Boot如何配置mongoDB多 ...
- echarts异步加载
echarts体积很大,在移动端使用异步加载是一种提高渲染速度的方法,结合webpack的做法如下: require.ensure([], function(require){ const echar ...
- swiper伸缩侧边菜单栏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Java Nio注意事项
Selector : public abstract class Selector extends Object SelectableChannel 对象的多路复用器. 可通过调用此类的 open ...
- POJ1018 Communication System
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26738 Accepted: 9546 Description We ...
- 结构型设计模式之装饰模式(Decorator)
结构 意图 动态地给一个对象添加一些额外的职责.就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活. 适用性 在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. ...
- VS2013 Sqlite3 操作指令
extern "C"{ #include "sqlite3.h" }; #pragma comment(lib,"sqlite.lib") ...
- cannot load shared object file undefined symbol
cannot load shared object file undefined symbol 场景: 共享库里引用了主程序一个符号,结构编译的时候没问题,运行时用 dlopen 打开共享库报上述错误 ...