通过脚本更改Unity Addressable的远程地址

一、使用场景

一般需要开发和发布两个环境,在开发地址上测试完成后,需要切换成正式地址,这时只需要更改远程地址,其他设置是不需要变更的。

二、配置文件

在这里插入图片描述
在这里插入图片描述
在profile中创建新的profile,如图中1的Develop和Release,然后在2中填写不同的地址

三、通过脚本切换地址

关键代码,切换profile,达到更换地址的目的

   static void setProfile(string profile)
    {
        string profileId = settings.profileSettings.GetProfileId(profile);
        if (String.IsNullOrEmpty(profileId))
            Debug.LogWarning($"Couldn't find a profile named, {profile}, " +
                             $"using current profile instead.");
        else
            settings.activeProfileId = profileId;
    }

四、完整打包脚本

#if UNITY_EDITOR

using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Settings;
using System;
using UnityEngine;

internal class BuildLauncher
{
    public static string build_script
        = "Assets/AddressableAssetsData/DataBuilders/BuildScriptPackedMode.asset";

    public static string settings_asset
        = "Assets/AddressableAssetsData/AddressableAssetSettings.asset";

    public static string profile_name = "Default";
    private static AddressableAssetSettings settings;


    static void getSettingsObject(string settingsAsset)
    {
        // This step is optional, you can also use the default settings:
        //settings = AddressableAssetSettingsDefaultObject.Settings;

        settings
            = AssetDatabase.LoadAssetAtPath<ScriptableObject>(settingsAsset)
                as AddressableAssetSettings;

        if (settings == null)
            Debug.LogError($"{settingsAsset} couldn't be found or isn't " +
                           $"a settings object.");
    }

    static void setProfile(string profile)
    {
        string profileId = settings.profileSettings.GetProfileId(profile);
        if (String.IsNullOrEmpty(profileId))
            Debug.LogWarning($"Couldn't find a profile named, {profile}, " +
                             $"using current profile instead.");
        else
            settings.activeProfileId = profileId;
    }



    static void setBuilder(IDataBuilder builder)
    {
        int index = settings.DataBuilders.IndexOf((ScriptableObject)builder);

        if (index > 0)
            settings.ActivePlayerDataBuilderIndex = index;
        else
            Debug.LogWarning($"{builder} must be added to the " +
                             $"DataBuilders list before it can be made " +
                             $"active. Using last run builder instead.");
    }



    static bool buildAddressableContent()
    {
        AddressableAssetSettings
            .BuildPlayerContent(out AddressablesPlayerBuildResult result);
        bool success = string.IsNullOrEmpty(result.Error);

        if (!success)
        {
            Debug.LogError("Addressables build error encountered: " + result.Error);
        }

        return success;
    }


    [MenuItem("Window/Asset Management/Addressables/Build Addressables only")]
    public static bool BuildAddressables(string profileName = "Default")
    {
        getSettingsObject(settings_asset);
        setProfile(profileName);
        IDataBuilder builderScript
            = AssetDatabase.LoadAssetAtPath<ScriptableObject>(build_script) as IDataBuilder;

        if (builderScript == null)
        {
            Debug.LogError(build_script + " couldn't be found or isn't a build script.");
            return false;
        }

        setBuilder(builderScript);

        return buildAddressableContent();
    }

    [MenuItem("Window/Asset Management/Addressables/Build Addressables and Player")]
    public static void BuildAddressablesAndPlayer(string environment)
    {
        string profileName = "Default";
        bool contentBuildSucceeded = false;
        if (environment == "develop")
        {
            profileName = "Develop";
        }
        else if (environment == "release")
        {
            profileName = "Release";
        }

        //if (string.IsNullOrEmpty(profileName))
        //{
        //    contentBuildSucceeded = BuildAddressables();
        //}
        //else
        //{
        //    contentBuildSucceeded = BuildAddressables(profileName);
        //}

        getSettingsObject(settings_asset);
        setProfile(profileName);


        //if (contentBuildSucceeded)
        //{
            var options = new BuildPlayerOptions();
            BuildPlayerOptions playerSettings
                = BuildPlayerWindow.DefaultBuildMethods.GetBuildPlayerOptions(options);

            BuildPipeline.BuildPlayer(playerSettings);
        //}
    }


    [MenuItem("Tools/Build For Development")]
    public static void BuildForDevelopment()
    {
        BuildAddressablesAndPlayer("develop");
    }


    [MenuItem("Tools/Build For Release")]
    public static void BuildForRelease()
    {
        BuildAddressablesAndPlayer("release");
    }
}


#endif


参考文档: 构建脚本

### 配置 Unity 中的 Addressable Assets Profiles 在 UnityAddressable Assets 系统中,配置 profiles 是管理构建和加载路径的关键部分。为了正确设置这些 profiles,在 `Window` -> `Asset Management` -> `Addressables` -> `Groups` 下找到并编辑现有的 group 或者新建一个 group 后可以访问到 profile 设置选项。 对于具体的路径设定: - **Build Path**: 此字段定义了打包过程中资源应被放置的位置。如果指定了相对路径,则该位置基于项目根目录;绝对路径同样支持。这决定了最终编译出来的文件会被存放在哪里[^2]。 - **Load Path**: 设定此项用于指示运行时从何处加载远程或本地存储的 addressable 资源。这个路径可能是服务器上的 URL 地址或者是应用安装包内的某个子目录。它允许开发者灵活地控制资源下载来源。 此外,值得注意的是初次使用 Addressable 功能前需先创建 `Addressables Settings` 文件,这一操作会自动生成必要的配置结构于项目的 `Assets` 文件夹之下,并以此为基础来进行后续的所有定制化调整[^3]。 ```csharp // 示例代码展示如何通过脚本获取当前使用的Profile对象 using UnityEngine; using UnityEditor.AddressableAssets.Settings; public class ProfileExample : MonoBehaviour { void Start() { var settings = AddressableAssetSettingsDefaultObject.GetOrCreateSingleton(); Debug.Log($"Current BuildPath is {settings.BuildPath}"); Debug.Log($"Current LoadPath is {settings.LoadPath}"); } } ``` #### 关联属性解释 存在一些附加属性可以帮助更好地管理和维护已发布的版本: - **Can/Cannot Change Post Release**: 当某组被打上 “Cannot change post release”的标签后,意味着一旦发布了含有这部分资产的应用程序更新,就不能再轻易修改其中的内容。这样的设计有助于防止意外更改影响已经部署的服务稳定性[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值