AddressableAssets资源加载总结--使用工具自动创建组

有个需求需要自动创建组,避免手动拖拽,这里写了编辑器工具,借鉴了这篇文章:Unity 之 Addressable Asset System 之用工具创建group - 孤独の巡礼 - 博客园

工具代码如下:

using System.Linq;
using System.IO;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
using System;

public class AddressableGroupSetter : ScriptableObject
{
    static AddressableAssetSettings Settings
    {
        get { return AddressableAssetSettingsDefaultObject.Settings; }
    }


    [MenuItem("AddressableTool/BuildGroups")]
    static void ResetGroups()
    {
        for (int i = 0; i < FindObjectOfType<AddressableAssetLoad>().createGroupPaths.Count; i++)
        {
            ResetGroup<GameObject>(FindObjectOfType<AddressableAssetLoad>().createGroupPaths[i].GroupName,
                                   FindObjectOfType<AddressableAssetLoad>().createGroupPaths[i].GroupPath,
                                   "t:prefab", assetPath =>
            {
                string fileName = Path.GetFileNameWithoutExtension(assetPath);
                string dirPath = Path.GetDirectoryName(assetPath);
                string dirName = Path.GetFileNameWithoutExtension(dirPath);
                return $"{dirName}/{fileName}";
            });
        }
    }

    /// <summary>
    /// 重置某分组
    /// </summary>
    /// <typeparam name="T">资源类型</typeparam>
    /// <param name="groupName">组名</param>
    /// <param name="assetFolder">资源目录</param>
    /// <param name="filter">过滤器:  
    /// <param name="getAddress">通过 asset path 得到地址名</param>
    static void ResetGroup<T>(string groupName, string assetFolder, string filter, Func<string, string> getAddress)
    {
        string[] assets = GetAssets(assetFolder, filter);
        AddressableAssetGroup group = CreateGroup<T>(groupName);
        foreach (var assetPath in assets)
        {
            string address = getAddress(assetPath);
            AddAssetEntry(group, assetPath, address);
        }

        Debug.Log($"Reset group finished, group: {groupName}, asset folder: {assetFolder}, filter: {filter}, count: {assets.Length}");
    }

    // 创建分组
    static AddressableAssetGroup CreateGroup<T>(string groupName)
    {
        AddressableAssetGroup group = Settings.FindGroup(groupName);
        if (group == null)
            group = Settings.CreateGroup(groupName, false, false, false,Settings.DefaultGroup.Schemas, typeof(T));//没有组就新建组,设置默认组的Schemas
        Settings.AddLabel(groupName, false);//添加组标签
        
        return group;
    }

    // 给某分组添加资源
    static AddressableAssetEntry AddAssetEntry(AddressableAssetGroup group, string assetPath, string address)
    {
        string guid = AssetDatabase.AssetPathToGUID(assetPath);
        AddressableAssetEntry entry = group.entries.FirstOrDefault(e => e.guid == guid);
        if (entry == null)
        {
            entry = Settings.CreateOrMoveEntry(guid, group, false, false);
        }

        // entry.address = address;//设置资源地址(完整路径)
        entry.address = Path.GetFileNameWithoutExtension(assetPath);//设置资源地址(自身名字);
        entry.SetLabel(group.Name, true, false, false);//设置资源标签(组名)
        return entry;
    }

    /// <summary>
    /// 获取指定目录的资源
    /// </summary>
    /// <param name="filter">过滤器:
    public static string[] GetAssets(string folder, string filter)
    {
        if (string.IsNullOrEmpty(folder))
            throw new ArgumentException("folder");
        if (string.IsNullOrEmpty(filter))
            throw new ArgumentException("filter");

        folder = folder.TrimEnd('/').TrimEnd('\\');

        if (filter.StartsWith("t:"))
        {
            string[] guids = AssetDatabase.FindAssets(filter, new string[] { folder });
            string[] paths = new string[guids.Length];
            for (int i = 0; i < guids.Length; i++)
                paths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
            return paths;
        }
        else
        {
            throw new InvalidOperationException("Unexpected filter: " + filter);
        }
    }
}

放在Unity的Editor文件夹下,在前面的AddressableAssetLoad里面添加一个组名和路径的累,在Inspector面板手动赋值:

public class AddressableAssetLoad : MonoBehaviour
{

    public List<CreateGroupPath> createGroupPaths = new List<CreateGroupPath>();
}

[Serializable]
public class CreateGroupPath
{
    public string GroupName;
    public string GroupPath;
}

 这个是已经生成好的组,点击这里可以重置重新生成

Demo:https://download.csdn.net/download/pq8888168/84844218

 

 

 

### Unity AssetBundle 工具和实用程序 对于管理与创建 AssetBundles,Unity 提供了一系列官方支持的工具以及社区开发的解决方案。AssetBundles 是一种打包资源的方式,允许开发者按需加载这些包中的纹理、模型和其他资产,从而优化游戏性能并减少初始下载大小。 #### 官方工具和支持 - **Unity Editor 内置功能** 使用 Unity 编辑器内置的功能可以方便地构建 AssetBundles。通过 `BuildPipeline.BuildAssetBundles` 方法能够指定输出路径以及其他选项来编译所需的 AssetBundles[^1]。 ```csharp using UnityEditor; public static void BuildAllAssetBundles() { string assetBundleDirectory = "Assets/AssetBundles"; if (!System.IO.Directory.Exists(assetBundleDirectory)) System.IO.Directory.CreateDirectory(assetBundleDirectory); BuildPipeline.BuildAssetBundles( assetBundleDirectory, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64); // 更改为目标平台 } ``` - **Addressable Assets Package** Addressables 是 Unity 推荐用于处理动态内容加载的新一代系统。它不仅简化了 AssetBundle 的管理和分发过程,还提供了更强大的依赖解析能力。安装此包后,可以通过简单的 API 调用来异步加载资源[^2]。 ```csharp using UnityEngine.AddressableAssets; private async Task<GameObject> LoadPrefabAsync(string prefabName) { var handle = Addressables.LoadAssetAsync<GameObject>(prefabName); await handle.Task; return handle.Result; } ``` #### 社区贡献工具 除了官方提供的工具外,还有许多由社区成员制作的强大插件可以帮助更好地利用 AssetBundles: - **Asset Bundle Browser** 这是一个非常受欢迎的编辑器扩展,提供了一个图形界面来浏览项目中存在的所有 AssetBundles 及其内部结构。这对于调试和分析哪些文件被打包进了哪个 bundle 非常有用。 - **Asset Bundle Manager** 此库旨在简化跨多个平台上部署和更新 AssetBundles 的流程。它包含了自动化的版本控制系统以及增量更新的支持等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值