MeowmentDebugTool/Packages/com.bywaystudios.meowmentdebugtool/Editor/CreateTabButtonPrefab.cs
2025-12-19 12:14:42 +08:00

100 lines
3.4 KiB
C#

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using TMPro;
/// <summary>
/// 手动创建Tab按钮预制件的辅助工具
/// </summary>
public class CreateTabButtonPrefab : EditorWindow
{
[MenuItem("Tools/Debug Tool/手动创建Tab按钮预制件")]
public static void CreatePrefab()
{
// 创建Tab按钮
GameObject btn = new GameObject("TabButtonPrefab");
RectTransform rt = btn.AddComponent<RectTransform>();
rt.sizeDelta = new Vector2(200, 80);
Image img = btn.AddComponent<Image>();
img.color = Color.gray;
Button button = btn.AddComponent<Button>();
button.targetGraphic = img;
// 添加文本
GameObject textObj = new GameObject("Text");
textObj.transform.SetParent(btn.transform, false);
RectTransform textRt = textObj.AddComponent<RectTransform>();
textRt.anchorMin = Vector2.zero;
textRt.anchorMax = Vector2.one;
textRt.sizeDelta = Vector2.zero;
TextMeshProUGUI text = textObj.AddComponent<TextMeshProUGUI>();
text.text = "Tab";
text.fontSize = 32;
text.alignment = TextAlignmentOptions.Center;
text.color = Color.white;
// 保存为预制件
string path = "Assets/TabButtonPrefab.prefab";
PrefabUtility.SaveAsPrefabAsset(btn, path);
// 清理
DestroyImmediate(btn);
// 选中
Selection.activeObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
EditorUtility.DisplayDialog("成功", $"Tab按钮预制件已创建: {path}\n请将其拖到UniversalDebugTool的Tab Button Prefab字段", "确定");
}
[MenuItem("Tools/Debug Tool/手动创建自定义按钮预制件")]
public static void CreateCustomButtonPrefab()
{
// 创建自定义按钮
GameObject btn = new GameObject("CustomButtonPrefab");
RectTransform rt = btn.AddComponent<RectTransform>();
rt.sizeDelta = new Vector2(200, 80);
Image img = btn.AddComponent<Image>();
img.color = new Color(0.3f, 0.3f, 0.8f);
Button button = btn.AddComponent<Button>();
button.targetGraphic = img;
// 添加文本
GameObject textObj = new GameObject("Text");
textObj.transform.SetParent(btn.transform, false);
RectTransform textRt = textObj.AddComponent<RectTransform>();
textRt.anchorMin = Vector2.zero;
textRt.anchorMax = Vector2.one;
textRt.sizeDelta = Vector2.zero;
textRt.offsetMin = new Vector2(10, 10);
textRt.offsetMax = new Vector2(-10, -10);
TextMeshProUGUI text = textObj.AddComponent<TextMeshProUGUI>();
text.text = "Button";
text.fontSize = 24;
text.alignment = TextAlignmentOptions.Center;
text.color = Color.white;
text.enableWordWrapping = true;
// 保存为预制件
string path = "Assets/CustomButtonPrefab.prefab";
PrefabUtility.SaveAsPrefabAsset(btn, path);
// 清理
DestroyImmediate(btn);
// 选中
Selection.activeObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
EditorUtility.DisplayDialog("成功", $"自定义按钮预制件已创建: {path}\n请将其拖到UniversalDebugTool的Button Prefab字段", "确定");
}
}