Change UI Prefabs Font for UnityEditor(NGUI UILabel)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
/// <summary>
/// UI替换字体
/// </summary>
public class UIFontEditor : EditorWindow
{
    private static Font newFont;
    private static string uiPrefabsDir = "Assets/RawAssets/UI";
    private static int fontSizeOffset = 0;
    private static bool isFontSize=false;
    private static bool isFontBold=false;
    [MenuItem("Tools/UI/UI字体替换")]
    public static void CreateWindow()
    {
        GetWindow<UIFontEditor>("Change UI Font");
    }

    private void OnGUI()
    {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.BeginVertical();
        EditorGUILayout.LabelField("该工具替换UI预设UILabel字体");
        EditorGUILayout.LabelField("默认不改变字体大小和样式");
        EditorGUILayout.LabelField("默认 UI Prefabs 路径:",uiPrefabsDir);
        EditorGUILayout.Space(10);
        uiPrefabsDir = EditorGUILayout.TextField("UI Prefabs Path:", uiPrefabsDir);
        EditorGUILayout.Space(10);
        newFont = EditorGUILayout.ObjectField("New Font", newFont, typeof(Font), false) as Font;
        EditorGUILayout.Space(10);
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button(" - ",GUILayout.Width(50),GUILayout.Height(25)))
        {
            fontSizeOffset -= 2;
            //if (fontSizeOffset < 0) fontSizeOffset = 0;
        }
        GUILayout.Label("字体size增加值:");
        GUILayout.Label(fontSizeOffset.ToString());
        if (GUILayout.Button(" + ",GUILayout.Width(50),GUILayout.Height(25)))
        {
            fontSizeOffset += 2;
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.Space(10);
        isFontSize = GUILayout.Toggle(isFontSize, "改变字体大小");
        isFontBold = GUILayout.Toggle(isFontBold, "粗体");
        EditorGUILayout.Space(10);
        if(GUILayout.Button("Change Font",GUILayout.Width(100),GUILayout.Height(25)))
        {
            if (newFont == null)
            {
                Debug.LogError("New Font 不能为空!");
                return;
            }
            FindPrefabs();
        }
        EditorGUILayout.EndVertical();
        EditorGUI.EndChangeCheck();
    }

    public static void FindPrefabs()
    {
        string[] guids =  AssetDatabase.FindAssets("t:Prefab", new[] {uiPrefabsDir});
        foreach (string guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            GameObject go =  AssetDatabase.LoadAssetAtPath<GameObject>(path);
            DoChange(go);
        }
        AssetDatabase.SaveAssets();
    }
    public static void DoChange(GameObject target)
    {
        int cnt = 0;
        UILabel[] labels = target.GetComponentsInChildren<UILabel>();
         foreach (UILabel label in labels)
         {
             if (label.trueTypeFont )
             {
                 //&& label.trueTypeFont.name != newFont.name
                 label.trueTypeFont = newFont;
                 if (isFontSize && 0 != fontSizeOffset)
                 {
                     label.fontSize += fontSizeOffset;
                 }
                 if (isFontBold)
                 {
                     label.fontStyle = FontStyle.Bold;
                 }
                 cnt++;
                 //Debug.LogFormat("ChangeFont_UILabelName:{0} ,FontName{1} ,FontSize{2}",label.name,label.trueTypeFont.name,label.fontSize);
             }
         }
         EditorUtility.SetDirty(target);
         Debug.LogFormat("Font Changed. Target:{0} ,Count:{1}",target.name,cnt);
    }
        
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值