背景:希望运行时可以访问出包参数等出包时注入的信息

核心代码如下

/// <summary>
/// 包体相关配置信息,可将出包参数带入该配置并在运行时访问
/// </summary>
[CreateAssetMenu(fileName = "BuildConfig", menuName = "ScriptableObject/BuildConfig")]
public class BuildConfig : ScriptableObject
{
#if UNITY_EDITOR
    public static readonly string PathEditor = "Assets/Resources/Configs/BuildConfig.asset";
#endif
    public static readonly string PathRuntime = "Configs/BuildConfig";

    /// <summary>
    /// 包体构建类型
    /// </summary>
    [SerializeField]
    public BuildMode buildMode;
}
// 出包时处理
BuildConfig config = AssetDatabase.LoadAssetAtPath<BuildConfig>(BuildConfig.PathEditor);
// 根据出包参数修改
config.buildMode = buildMode;
// SetDirty确保其可以被保存(命令行模式下不加SetDirty无法保存)
EditorUtility.SetDirty(config);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
// 运行时加载
BuiltinBuildConfig = Resources.Load<BuildConfig>(BuildConfig.PathRuntime);
Debug.Log("buildMode " + BuiltinBuildConfig.buildMode);

 

注:ScriptObject可以放在Resources下也可以打成AssetBundle,加载时按不同方式加载即可