# Tinax-Addressables-XLua **Repository Path**: chasing2moro/tinax-addressables-xlua ## Basic Information - **Project Name**: Tinax-Addressables-XLua - **Description**: Tinax+Addressables+XLua - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-12-30 - **Last Updated**: 2022-06-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Tinax-Addressables-XLua ### 相关网站 - TinaX:https://tinax.corala.space/ - Addressables:https://github.com/Unity-Technologies/Addressables-Sample - xLua:https://github.com/Tencent/xLua 里面打飞机的例子就是从 Addressables-Sample 拷贝过来的 #### 介绍 - 使用TinaX开发UI模块 - 使用Addressables开发资源管理模块 - 使用xLua开发热更模块 #### 软件架构 此框架在Unity2019.4.9f1上2次开发 #### 使用说明 1. 工程入口Scene:SampleScene.unity,工程入口C#:AppStartup.cs 2. 详细讲述地址: https://www.jianshu.com/p/628e1b679b8b # 序言 好处 --- 1. ++Flexibility++: Addressables give you the flexibility to adjust where you host your assets. You can install assets with your application or download them on demand. You can change where you access a specific asset at any stage in your project without rewriting any game code. ``` stateDiagram-v2 state if_state <> [*] --> 加载资源 加载资源 --> catalog catalog --> InstalledBuild: 资源路径指向安装包 catalog --> DeviceCache: 资源路径指向本地硬盘 catalog --> RemoteHost: 资源路径指向服务器 RemoteHost --> 可以用来做WebGL游戏 ``` 2. ++Dependency management++: The system automatically loads all dependencies of any assets you load, so that all meshes, shaders, animations, and other assets load before the system returns the content to you. - 用AssetReference引用资源,可以异步加载,不卡顿。(Built-in引用,只能是同步) ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/AssetReference.png) ``` Monobehaviour的脚步中添加一个成员: public AssetReference ar; void Awake(){ //同步加载 GameObject gameObject = ar.InstantiateAsync().WaitForCompletion(); //异步加载 var op = ar.InstantiateAsync(); op.Completed += OnCompleted } ``` 注意这句话:Unity does not automatically load the asset assigned to the ==AssetReference==, so you have more control over when to load and unload it.就是说:一个界面,我引用了一个依赖资源(`升级特效预设`),但是玩家一直不点击升级,这个依赖资源(`升级特效预设`)会一直躺在硬盘,不加载它。 限定资源引用资源: According to base AssetReference type, Addressables provides a few more specialized types, such as AssetReferenceGameObject and AssetReferenceTexture. You can use these specialized subclasses to eliminate the possiblity of assigning the wrong type of asset to an AssetReference field. In addition, you can use the AssetReferenceUILabelRestriction attribute to limit assignment to Assets with specific labels 3. ++Memory management++: The system unloads assets as well as loads them, counting references automatically and providing a robust profiler to help you spot potential memory problems. Addressables资源引用检测泄露,不用打AB就可以直接检测内存泄漏 - Use the Asset database ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/MemoryLeakNoBundle.jpg) - Use existing build ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/MemoryLeakBundle.jpg) 检测泄露窗口,两种运行模式的区别只有:后者看到资源引用关系,其他都没区别 4. ++Content packing++: Because the system maps and understands complex dependency chains, it package AssetBundles efficiently, even when you move or rename assets. You can prepare assets for both local and remote deployment, to support downloadable content and reduced application size. ``` erDiagram Project ||--o{ Assets : Git Assets ||--|{ AddressableAssetsData : SubFolder AddressableAssetsData ||--|{ addressables_content_state_bin : SubFolder Project }|..|{ Library : CacheBundleFolder Library ||--|{ com_unity_addressables : SubFolder com_unity_addressables ||--|{ AddressablesBundle : SubFolder ``` 不用提交`AddressablesBundle`(也无法提交),只需要提交上面的`addressables_content_state.bin`文件,别人的机器就可以打热更包了。 使用Addressables注意的3个地方: --- 1. You only need to make sure that anytime you explicitly load an asset, you release it when your application no longer need it 2. You only need to make sure that anytime you explicitly load an asset, you release it when your application no longer need it 3. You only need to make sure that anytime you explicitly load an asset, you release it when your application no longer need it # 1. 安装 Window/Package Manager 安装 Addressables # 2. 使用 ## 2.1 资源打标签![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/%E6%89%93%E6%A0%87%E7%AD%BE.jpg) ## 2.2 加载资源 ### 2.2.1 实例化预设(GameObject) ``` //同步加载 public static GameObject CreateInstanceSync(string key) { //Addressables.Instantiate(key) 已经弃用 GameObject gameObject = Addressables.InstantiateAsync(key).WaitForCompletion(); return gameObject; } //异步加载:不用写回调函数 public static async void CreateInstanceAsync(string key, Action callBack) { var gameObject = await Addressables.InstantiateAsync(key).Task; callBack(gameObject);//不用写回调函数 } //异步加载:写回调函数 public static void CreateInstanceAsync(string key, Action callBack) { var op = Addressables.InstantiateAsync(key); Action> action = null; //写回调函数 action = (AsyncOperationHandle aop) => { aop.Completed -= action; callBack(aop.Result); }; op.Completed += action; } ``` 销毁预设(GameObject):挂一个MonoBehaviour脚本,实现如下方法来销毁(这样做是防止GameObject被直接拷贝实例化,销毁失败) ``` public void Destroy() { //Returns true if the instance was successfully released. var ret = Addressables.ReleaseInstance(this.gameObject)//如果没“成对”调用此函数,跳转场景是会被释放的 if (!ret) GameObject.Destroy(this.gameObject); } ``` ### 2.2.2 加载特定Asset`(Texture/Audio/Material...)` ``` //T as Texture/Audio/Material... //同步加载 //Addressables.LoadAsset(key); 已经弃用 private static async void LoadAssetInternal(string key, Action callBack) { var op = await Addressables.LoadAssetAsync(key).Task; callBack(op); } //异步加载:不用写回调函数 public static async void LoadAssetAsync(string key, Action callBack) { var op = await Addressables.LoadAssetAsync(key).Task; callBack(op); } //异步加载:写回调函数 public static void LoadAssetAsync(string key, Action callBack) { var op = Addressables.LoadAssetAsync(key); Action> action = null; action = (AsyncOperationHandle aop) => { aop.Completed -= action; callBack(aop.Result); }; op.Completed += action; } ``` 销毁特定Asset ``` //T as Texture/Audio/Material... T asset = ? Addressables.Release(asset);//加载完,可以马上释放。只要Unity其他地方引用到asset,就不会被销毁 //如果没“成对”调用此函数,跳转场景是不会被释放的 ``` ### 2.2.3 加载场景 (略) # 3. 进阶 ## address和label的组合使用 - 加载函数接口传key的地方,都支持传address和label ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/KeyEqualLabel.jpg) - address和label不强制唯一,如果不唯一时,就返回第一个资源(LoadAssetAsync)或多个资源(LoadAsset==s==Async) - address和label结合使用 - Asset1=> address: "plate_armor_rusty", label: "hd" - Asset2=> address: "plate_armor_rusty", label: "sd" - Asset3=> address: "plate_sword_rusty", label: "hd" - Asset4=> address: "plate_sword_rusty", label: "sd" ``` Addressables.LoadAssetsAsync( new List { "plate_armor_rusty", "sd"}, (sprite)=> { }, Addressables.MergeMode.Intersection ); //Addressables.MergeMode.Intersection 加载"plate_armor_rusty", "sd"交集的资源,来达到一个常用的用途: 低端手机用底模"sd"的盔甲"plate_armor_rusty" MergeMode: None Use to indicate that no merge should occur. The first set of results will be used. UseFirst Use to indicate that the merge should take the first set of results. Union Use to indicate that the merge should take the union of the results. Intersection Use to indicate that the merge should take the intersection of the results. ``` --- - 组打包的资源加载慢,可以改成单个打包:组里资源加载互不干扰, 直接修改配置,不用改一行代码。 ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/Group2Single.png) - 运行模式 - Use the Asset database: Addressables loads Assets directly from the Asset database. This option typically provides the fastest iteration speed if you are making both code and Asset changes, but also least resembles a production build. - Simulate groups: Addressables loads Assets while simulating groups. This option is helpful if you are working on organizing and optimizing your Addressables groups themselves. It provides Addressables events without requiring a full content rebuild after every change. - Use existing build: Addressables loads content from your last content build. This option most resembles a production build and can provide fast iteration turnaround if you aren't changing Assets. ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/OpenGroup.png) ![image](https://gitee.com/chasing2moro/tinax-addressables-xlua/raw/master/Doc/PlayMode.png) - “不可热更”InAppRes的资源放到一个不可热更的组,通用同套API加载 - 热更打补丁 - 依赖Dependence - Lua扩展 - 修改配置就决定本地资源还是网络资源 - 版本管理(Editor静态变量、动态变量) - Addressables资源释放机制 - Addressables错误检测 - 版本管理 [热更文章1](https://blog.csdn.net/qq_33805569/article/details/122685599) [热更文章2](https://blog.csdn.net/qq_33805569/article/details/123035205)