com.alicizax.unity.editor.e.../Editor/Postprocessor/Atlas/AtlasEditorWindow.cs

214 lines
7.8 KiB
C#
Raw Normal View History

2025-03-14 16:02:36 +08:00
#if UNITY_EDITOR
2025-03-11 17:46:52 +08:00
using UnityEditor;
2025-03-27 17:00:12 +08:00
using UnityEditorInternal;
2025-03-11 17:46:52 +08:00
using UnityEngine;
2025-03-14 16:02:36 +08:00
public class AtlasConfigWindow : EditorWindow
2025-03-11 17:46:52 +08:00
{
2025-03-27 17:00:12 +08:00
[MenuItem("Tools/图集管理工具/Configuration Panel")]
2025-03-11 17:46:52 +08:00
public static void ShowWindow()
{
2025-03-27 17:00:12 +08:00
GetWindow<AtlasConfigWindow>("Atlas Config").minSize = new Vector2(500, 600);
2025-03-11 17:46:52 +08:00
}
2025-03-27 17:00:12 +08:00
private Vector2 scrollPosition;
private ReorderableList keywordsList;
private ReorderableList foldersList;
private bool showExclusionSettings = true;
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
private void OnEnable()
{
var config = AtlasConfiguration.Instance;
// 初始化关键词列表
keywordsList = CreateKeywordsList(config);
// 初始化目录列表
foldersList = CreateFoldersList(config);
}
2025-03-11 17:46:52 +08:00
private void OnGUI()
{
2025-03-14 16:02:36 +08:00
var config = AtlasConfiguration.Instance;
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
using (var scroll = new EditorGUILayout.ScrollViewScope(scrollPosition))
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
scrollPosition = scroll.scrollPosition;
2025-03-11 17:46:52 +08:00
2025-03-14 16:02:36 +08:00
EditorGUI.BeginChangeCheck();
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
DrawDirectorySettings(config);
2025-03-14 16:02:36 +08:00
DrawPlatformSettings(config);
DrawPackingSettings(config);
2025-03-27 17:00:12 +08:00
DrawCompressionSettings(config);
DrawExclusionSettings(config);
DrawFeatureToggles(config);
2025-03-11 17:46:52 +08:00
2025-03-14 16:02:36 +08:00
if (EditorGUI.EndChangeCheck())
2025-03-11 17:46:52 +08:00
{
2025-03-14 16:02:36 +08:00
AtlasConfiguration.Save(true);
AssetDatabase.Refresh();
2025-03-11 17:46:52 +08:00
}
2025-03-14 16:02:36 +08:00
}
2025-03-27 17:00:12 +08:00
DrawActionButtons();
2025-03-14 16:02:36 +08:00
}
2025-03-27 17:00:12 +08:00
#region List Creation
private ReorderableList CreateKeywordsList(AtlasConfiguration config)
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
var list = new ReorderableList(config.excludeKeywords, typeof(string), true, true, true, true)
{
drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Excluded Keywords"),
elementHeight = EditorGUIUtility.singleLineHeight + 4,
drawElementCallback = (rect, index, active, focused) =>
{
rect.y += 2;
config.excludeKeywords[index] = EditorGUI.TextField(
new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
config.excludeKeywords[index]
);
},
onAddCallback = _ =>
{
ArrayUtility.Add(ref config.excludeKeywords, "");
EditorGUI.FocusTextInControl(null);
},
onRemoveCallback = _ => ArrayUtility.RemoveAt(ref config.excludeKeywords, keywordsList.index)
};
return list;
2025-03-14 16:02:36 +08:00
}
2025-03-27 17:00:12 +08:00
private ReorderableList CreateFoldersList(AtlasConfiguration config)
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
var list = new ReorderableList(config.excludeFolders, typeof(string), true, true, true, true)
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
drawHeaderCallback = rect => EditorGUI.LabelField(rect, "Excluded Folders"),
elementHeight = EditorGUIUtility.singleLineHeight + 4,
drawElementCallback = (rect, index, active, focused) =>
2025-03-11 17:46:52 +08:00
{
2025-03-27 17:00:12 +08:00
rect.y += 2;
var textRect = new Rect(rect.x, rect.y, rect.width - 60, EditorGUIUtility.singleLineHeight);
var buttonRect = new Rect(rect.x + rect.width - 55, rect.y, 50, EditorGUIUtility.singleLineHeight);
config.excludeFolders[index] = EditorGUI.TextField(textRect, config.excludeFolders[index]);
if (GUI.Button(buttonRect, "Browse"))
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
var path = EditorUtility.OpenFolderPanel("Select Folder", Application.dataPath, "");
if (!string.IsNullOrEmpty(path))
{
config.excludeFolders[index] = "Assets" + path.Substring(Application.dataPath.Length);
}
2025-03-14 16:02:36 +08:00
}
2025-03-27 17:00:12 +08:00
},
onAddCallback = _ =>
{
ArrayUtility.Add(ref config.excludeFolders, "");
EditorGUI.FocusTextInControl(null);
},
onRemoveCallback = _ => ArrayUtility.RemoveAt(ref config.excludeFolders, foldersList.index)
};
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
return list;
}
#endregion
#region Drawing Methods
private void DrawDirectorySettings(AtlasConfiguration config)
{
GUILayout.Label("Directory Settings", EditorStyles.boldLabel);
config.outputAtlasDir = DrawFolderField("Output Directory", config.outputAtlasDir);
config.sourceAtlasRoot = DrawFolderField("Source Root", config.sourceAtlasRoot);
EditorGUILayout.Space();
2025-03-11 17:46:52 +08:00
}
2025-03-14 16:02:36 +08:00
private void DrawPlatformSettings(AtlasConfiguration config)
2025-03-11 17:46:52 +08:00
{
2025-03-27 17:00:12 +08:00
GUILayout.Label("Platform Settings", EditorStyles.boldLabel);
config.androidFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("Android Format", config.androidFormat);
config.iosFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("iOS Format", config.iosFormat);
config.webglFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("WebGL Format", config.webglFormat);
2025-03-11 17:46:52 +08:00
EditorGUILayout.Space();
2025-03-14 16:02:36 +08:00
}
2025-03-11 17:46:52 +08:00
2025-03-14 16:02:36 +08:00
private void DrawPackingSettings(AtlasConfiguration config)
{
2025-03-27 17:00:12 +08:00
GUILayout.Label("Packing Settings", EditorStyles.boldLabel);
config.padding = EditorGUILayout.IntPopup("Padding", config.padding,
new[] { "2", "4", "8" }, new[] { 2, 4, 8 });
config.blockOffset = EditorGUILayout.IntField("Block Offset", config.blockOffset);
2025-03-14 16:02:36 +08:00
config.enableRotation = EditorGUILayout.Toggle("Enable Rotation", config.enableRotation);
2025-03-27 17:00:12 +08:00
config.tightPacking = EditorGUILayout.Toggle("Trim Transparency", config.tightPacking);
2025-03-11 17:46:52 +08:00
EditorGUILayout.Space();
2025-03-14 16:02:36 +08:00
}
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
private void DrawCompressionSettings(AtlasConfiguration config)
{
GUILayout.Label("Compression", EditorStyles.boldLabel);
config.compressionQuality = EditorGUILayout.IntSlider("Quality", config.compressionQuality, 0, 100);
EditorGUILayout.Space();
}
2025-03-11 17:46:52 +08:00
2025-03-27 17:00:12 +08:00
private void DrawExclusionSettings(AtlasConfiguration config)
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
showExclusionSettings = EditorGUILayout.BeginFoldoutHeaderGroup(showExclusionSettings, "Exclusion Settings");
if (showExclusionSettings)
2025-03-11 17:46:52 +08:00
{
2025-03-27 17:00:12 +08:00
EditorGUILayout.HelpBox("Items matching these criteria will be excluded from atlas generation",
MessageType.Info);
2025-03-14 16:02:36 +08:00
2025-03-27 17:00:12 +08:00
keywordsList.DoLayoutList();
EditorGUILayout.Space(10);
foldersList.DoLayoutList();
}
EditorGUILayout.EndFoldoutHeaderGroup();
}
2025-03-14 16:02:36 +08:00
2025-03-27 17:00:12 +08:00
private void DrawFeatureToggles(AtlasConfiguration config)
{
GUILayout.Label("Features", EditorStyles.boldLabel);
config.autoGenerate = EditorGUILayout.Toggle("Auto Generate", config.autoGenerate);
config.enableLogging = EditorGUILayout.Toggle("Enable Logging", config.enableLogging);
config.enableV2 = EditorGUILayout.Toggle("Use V2 Packing", config.enableV2);
EditorGUILayout.Space();
}
2025-03-14 16:02:36 +08:00
2025-03-27 17:00:12 +08:00
private string DrawFolderField(string label, string path)
{
using (new EditorGUILayout.HorizontalScope())
{
path = EditorGUILayout.TextField(label, path);
if (GUILayout.Button("Select", GUILayout.Width(60)))
2025-03-14 16:02:36 +08:00
{
2025-03-27 17:00:12 +08:00
var newPath = EditorUtility.OpenFolderPanel(label, Application.dataPath, "");
if (!string.IsNullOrEmpty(newPath))
{
path = "Assets" + newPath.Substring(Application.dataPath.Length);
}
2025-03-14 16:02:36 +08:00
}
2025-03-11 17:46:52 +08:00
}
2025-03-27 17:00:12 +08:00
return path;
2025-03-14 16:02:36 +08:00
}
private void DrawActionButtons()
{
2025-03-27 17:00:12 +08:00
EditorGUILayout.Space(20);
2025-03-14 16:02:36 +08:00
using (new EditorGUILayout.HorizontalScope())
2025-03-11 17:46:52 +08:00
{
2025-03-27 17:00:12 +08:00
if (GUILayout.Button("Generate All Atlases", GUILayout.Height(30)))
2025-03-14 16:02:36 +08:00
{
EditorSpriteSaveInfo.ForceGenerateAll();
}
2025-03-27 17:00:12 +08:00
if (GUILayout.Button("Clear Cache", GUILayout.Height(30)))
2025-03-14 16:02:36 +08:00
{
EditorSpriteSaveInfo.ClearCache();
}
2025-03-11 17:46:52 +08:00
}
}
2025-03-27 17:00:12 +08:00
#endregion
2025-03-11 17:46:52 +08:00
}
2025-03-14 16:02:36 +08:00
#endif