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

来源:互联网 发布:淘宝信用卡付款手续费 编辑:程序博客网 时间:2024/06/06 18:36

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

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

[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using UnityEngine.UI;  
  3. using UnityEditor;  
  4.   
  5. using System.Collections;  
  6. using System.Collections.Generic;  
  7.   
  8. //[InitializeOnLoad]  
  9. public class ChangeFontWindow : EditorWindow {  
  10.   
  11.     static ChangeFontWindow() {  
  12.         //toChangeFont = new Font("Arial");  
  13.         //toChangeFontStyle = FontStyle.Normal;  
  14.     }  
  15.       
  16.     [MenuItem("Window/Change Font")]  
  17.     private static void ShowWindow() {  
  18.      ChangeFontWindow cw=   EditorWindow.GetWindow<ChangeFontWindow>(true"Window/Change Font");  
  19.      
  20.     }  
  21.     Font toFont = new Font("Arial");  
  22.     static Font toChangeFont;  
  23.     FontStyle toFontStyle;  
  24.     static FontStyle toChangeFontStyle;  
  25.     private void OnGUI() {  
  26.         GUILayout.Space(10);  
  27.         GUILayout.Label("目标字体:");  
  28.         toFont = (Font)EditorGUILayout.ObjectField(toFont, typeof(Font), true, GUILayout.MinWidth(100f));  
  29.         toChangeFont = toFont;  
  30.         GUILayout.Space(10);  
  31.         GUILayout.Label("类型:");  
  32.         toFontStyle = (FontStyle)EditorGUILayout.EnumPopup(toFontStyle, GUILayout.MinWidth(100f));  
  33.         toChangeFontStyle = toFontStyle;  
  34.         if (GUILayout.Button("修改字体!")) {  
  35.             Change();  
  36.         }  
  37.     }  
  38.     public static void Change() {  
  39.         //获取所有UILabel组件  
  40.         if (Selection.objects == null || Selection.objects.Length==0) return;  
  41.         //如果是UGUI讲UILabel换成Text就可以  
  42.         Object[] labels = Selection.GetFiltered(typeof(Text), SelectionMode.Deep);  
  43.         foreach (Object item in labels) {  
  44.             //如果是UGUI讲UILabel换成Text就可以  
  45.             Text label = (Text)item;  
  46.             label.font = toChangeFont;  
  47.             label.fontStyle = toChangeFontStyle;  
  48.             //label.font = toChangeFont;(UGUI)  
  49.             Debug.Log(item.name + ":" + label.text);  
  50.             //  
  51.             EditorUtility.SetDirty(item);//重要  
  52.         }  
  53.     }  
  54.     private void OnEnable() {  
  55.     }  
  56.       
  57.     private void OnDisable() {  
  58.     }  
  59.       
  60.   
  61.       
  62.     private void Update() {  
  63.     }  
  64.       
  65.     private void OnDestroy() {  
  66.     }  
  67.       
  68. }  
1 0