Files
5.24BuYu/Assets/MyGame/Editor/TurretSpriteAALabeler.cs
T
2026-10-20 18:30:03 +08:00

153 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEditor;
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 将炮台 Sprite(Sprites/paotai*.png)添加到 Addressables 系统,
/// 标签:PT + Default,地址与 prefabTurretResourcesPath 一致。
/// 菜单:Tools → 炮台 → 配置AA标签(PT+Default)
/// </summary>
public class TurretSpriteAALabeler : EditorWindow
{
private const string SPRITES_DIR = "Assets/MyGame/aa/Game/Turrets/Sprites";
private const string GROUP_NAME = "Default Local Group";
private const string LABEL_PT = "PT";
private const string LABEL_DEFAULT = "Default";
// ============================ 日志开关 ============================
// 启用: 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); }
[MenuItem("Tools/炮台/配置AA标签(PT+Default)", false, 101)]
public static void SetupAddressableLabels()
{
var settings = AddressableAssetSettingsDefaultObject.Settings;
if (settings == null)
{
EditorUtility.DisplayDialog("错误", "Addressables 未初始化,请先配置 Addressable Asset Settings。", "确定");
return;
}
// 找到 Default Local Group
AddressableAssetGroup defaultGroup = settings.FindGroup(GROUP_NAME);
if (defaultGroup == null)
{
defaultGroup = settings.DefaultGroup;
if (defaultGroup == null)
{
EditorUtility.DisplayDialog("错误", $"找不到 Addressable Group: {GROUP_NAME}", "确定");
return;
}
}
// 确保标签存在
EnsureLabel(settings, LABEL_PT);
EnsureLabel(settings, LABEL_DEFAULT);
// ===== 直接扫文件系统,不依赖 AssetDatabase.FindAssets =====
string absoluteSpritesDir = Path.Combine(Application.dataPath, "MyGame/aa/Game/Turrets/Sprites");
if (!Directory.Exists(absoluteSpritesDir))
{
EditorUtility.DisplayDialog("错误", $"目录不存在:\n{absoluteSpritesDir}", "确定");
return;
}
string[] pngFiles = Directory.GetFiles(absoluteSpritesDir, "paotai*.png");
Log($"[TurretSpriteAA] 扫描到 {pngFiles.Length} 个炮台 PNG 文件");
// 强制刷新 AssetDatabase,确保所有 PNG 都被识别
AssetDatabase.Refresh();
int count = 0;
var errors = new List<string>();
foreach (string absolutePath in pngFiles)
{
string fileName = Path.GetFileNameWithoutExtension(absolutePath); // paotai11001
// 绝对路径 → Assets 相对路径
string assetPath = absolutePath.Replace(Application.dataPath, "Assets").Replace("\\", "/");
// 确保 TextureImporter 是 Sprite
var importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (importer == null)
{
// 文件可能还没导入,强制导入
Log($"[TurretSpriteAA] 强制导入: {assetPath}");
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
}
if (importer == null)
{
errors.Add($"{fileName}: TextureImporter 仍为空,跳过");
continue;
}
if (importer.textureType != TextureImporterType.Sprite)
{
importer.textureType = TextureImporterType.Sprite;
importer.spriteImportMode = SpriteImportMode.Single;
importer.SaveAndReimport();
}
// 获取 GUID(刷新后)
string guid = AssetDatabase.AssetPathToGUID(assetPath);
if (string.IsNullOrEmpty(guid))
{
errors.Add($"{fileName}: 无法获取 GUID");
continue;
}
// 创建或获取 Addressable 条目
var entry = settings.FindAssetEntry(guid);
if (entry == null)
{
entry = settings.CreateOrMoveEntry(guid, defaultGroup);
}
if (entry == null)
{
errors.Add($"{fileName}: 无法创建 AA 条目");
continue;
}
// 设置地址 = 文件名(paotai11001),与 prefabTurretResourcesPath 匹配
entry.address = fileName;
// 设置标签:PT + Default
entry.SetLabel(LABEL_PT, true, true);
entry.SetLabel(LABEL_DEFAULT, true, true);
count++;
Log($"[TurretSpriteAA] ✓ {fileName}: address={entry.address}, labels=PT+Default");
}
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, null, true);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
string message = $"成功: {count} 个";
if (errors.Count > 0)
message += $"\n\n失败 {errors.Count} 个:\n" + string.Join("\n", errors);
EditorUtility.DisplayDialog("AA标签配置完成", message, "确定");
}
private static void EnsureLabel(AddressableAssetSettings settings, string label)
{
if (!settings.GetLabels().Contains(label))
{
settings.AddLabel(label);
Log($"[TurretSpriteAA] 新增标签: {label}");
}
}
}