using UnityEngine; using UnityEngine.UI; using UnityEditor; using UnityEditor.SceneManagement; using System.Collections.Generic; using System.IO; public class GlobalSceneFontReplacer { [MenuItem("Tools/全项目场景 + Prefab 替换字体")] static void ReplaceFontInAllScenesAndPrefabs() { string fontPath = "Assets/BowArrow/Fonts/HarmonyOS_Sans_SC_Regular.ttf"; Font newFont = AssetDatabase.LoadAssetAtPath(fontPath); if (newFont == null) { Debug.LogError("找不到目标字体!"); return; } int totalChanges = 0; // ✅ 替换所有 prefab string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab"); foreach (string guid in prefabGuids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); GameObject obj = AssetDatabase.LoadAssetAtPath(assetPath); if (obj == null) continue; Text[] texts = obj.GetComponentsInChildren(true); bool changed = false; foreach (Text text in texts) { if (text.font != null && text.font.name == "Arial") { Undo.RecordObject(text, "Replace Font"); text.font = newFont; EditorUtility.SetDirty(text); changed = true; totalChanges++; } } if (changed) { PrefabUtility.SavePrefabAsset(obj); AssetDatabase.SaveAssets(); } } // ✅ 替换所有场景 string[] sceneGuids = AssetDatabase.FindAssets("t:Scene"); foreach (string guid in sceneGuids) { string scenePath = AssetDatabase.GUIDToAssetPath(guid); var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single); bool changed = false; foreach (GameObject root in scene.GetRootGameObjects()) { Text[] texts = root.GetComponentsInChildren(true); foreach (Text text in texts) { if (text.font != null && text.font.name == "Arial") { Undo.RecordObject(text, "Replace Font"); text.font = newFont; EditorUtility.SetDirty(text); changed = true; totalChanges++; } } } if (changed) { EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(scene); } } Debug.Log($"✅ 场景 + Prefab 字体替换完成,共替换 {totalChanges} 个 Text 字体。"); } }