支持单个替换,以及批量替换。另外,如果面板关联的脚本跟面板名称是一致的,也可以将代码中的Text修改为TextMeshProUGUI
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class Text2TextMeshProUtil : EditorWindow {
Vector2 scrollPos = Vector2.zero;
static List scriptsFolders;
string path;
string Path {
get
{
return path;
}
set
{
if (value != path) {
if (directoryPrefabs == null) {
directoryPrefabs = new List();
}
else {
directoryPrefabs.Clear();
}
if (AllAssetPaths == null) {
AllAssetPaths = new List();
}
else {
AllAssetPaths.Clear();
}
path = value;
}
}
}
static EditorWindow myWindow;
static List directoryPrefabs;
static List AllAssetPaths;
bool showPrefabs = true;
private static TMP_FontAsset tmp_Font = null;
readonly string [] ReplaceMode = { "批量替换" , "单个替换" };
int replaceModeIndex;
int ReplaceModeIndex {
get
{
return replaceModeIndex;
}
set
{
if (value != replaceModeIndex) {
if (directoryPrefabs == null) {
directoryPrefabs = new List();
}
else {
directoryPrefabs.Clear();
}
if (AllAssetPaths == null) {
AllAssetPaths = new List();
}
else {
AllAssetPaths.Clear();
}
Path = "";
replaceModeIndex = value;
}
}
}
bool isAutoFixLinkedScripts = true;
[MenuItem("Tools/Text2TextMeshPro")]
static void Init() {
myWindow = EditorWindow.GetWindow(typeof(Text2TextMeshProUtil));
directoryPrefabs = new List();
AllAssetPaths = new List();
scriptsFolders = new List { "Assets/GameMain/Scripts/UI" };
myWindow.minSize = new Vector2(500 , 300);
}
void OnGUI() {
isAutoFixLinkedScripts = EditorGUILayout.BeginToggleGroup("修改关联脚本" , isAutoFixLinkedScripts);
ShowScriptsFolders();
EditorGUILayout.EndToggleGroup();
ReplaceModeIndex = GUILayout.Toolbar(ReplaceModeIndex , ReplaceMode);
if (ReplaceModeIndex == 0) {
scrollPos = EditorGUILayout.BeginScrollView(scrollPos , new GUILayoutOption [] { GUILayout.Width(myWindow.position.width) , GUILayout.Height(myWindow.position.height -scriptsFolders.Count*20 - 70) });
GetPath(true);
ShowAllPrefabs();
EditorGUILayout.EndScrollView();
EditorGUILayout.BeginHorizontal();
LoadPrefab();
Text2TextMeshPro();
EditorGUILayout.EndHorizontal();
}
else if (ReplaceModeIndex == 1) {
scrollPos = EditorGUILayout.BeginScrollView(scrollPos , new GUILayoutOption [] { GUILayout.Width(myWindow.position.width) , GUILayout.Height(myWindow.position.height - scriptsFolders.Count * 20 - 70) });
GetPath(false);
ShowAllPrefabs();
EditorGUILayout.EndScrollView();
Text2TextMeshPro();
}
}
void GetPath(bool isFolder = true) {
Event e = Event.current;
EditorGUILayout.BeginHorizontal();
GUILayout.Label("Path:" , GUILayout.Width(40));
Path = GUILayout.TextField(Path);
EditorGUILayout.EndHorizontal();
if (Event.current.type == EventType.DragExited || Event.current.type == EventType.DragUpdated) {
if (GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition)) {
DragAndDrop.visualMode = DragAndDropVisualMode.Link;
if (Event.current.type == EventType.DragExited) {
DragAndDrop.AcceptDrag();
if (!isFolder) {
if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length > 0) {
Object [] objectReferences = DragAndDrop.objectReferences;
for (int i = 0 ; i < objectReferences.Length ; i++) {
int index = i;
if (AssetDatabase.GetAssetPath(objectReferences [index]).EndsWith(".prefab")) {
if (!directoryPrefabs.Contains(AssetDatabase.GetAssetPath(objectReferences [index]))) {
directoryPrefabs.Add(AssetDatabase.GetAssetPath(objectReferences [index]));
}
}
}
}
}
else {
if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0) {
if (File.Exists(DragAndDrop.paths [0])) {
EditorUtility.DisplayDialog("警告" , "批量模式下请拖拽文件夹!" , "确定");
return;
}
Path = DragAndDrop.paths [0];
}
}
}
e.Use();
}
else {
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
}
}
}
void ShowAllPrefabs() {
if (directoryPrefabs != null && directoryPrefabs.Count > 0) {
showPrefabs = EditorGUILayout.Foldout(showPrefabs , "显示预制体");
if (showPrefabs) {
for (int i = 0 ; i < directoryPrefabs.Count ; i++) {
int index = i;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.SelectableLabel($"预制体路径:{directoryPrefabs [index]}");
if (GUILayout.Button("查看" , GUILayout.Width(60))) {
EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath 


