Unity 一键更换字体工具

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.UI;

public class ChangeFontWindow : EditorWindow {

    [MenuItem("Tools/UI/Change Font")]
    private static void ShowWindow()
    {
        ChangeFontWindow cw = EditorWindow.GetWindow(true, "Custom/Change Font");
    }

    static Font defaultFont = new Font("Arial");
    static Font toFont = new Font("Arial");

    void OnGUI()
    {
        GUILayout.Space(10);
        GUILayout.Label("目标字体:");
        toFont = (Font)EditorGUILayout.ObjectField(toFont, typeof(Font), true, GUILayout.MinWidth(100f));
        GUILayout.Space(10);
        GUILayout.Label("字体类型:");
        if (GUILayout.Button("确认修改"))
        {
            Change();
        }
    }

    public static void Change()
    {
        Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        List list = new List();
        for (int i = 0; i < selection.Length; i++)
        {
            string full = AssetDatabase.GetAssetOrScenePath(selection[i]);
            string last = Packager.GetLastPath(full);
            if (last.EndsWith(".prefab"))
            {
                list.Add(((GameObject)selection[i]).transform);
            }
        }

        List labels = new List();
        GetLabels(labels, list);
        foreach (Text label in labels)
        {
            if (label.font.name == "Arial")
            {
                label.font = toFont;
                EditorUtility.SetDirty(label);
            }
        }
        AssetDatabase.Refresh();
    }

    static void GetLabels(List labels, List list)
    {
        foreach (Transform t in list)
        {
            A(labels, t);
        }
    }

    static void A(List list, Transform target)
    {
        if (target.childCount > 0)
        {
            foreach (Transform child in target)
            {
                A(list, child);
            }
        }
        Text l = target.GetComponent();
        if (l)
        {

            list.Add(l);
        }
    }
}

你可能感兴趣的:(unity,unity3d)