Unity3d给纹理图片加密
如果不想让其他人较轻易的获取包体里的图片,可以通过给图片加密。
步骤:
1.先将图集打包成AssetBundle
2.给图集AssetBundle加密,并拷贝到StreamingAssets文件夹里
3.在游戏中加载AssetBundle,然后解密,读取纹理到内存中
4.调用并显示图片
1.先将图集打包成AssetBundle,这里我使用的是Texturepack这个软件打包出来的纹理图集
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Text;public static class ImageEncryption
{private static string TEMP_ASSETBUNDLE_PATH = Application.dataPath + "/TempAssetBundle";private static string ASSETBUNDLE_NAME = "GameViewTP.assetbundle";//将纹理打包成Assetbundle[MenuItem("Encryption/1.CreateImageAssetBundle")]public static void CreateImageAssetBundle(){List<AssetBundleBuild> builds = new List<AssetBundleBuild>();AssetBundleBuild build1 = new AssetBundleBuild();build1.assetBundleName = ASSETBUNDLE_NAME;build1.assetNames = new string[] { "Assets/Sprites/GameViewTP.png", "Assets/Sprites/GameViewTP.tpsheet" };builds.Add(build1);if (!Directory.Exists(TEMP_ASSETBUNDLE_PATH))Directory.CreateDirectory(TEMP_ASSETBUNDLE_PATH);if (BuildPipeline.BuildAssetBundles(TEMP_ASSETBUNDLE_PATH, builds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.Android)){Debug.Log("资源打包成功");}UnityEditor.AssetDatabase.SaveAssets();UnityEditor.AssetDatabase.Refresh();}}
点击执行
生成如下文件
2.给红包图集AssetBundle加密,并拷贝到StreamingAssets文件夹里
//纹理图集加密,并拷贝到StreamingAssets文件夹里[MenuItem("Encryption/2.EncryptExPackImage")]public static void EncryptExPackImage(){string assetbundle_file_path = Path.Combine(TEMP_ASSETBUNDLE_PATH, ASSETBUNDLE_NAME.ToLower());byte[] bytes = File.ReadAllBytes(assetbundle_file_path);//字节数组加密,这个可以自己网上搜一下相应的加密算法byte[] encryptedBytes = CryptoBase.EncryptEx(bytes);string targetPath = Application.streamingAssetsPath;if (!Directory.Exists(targetPath))Directory.CreateDirectory(targetPath);File.WriteAllBytes(targetPath + "/" + ASSETBUNDLE_NAME.ToLower(), encryptedBytes);UnityEditor.AssetDatabase.SaveAssets();UnityEditor.AssetDatabase.Refresh();}
点击执行
在StreamingAssets生成gameviewtp.assetbundle
3.在游戏中加载AssetBundle,然后解密,读取纹理到内存中
创建一个新的脚本ReadTexture.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;public class ReadTexture : MonoBehaviour
{private bool isTextureLoadFinish;private Dictionary<string, Sprite> m_spriteCatch = new Dictionary<string, Sprite>();public Image image1, image2;// Start is called before the first frame updatevoid Start(){StartCoroutine(LoadPackImage());StartCoroutine(ShowImage());}private IEnumerator LoadPackImage(){string path;
#if UNITY_ANDROID && !UNITY_EDITORpath = Application.streamingAssetsPath + "/gameviewtp.assetbundle";
#elsepath = "file://" + Application.streamingAssetsPath + "/gameviewtp.assetbundle";
#endifvar uwr = UnityWebRequest.Get(path);yield return uwr.SendWebRequest();//字节数组解密算法byte[] decryptedBytes = CryptoBase.DecryptEx(uwr.downloadHandler.data);AssetBundle bundle1 = AssetBundle.LoadFromMemory(decryptedBytes);Sprite[] sprites = bundle1.LoadAllAssets<Sprite>();foreach (Sprite sp in sprites){try{if (!m_spriteCatch.ContainsKey(sp.name))m_spriteCatch.Add(sp.name, sp);elseDebug.LogError("sp.name already loaded " + sp.name);}catch (Exception e){Debug.LogError("sp.name" + e);}}isTextureLoadFinish = true;}private IEnumerator ShowImage(){yield return new WaitUntil(() => { return isTextureLoadFinish; });image1.sprite = m_spriteCatch["flower"];image2.sprite = m_spriteCatch["coinbox"];}}
创建一个场景,并在场景上挂载该脚本,并添加两个Image,把Image拖到脚本上
点击运行:

完结。
Unity3d给纹理图片加密相关推荐
- 【Unity3D】纹理贴图 ( 纹理 Texture 简介 | 为 3D 模型设置纹理贴图 )
文章目录 一.纹理 Texture 简介 二.为 3D 模型设置纹理贴图 一.纹理 Texture 简介 上一篇博客 [Unity3D]材质 Material ( 材质简介 | 创建材质 | 设置材质 ...
- 实现图片打乱_基于混沌Logistic加密算法的图片加密与还原
基于混沌Logistic加密算法的图片加密与还原 摘要 一种基于混沌Logistic加密算法的图片加密与还原的方法,并利用Lena图和Baboon图来验证这种加密算法的加密效果.为了能够体现该算法在图 ...
- 利用opencv读取图片将其作为opengl的纹理图片的实现方法
在opengl中实现三维物体的纹理贴图的第一步就是要读入图片,然后指定该图片为纹理图片.我最早接手的项目中是通过一个专门的读取bmp图片的函数来实现纹理图片的读取的.这个函数有三个缺点: 1.只能读取 ...
- Metal之加载TGA与PNG/JPEG纹理图片
TGA纹理 ① 效果展示 ② 环境准备 视图控制器类:在 viewDidLoad 函数中创建 MTKView 对象.自定义 render 对象,并设置 view 的代理为 render,其流程请参考: ...
- 极验验证码流程-3.图片加密处理 图片移位
终于把图片加密给搞定了,原理是他把图分成了52个部分,然后通过移动来形成新的图片 主要的位置关系看代码 顺便吐槽下ruby,小众语言就是这么不方便,很多库都没有,百度了半天 最后换成了java来写 图 ...
- 8tupian图片加密平台源码 v3.5
介绍: 8tupian图片加密平台采用 php+mysql 的环境,php版本是php5.6或以上. 源码是通过调用八图片平台的API,进行设计开发的.用此源码搭建的网站,用户资金直接进入你自己的接口 ...
- python-PIL生成棋盘纹理图片
续更,因为发现上次那份没有按照要求,所有重新写了这份. PIL生成棋盘纹理图片 from PIL import Imagedef qipan(width, height, color1, color2 ...
- 学习opengl之为立方体六个面贴上不同的纹理图片
效果 相比于立方体自转,这一博客增添了 为立方体的不同面添加不同纹理图片的操作,不再是六个面单一纹理了. 工具 glad.glfw(这个无所谓).glm(这个也主要是为了和glad配合,使用eigen ...
- Html源代码图片解密,通过图片加密、解密文件
[实例简介] [实例截图] [核心代码] using System; using System.Collections.Generic; using System.ComponentModel; us ...
最新文章
- oracle9201怎么安装,Solaris10上安装64位Oracle9201
- R语言可视化dataframe数据、并自定义设置坐标轴各个标签使用不同的色彩
- gitee markdown 笔记_使用Gitee作为Markdown的图床
- mapreduce工作流程_我要进大厂之大数据MapReduce知识点(2)
- MySQL执行计划extra中的using index 和 using where using index 的区别
- 【LeetCode】【HOT】84. 柱状图中最大的矩形(栈)
- Disallowed Key Characters(转)
- python编程教学软件-编程教学平台的python编辑器的开发
- 数据分析师必学第一课:构建完整的指标体系
- 抛开复杂的架构设计,MySQL优化思想基本都在这了
- Fabric 1.0的多机部署
- Rhino显示左边的工具栏
- 乐视盒子UI 官方下载地址
- FlashFXP连接linux服务器(centos7环境)提示连接失败 (Unable to access SFTP sub-system, operation failed.)
- 不能创建对象qmdispatch_按键精灵更新时提示 ActiveX 部件不能创建对象 错误代码 800a01ad_电脑故障...
- BZOJ3894: 文理分科
- 从4G到5G,从物联网到云计算 通信的下一个引爆点在哪里?
- REST ful风格
- linux服務器重啟後自動啟動java項目的腳本配置
- STM32HAL库学习之路(八):MG90S舵机应用