unity addressables遍历资源文件夹将旗下所有资源所在的文件夹名作为group的名字将所有资源添加到addressable中。用代码实现
以下是使用Unity Addressables遍历资源文件夹,并将所有资源添加到Addressable中的示例代码:
using UnityEngine;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
public class AddressablesBuilder
{
[MenuItem("Tools/Build Addressables")]
public static void BuildAddressables()
{
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
AddressableAssetGroup defaultGroup = settings.DefaultGroup;
// 获取资源文件夹路径
string[] folderPaths = AssetDatabase.GetSubFolders("Assets/Resources");
foreach (string folderPath in folderPaths)
{
// 获取文件夹名作为group的名字
string groupName = System.IO.Path.GetFileName(folderPath);
// 创建group
AddressableAssetGroup group = settings.CreateGroup(groupName, false, false, true, null, typeof(BundledAssetGroupSchema));
// 获取所有资源路径
string[] assetPaths = AssetDatabase.FindAssets("", new string[] { folderPath });
foreach (string assetPath in assetPaths)
{
// 添加资源到group
string assetGUID = AssetDatabase.GUIDToAssetPath(assetPath);
AddressableAssetEntry entry = settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(assetGUID), group, false, false);
entry.address = assetGUID;
}
}
// 保存Addressable设置
EditorUtility.SetDirty(settings);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
请确保你已经安装了Unity Addressables的包,并在Unity编辑器的菜单栏中选择 "Tools -> Build Addressables" 来执行该代码。这会遍历 "Assets/Resources" 文件夹下的所有资源,并将它们添加到Addressable中,以每个资源所在文件夹的名字作为group的名字
原文地址: https://www.cveoy.top/t/topic/hz4d 著作权归作者所有。请勿转载和采集!