u3d 加密资源并缓存加载

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

完。

u3d 加密资源并缓存加载,布布扣,bubuko.com

时间: 2024-08-01 14:21:44

u3d 加密资源并缓存加载的相关文章

u3d外部资源 打包与加载的问题

被坑了一下午,调bug,u3d外部加载资源一会可以,一会不行,始终找不到问题,最后快下班的时候,重新试了一下,原来是资源打包之前的文件名,和之后的加载资源名必须一样 [MenuItem("Custom Editor/Build AssetBundle From Selection Twice")] static void ExportResourceNoTrack() { // Bring up save panel string path = EditorUtility.SaveFi

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

Javascript 异步加载详解 这篇文章很详细的介绍了HTML的页面处理以及资源文件的加载. 本文总结一下浏览器在 javascript 的加载方式. 关键词:异步加载(async loading),延迟加载(lazy loading),延迟执行(lazy execution),async 属性, defer 属性 一.同步加载与异步加载的形式 1. 同步加载 我们平时最常使用的就是这种同步加载形式: <script src="http://yourdomain.com/script.

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

简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人修改补充+demo测试的形式,对expo进行一次大补血!欢迎加入expo兴趣学习交流群:597732981 [之前我写过一些列关于expo和rn入门配置的东i西,大家可以点击这里查看:从零学习rn开发] 相关文章: Expo大作战(一)--什么是expo,如何安装expo clinet和xde,xd

自研模块加载器(四) 模块资源定位-异步加载

资源定位-动态加载 通过resolve方法进行异步解析,完整解析如下图所示: 根据上篇文章startUp.js代码,我们继续完善本章动态加载资源的代码. (function(global) { var startUp = global.startUp = { version: '1.0.1' } var data = {}; // 获取当前模块加载器配置信息 var cache = {}; // 缓存 //模块的生命周期 var status = { FETCHED: 1, SAVED: 2,

自定义泪价值器2——加密class文件 解密加载class文件

public class MyClassLoader extends ClassLoader { private String classDir;//自定义类加载器 所查找的目录 MyClassLoader(String classDir){ this.classDir = classDir; } @[email protected]("deprecation") //findClass的主要作用就是 把class文件读取到内存中 那么涉及两个流,,但是class文件被加密 所以需要先

【Cocos2d-Js基础教学(5)资源打包工具的使用及资源的异步加载处理】

[转载]http://www.cnblogs.com/zisou/p/cocos2dx-js5.html   TexturePacker是纹理资源打包工具,支持Cocos2dx的游戏资源打包. 如果用过的同学可以直接看下面的资源的异步加载处理 首先为什么用TexturePacker? 1,节省图片资源实际大小 2,减少我们游戏中大量资源带来的内存负荷 3,方便游戏对纹理资源的内存管理 游戏开发到后期,我们或许都会遇到的瓶颈就是需要对游戏包进行"瘦身"和内存优化.那么使用TextureP

采用Spring实现在容器启动时把用ConcurrentHashMap实现的并发缓存加载到ServletContext中

1.ConstantDataDTO.java,一定要重写hashcode和equals方法 import java.io.Serializable; import java.util.Date; /** * ConstantDataDTO.java * * @author steveguoshao * @created 2014年07月10日 下午6:15:55 * @version 1.0 */ public class ConstantDataDTO implements Serializa

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

[源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 资源 CustomResource ResourceDictionary 加载外部的 ResourceDictionary 文件 示例1.演示“CustomResource”相关知识点CustomResourceTest.cs /* * 本例是一个自定义 CustomXamlResourceLoader,用于演示 CustomResource 的使用 */ using Windows.UI.Xaml.Resources;

Android批量图片加载经典系列——afinal框架实现图片的异步缓存加载

一.问题描述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存加载(查看系列文章:http://www.cnblogs.com/jerehedu/p/4607599.html#pltpjz),接下来我们再介绍一下afinal 框架的使用. Afinal 是一个android的http框架.sqlite orm 和 ioc 框架.使其更加简单易用,Afinal的宗旨是简洁,快速.约定配置的方式之后,尽量一行代码完成所有事情,代码入侵性小,在三者中比较推荐.在这里我们主要使用