| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 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<Font>(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<GameObject>(assetPath);
- if (obj == null) continue;
- Text[] texts = obj.GetComponentsInChildren<Text>(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<Text>(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 字体。");
- }
- }
|