- AssetBundle 打包管理
- 类对象池资源对象池
- 离线数据及配置表
- 融合使用框架制作简单的UI系统
1. 预备知识
- 程序集
- .dll CSharper.dll Editor.dll
- 自定义程序集 同层级以及子集为同一个程序集
- unity资源加载方式介绍
- 拖在组件上
- Rerources.load
- AssetBundle
- AssetDataBase.LoadAtPath
- c#的xml序列化
- 类转成xml
- 类转成二进制
- 定义:序列化是通过将对象转换为字节流,从而存储对象或将对象传输到内存,数据库或文件的过程。主要用途是保存对象的状态,包括对象的数据,以便能够在需要是重建对象。反向过程称为 反序列化。
- 序列化相关API
///类的序列化 //获取一个xml文件流 FileStream fileStream = new FileStream(Path.Combine(Application.dataPath, "test.xml"), FileMode.Create, FileAccess.ReadWrite, FileAccess.ReadWrite) //获取一个写入流 StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8); //使用XmlSerializer序列化类 XmlSerializer xml = new XmlSerializer(serializeExample.GetType()) //序列化类 xml.Serialize(streamWrite, serializeExample); streamWriter.Close(); fileStream.Close();
- unity asset序列化
AssetBundle基础
- AssetBundle的定义和作用
- AssetBundle是一个压缩包,包含模型、贴图、预制体、声、甚至整个场景,可以在游戏运行的时候被加载
- AssetBunlde自身保存着相互的依赖关系
- 什么是AssetBundle
- AssetBundl在硬盘中是文件,使用时被加载到内存中,操作的是一个AssetBundle对象
- 一部分是包含资源的文件,叫做Assetbundle压缩包。压缩包里有两种文件类型:序列化文件和资源文件。序列化文件比如场景、Prefab等,资源文件比如贴图、模型网格、音频、视频等二进制数据。另一部分是压缩包中包含资源的映射信息。通过代码加载Assetbundle时就需要从这一部分中获取资源对应的位置信息
- AssetBundle使用流程
-
步骤
- 指定资源的AssetBundle属性
- 通用API构建AssetBundle包
- 上传AB包到资源服务器
- 加载AB包和包里面的资源(热更删除旧的资源,使用新的资源)
-
使用代码打包AssetBundle
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
-
从本地读取assetbundle包
AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/player.unity3d"); //从硬盘加载到内存 GameObject playerPrefab = ab.LoadAsset<GameObject("Attack"); Instantiate(playerPrefab);
-
从内存中读取
public static AssetBundleCreateRequest LoadFromMemoryAsync(byte[] binary);
-
从网络或本地读取
public static UnityWebRequest GetAssetBundle(string uri); public static AssetBundle GetContent(UnityWebRequest www);
-
- 加载和卸载AssetBundle资源 AssetBundle的两种加载方式:
- 加载非“对象预设(非GameObject)”方式(例如:贴图、材质、音频等)
- 加载“对象预设(Prefab)”方式
-
创建AssetBundle包
把硬盘或者网络的一个文件读到内存的一个区域,这时候只是个AssetBundle内存镜像数据块,还没有Assets的概念CreateFromFile(); CreateFromMemory(byte[]); //WWW的assetBundle就是内部数据读取完后自动创建了一个assetBundle而已
-
下载AssetBundle资源包
从AssetBundle的内存镜像里读取并创建一个Asset对象,创建Asset对象同时也会分配相应内存用于存放(反序列化)//load an AssetBundle from a fole on disk string filePath = Path.Combine(Application.streamingAssetPath, "AssetLabelsName") AssetBundle ab = AssetBundle.LoadFromFile(filePath);
-
从AssetBundle包中加载指定资源(Object)
Object prefab = ab.LoadAsset<GameObject>("AssetName"); //加载为内存资源 GameObject = Instantiate(prefab); //实例化对象
-
卸载AssetBundle包
AssetBundle.Unload(flase);//是释放AssetBundle文件的内存镜像,不包含Load创建的Asset内存对象。 AssetBundle.Unload(true);//是释放那个AssetBundle文件内存镜像和并销毁所有用Load创建的Asset内存对象。