Unity Addressable 实现Build时自定义剔除资源组

核心原理

重写BuildScriptBase的ProcessGroup,让其在需要剔除的时候返回string.Empty或null即可

代码实现

先写一个Schema来给需要管理的Group上标记

csharp 复制代码
using UnityEditor.AddressableAssets.Settings;

public class BuildExclusionSchema : AddressableAssetGroupSchema
{
    public enum FilterType { 
        Normal,
        TestOnly,
        ReleaseOnly,
    }
    public FilterType filterType = FilterType.Normal;
    public bool ShouldExclude() { 
        if(filterType == FilterType.Normal)return false;
        if (filterType == FilterType.TestOnly) return !CommonData.isTest;
        if (filterType == FilterType.ReleaseOnly) return CommonData.isTest;
        return false;
    }
}

随便写的,具体自己的项目里想怎么改怎么改

然后是核心,自定义一个AA的BuildMode

这里直接继承默认的BuildScriptPackedMode

csharp 复制代码
using UnityEditor.AddressableAssets.Build.DataBuilders;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
[CreateAssetMenu(fileName = "BuildScriptPackedFilterMode.asset", menuName = "Addressables/Custom Build/Filter Build Script")]
public class BuildScriptPackedFilterMode : BuildScriptPackedMode {
    public override string Name => "Filter Build Script";
    protected override string ProcessGroup(AddressableAssetGroup assetGroup, AddressableAssetsBuildContext aaContext) {
        var customFilter = assetGroup.GetSchema<BuildExclusionSchema>();
        if (customFilter != null && customFilter.ShouldExclude()) {
            Debug.Log($"[AA Filter Build] 剔除资源组: {assetGroup.name}");
            return null;
        }
        else { 
            return base.ProcessGroup(assetGroup, aaContext);
        }
    }
}

记得重写Name属性,在各种面板上显示时都要用的

准备工作

先给对应的Group添加对应的Schema

选择对应的模式

然后创建刚刚自定义的Build Script的asset资源并加进AA的Setting里

这样,就可以在AA的Build栏下看见我们自定义的BuildScript了

点击执行

注意事项

build会触发recompile所以控制台里的 clear on compile要是勾选了,对应的log就会被清掉导致看不见。

AA有很多个自带的Build Script,而且Build游戏和使用Update a Previous Build都会执行Build Script,如何确保它执行的是我们想要的Script呢?

首先,AA有一个active Index,保存了上次手动执行的Build Script的下标,在debug窗口就能看见
这是我执行了Filter Build,所以下标是3,也就是最后我加进去的那个

那么很简单了,要么你把自带的都移除出Builders,只剩一个就好了,要么别乱点其他的Build,完事

相关推荐
五仁烧饼2 小时前
Unity性能优化系列内存篇 - 移动端内存优化
unity·性能优化·游戏引擎
WiChP2 天前
【V0.1B15】从零开始的2D游戏引擎开发之路
游戏引擎
zhchyun20083 天前
# 【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(06)
ui·unity·游戏引擎
Behaviour3 天前
Unity游戏HybridCLR 热更新接入实战
游戏·unity·游戏引擎
LONGZETECH4 天前
新能源汽车动力电池实训教学痛点与虚拟仿真技术解决方案
c语言·3d·unity·架构·汽车·汽车教学软件
王维志4 天前
UiSplineRenderer
unity·游戏引擎
k4m7v2pz4 天前
Godot 4 仿 agar.io:相机缩放被 max_zoom 卡死,窗口越大球越小的根因与修复
游戏引擎·godot·相机·缩放·clamp·camera2d
FairGuard手游加固4 天前
iOS 游戏加固技术解析:代码混淆、资源加密、内存保护与重签名检测
安全·游戏·macos·unity·ios·objective-c
k4m7v2pz5 天前
Godot 4 窗口拉伸黑边与变形:stretch 配置全解析
数码相机·游戏引擎·godot·2d游戏·黑边·stretch·画面比例
UWA5 天前
GPM 2.0 WebGL 监测能力扩容|覆盖 Unity/Cocos/LayaAir 多引擎微信 & 抖音小游戏
unity·微信·性能优化·小游戏·webgl