76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
using UnityEditor;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
using UnityEngine.UI;
|
|
|
|
public class ReplaceTMPDropdownEditor : EditorWindow
|
|
{
|
|
[MenuItem("Tools/Replace TMP Dropdowns")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<ReplaceTMPDropdownEditor>("Replace TMP Dropdowns");
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
GUILayout.Label("Replace TMP_Dropdowns with CustomTMPDropdown", EditorStyles.boldLabel);
|
|
|
|
if (GUILayout.Button("Replace All TMP_Dropdowns in Scene (Include Hidden)"))
|
|
{
|
|
ReplaceAllDropdownsInScene();
|
|
}
|
|
}
|
|
|
|
private static void ReplaceAllDropdownsInScene()
|
|
{
|
|
int count = 0;
|
|
|
|
// 遍历场景根对象
|
|
foreach (var root in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects())
|
|
{
|
|
count += ReplaceDropdownsRecursive(root.transform);
|
|
}
|
|
|
|
Debug.Log($"ReplaceTMPDropdownEditor: Replaced {count} TMP_Dropdowns with CustomTMPDropdown (including hidden).");
|
|
}
|
|
|
|
private static int ReplaceDropdownsRecursive(Transform parent)
|
|
{
|
|
int replacedCount = 0;
|
|
|
|
// 获取 TMP_Dropdown 组件(即使隐藏)
|
|
TMP_Dropdown tmp = parent.GetComponent<TMP_Dropdown>();
|
|
if (tmp != null && !(tmp is CustomTMPDropdown))
|
|
{
|
|
GameObject go = tmp.gameObject;
|
|
|
|
// 保存属性
|
|
var template = tmp.template;
|
|
var captionText = tmp.captionText;
|
|
var itemText = tmp.itemText;
|
|
var options = tmp.options;
|
|
|
|
// 删除旧组件
|
|
Object.DestroyImmediate(tmp, true);
|
|
|
|
// 添加 CustomTMPDropdown
|
|
CustomTMPDropdown newDropdown = go.AddComponent<CustomTMPDropdown>();
|
|
newDropdown.template = template;
|
|
newDropdown.captionText = captionText;
|
|
newDropdown.itemText = itemText;
|
|
newDropdown.options = new System.Collections.Generic.List<TMP_Dropdown.OptionData>(options);
|
|
newDropdown.alphaFadeSpeed = 0.15f;
|
|
|
|
replacedCount++;
|
|
}
|
|
|
|
// 递归遍历子物体
|
|
foreach (Transform child in parent)
|
|
{
|
|
replacedCount += ReplaceDropdownsRecursive(child);
|
|
}
|
|
|
|
return replacedCount;
|
|
}
|
|
}
|