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<ChangeFontWindow>(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() {
	}
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rains卍Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值