using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
namespace AutoGroupGenerator
{
///
/// Utility helpers for interacting with the Addressables configuration.
///
public static class AddressableUtil
{
#region Static Methods
///
/// Finds the default Addressable group template from the project settings.
///
/// The default group template.
public static AddressableAssetGroupTemplate FindDefaultAddressableGroupTemplate()
{
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
if (settings == null)
{
throw new("AddressableAssetSettings not found. Ensure Addressables are initialized.");
}
List templates = settings.GroupTemplateObjects;
if (templates == null || templates.Count == 0)
{
throw new("No group templates found in Addressable Settings.");
}
var defaultTemplate = templates[0];
return defaultTemplate as AddressableAssetGroupTemplate;
}
///
/// Gets all Addressable asset entries from non-read-only groups.
///
/// A list of addressable asset entries.
public static List GetAddressableEntries()
{
var entries = new List();
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
if (settings == null)
{
return entries;
}
foreach (var group in settings.groups.Where(group => !group.ReadOnly))
{
if (group == null)
continue;
entries.AddRange(group.entries);
}
return entries;
}
///
/// Gets addressable asset paths, expanding folders into their contained assets.
///
/// If true, include folder paths in the result.
/// A set of asset paths referenced by Addressables.
public static HashSet GetExtendedAddressableEntries(bool includeFolderEntries = false)
{
var result = new HashSet();
foreach (var entry in GetAddressableEntries())
{
if (entry == null)
continue;
string entryAssetPath = entry.AssetPath;
if (string.IsNullOrEmpty(entryAssetPath))
continue;
bool isFolder = AssetDatabase.IsValidFolder(entryAssetPath);
if (isFolder)
{
if (includeFolderEntries)
result.Add(entryAssetPath);
var guidsUnderFolder = AssetDatabase.FindAssets(string.Empty, new[] { entryAssetPath });
foreach (var guid in guidsUnderFolder)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
if (string.IsNullOrEmpty(assetPath))
continue;
if (AssetDatabase.IsValidFolder(assetPath))
continue;
result.Add(assetPath);
}
}
else
{
result.Add(entryAssetPath);
}
}
return result;
}
#endregion
}
}