2026-06-08 21:23:40 +08:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
|
using UnityEditor.AddressableAssets;
|
|
|
|
|
|
using UnityEditor.AddressableAssets.Settings;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将炮台 Prefab(Turrets/paotai*.prefab)添加到 Addressables,
|
|
|
|
|
|
/// 标签:PT + Default(与 Sprite 一致)。
|
|
|
|
|
|
/// 菜单:Tools → 炮台 → 配置Prefab AA标签
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class TurretPrefabAALabeler : EditorWindow
|
|
|
|
|
|
{
|
|
|
|
|
|
private const string PREFABS_DIR = "Assets/MyGame/aa/Game/Turrets";
|
|
|
|
|
|
private const string GROUP_NAME = "Default Local Group";
|
|
|
|
|
|
private const string LABEL_PT = "PT";
|
|
|
|
|
|
private const string LABEL_DEFAULT = "Default";
|
|
|
|
|
|
|
2026-07-29 22:59:26 +08:00
|
|
|
|
// ============================ 日志开关 ============================
|
|
|
|
|
|
// 启用: Player Settings → Scripting Define Symbols 添加 ENABLE_EDITOR_LOG
|
|
|
|
|
|
// 关闭: 删除该符号。未定义时所有 Log/LogError 调用被编译器自动移除,零开销
|
|
|
|
|
|
[System.Diagnostics.Conditional("ENABLE_EDITOR_LOG")]
|
|
|
|
|
|
private static void Log(string msg) { Debug.Log(msg); }
|
|
|
|
|
|
|
|
|
|
|
|
[System.Diagnostics.Conditional("ENABLE_EDITOR_LOG")]
|
|
|
|
|
|
private static void LogError(string msg) { Debug.LogError(msg); }
|
|
|
|
|
|
|
2026-06-08 21:23:40 +08:00
|
|
|
|
[MenuItem("Tools/炮台/配置Prefab AA标签(PT+Default)", false, 102)]
|
|
|
|
|
|
public static void SetupAddressableLabels()
|
|
|
|
|
|
{
|
|
|
|
|
|
var settings = AddressableAssetSettingsDefaultObject.Settings;
|
|
|
|
|
|
if (settings == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorUtility.DisplayDialog("错误", "Addressables 未初始化。", "确定");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AddressableAssetGroup defaultGroup = settings.FindGroup(GROUP_NAME)
|
|
|
|
|
|
?? settings.DefaultGroup;
|
|
|
|
|
|
if (defaultGroup == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
EditorUtility.DisplayDialog("错误", $"找不到 Group: {GROUP_NAME}", "确定");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EnsureLabel(settings, LABEL_PT);
|
|
|
|
|
|
EnsureLabel(settings, LABEL_DEFAULT);
|
|
|
|
|
|
|
|
|
|
|
|
string absoluteDir = Path.Combine(Application.dataPath, "MyGame/aa/Game/Turrets");
|
|
|
|
|
|
string[] prefabFiles = Directory.GetFiles(absoluteDir, "paotai*.prefab");
|
2026-07-29 22:59:26 +08:00
|
|
|
|
Log($"[TurretPrefabAA] 扫描到 {prefabFiles.Length} 个炮台 Prefab");
|
2026-06-08 21:23:40 +08:00
|
|
|
|
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
var errors = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string absolutePath in prefabFiles)
|
|
|
|
|
|
{
|
|
|
|
|
|
string fileName = Path.GetFileNameWithoutExtension(absolutePath);
|
|
|
|
|
|
string assetPath = absolutePath.Replace(Application.dataPath, "Assets").Replace("\\", "/");
|
|
|
|
|
|
|
|
|
|
|
|
string guid = AssetDatabase.AssetPathToGUID(assetPath);
|
|
|
|
|
|
if (string.IsNullOrEmpty(guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
errors.Add($"{fileName}: GUID 为空");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var entry = settings.FindAssetEntry(guid);
|
|
|
|
|
|
if (entry == null)
|
|
|
|
|
|
entry = settings.CreateOrMoveEntry(guid, defaultGroup);
|
|
|
|
|
|
|
|
|
|
|
|
if (entry == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
errors.Add($"{fileName}: 无法创建 AA 条目");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entry.address = fileName; // paotai11001
|
|
|
|
|
|
entry.SetLabel(LABEL_PT, true, true);
|
|
|
|
|
|
entry.SetLabel(LABEL_DEFAULT, true, true);
|
|
|
|
|
|
|
|
|
|
|
|
count++;
|
2026-07-29 22:59:26 +08:00
|
|
|
|
Log($"[TurretPrefabAA] ✓ {fileName}");
|
2026-06-08 21:23:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, null, true);
|
|
|
|
|
|
AssetDatabase.SaveAssets();
|
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
|
|
|
|
|
|
string msg = $"成功: {count} 个";
|
|
|
|
|
|
if (errors.Count > 0) msg += $"\n\n失败 {errors.Count} 个:\n" + string.Join("\n", errors);
|
|
|
|
|
|
EditorUtility.DisplayDialog("Prefab AA标签完成", msg, "确定");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void EnsureLabel(AddressableAssetSettings settings, string label)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!settings.GetLabels().Contains(label))
|
|
|
|
|
|
{
|
|
|
|
|
|
settings.AddLabel(label);
|
2026-07-29 22:59:26 +08:00
|
|
|
|
Log($"[TurretPrefabAA] 新增标签: {label}");
|
2026-06-08 21:23:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|