博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
u3d 加密资源并缓存加载
阅读量:7143 次
发布时间:2019-06-29

本文共 3891 字,大约阅读时间需要 12 分钟。

// 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

 

的作用就是下载并缓存资源,要注意后面的版本号参数,如果替换了资源却没有更新版本号,客户端依然会加载缓存中的文件。

www.assetBundle.Load("characters",typeof(TextAsset)) as TextAsset  //characters是加密文件的名字

AssetBundleCreateRequest acr = AssetBundle.CreateFromMemory(decryptedData);           

这句是官网最坑爹的,AssetBundle.CreateFromMemory明明返回的是AssetBundleCreateRequest官网却写得是AssetBundle,而且AssetBundleCreateRequest是一个异步加载,必须用协程的方式加载官网也没有提到。跟多兄弟就倒在了这里

完。

转载地址:http://xsgrl.baihongyu.com/

你可能感兴趣的文章
LaTeX的简单使用方法
查看>>
第二十四章:页面导航(四)
查看>>
数字对讲系统开发札记(前端linux c 后端 c#)
查看>>
阿里在使用一种更灵活的软件集成发布模式
查看>>
SpringBoot使用Nacos配置中心
查看>>
SOP 1.2.0 发布,开放平台解决方案项目
查看>>
Element 2.6.3 发布,基于 Vue 2.0 的桌面端组件库
查看>>
丰田研发智能汽车FV2,可利用肢体进行操控
查看>>
基于kubeadm的kubernetes高可用集群部署
查看>>
定位「数字化助手」,腾讯想用服务创新助力产业智慧升级
查看>>
golang之sync.Mutex互斥锁源码分析
查看>>
SAP增强的PA教材内容
查看>>
jQuery系列 第八章 jQuery框架Ajax模块
查看>>
C#使用Xamarin开发可移植移动应用(3.Xamarin.Views控件)附源码
查看>>
Java 模拟基于UDP的Socket通信
查看>>
有关 Windows Lite 的一切,只为对抗 Chrome OS?
查看>>
Android 自定义控件之SlidingMenuVertical顶部悬浮(垂直折叠抽屉,有滑动渐变回调,可自行添加渐变动画)...
查看>>
NG-ZORRO 7.0.1 发布,Ant Design 的 Angular 实现
查看>>
Django 2.0 model on_delete错误小记
查看>>
ffmpeg中的sws_scale算法性能测试
查看>>