Unity3D 【编辑器扩展】一键批量修改预设UGUI Text字体

我们做项目的时候经常会遇到要换个字体的工作情况,比如美工同学觉着字体不好看或者要做其它语言版本什么的。遇到这种情况我们总不能一个标签一个标签的去找到它们把字体换了,累不累就不说了,万一漏了也是麻烦事。

转载请保留原文链接:http://blog.csdn.net/andyhebear/article/details/51393259

using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

using System.Collections;
using System.Collections.Generic;

//[InitializeOnLoad]
public class ChangeFontWindow : EditorWindow {

	static ChangeFontWindow() {
        //toChangeFont = new Font("Arial");
        //toChangeFontStyle = FontStyle.Normal;
	}
	
	[MenuItem("Window/Change Font")]
	private static void ShowWindow() {
     ChangeFontWindow cw=   EditorWindow.GetWindow(true, "Window/Change Font");
   
	}
    Font toFont = new Font("Arial");
    static Font toChangeFont;
    FontStyle toFontStyle;
    static FontStyle toChangeFontStyle;
    private void OnGUI() {
        GUILayout.Space(10);
        GUILayout.Label("目标字体:");
        toFont = (Font)EditorGUILayout.ObjectField(toFont, typeof(Font), true, GUILayout.MinWidth(100f));
        toChangeFont = toFont;
        GUILayout.Space(10);
        GUILayout.Label("类型:");
        toFontStyle = (FontStyle)EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f));
        toChangeFontStyle = toFontStyle;
        if (GUILayout.Button("修改字体!")) {
            Change();
        }
	}
    public static void Change() {
        //获取所有UILabel组件
        if (Selection.objects == null || Selection.objects.Length==0) return;
        //如果是UGUI讲UILabel换成Text就可以
        Object[] labels = Selection.GetFiltered(typeof(Text), SelectionMode.Deep);
        foreach (Object item in labels) {
            //如果是UGUI讲UILabel换成Text就可以
            Text label = (Text)item;
            label.font = toChangeFont;
            label.fontStyle = toChangeFontStyle;
            //label.font = toChangeFont;(UGUI)
            Debug.Log(item.name + ":" + label.text);
            //
            EditorUtility.SetDirty(item);//重要
        }
    }
	private void OnEnable() {
	}
	
	private void OnDisable() {
	}
	

	
	private void Update() {
	}
	
	private void OnDestroy() {
	}
	
}


你可能感兴趣的:(Unity3D)