1. 程式人生 > 實用技巧 >Unity 批量替換字型

Unity 批量替換字型

using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
 
public class ChangeFontWindow : EditorWindow
{
    [MenuItem("Tools/更換字型")]
    public static void Open()
    {
        EditorWindow.GetWindow(typeof(ChangeFontWindow));
    }
 
    Font toChange;
    static Font toChangeFont;
    FontStyle toFontStyle;
    
static FontStyle toChangeFontStyle; void OnGUI() { toChange = (Font)EditorGUILayout.ObjectField(toChange, typeof(Font), true, GUILayout.MinWidth(100f)); toChangeFont = toChange; toFontStyle = (FontStyle)EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f)); toChangeFontStyle
= toFontStyle; if (GUILayout.Button("更換")) { Change(); } } public static void Change() { Transform canvas = GameObject.Find("Canvas").transform; if (!canvas) { Debug.Log("NO Canvas"); return; } Transform[] tArray
= canvas.GetComponentsInChildren<Transform>(); for (int i = 0; i < tArray.Length; i++) { Text t = tArray[i].GetComponent<Text>(); if (t) { //如果不加這個程式碼 在做完更改後 自己隨便修改下場景裡物體的狀態 在儲存就好了 ,不然他會覺得沒變 就不會儲存 就失敗了 Undo.RecordObject(t, t.gameObject.name); t.font = toChangeFont; t.fontStyle = toChangeFontStyle; //重新整理下 EditorUtility.SetDirty(t); } } Debug.Log("Succed"); } }

本文連結:https://blog.csdn.net/li1214661543/article/details/105615225