Unity编辑器下,界面替换NGUI字体以及字号

Posted 阳光下的蒲公英

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity编辑器下,界面替换NGUI字体以及字号相关的知识,希望对你有一定的参考价值。

项目中有需要批量替换字体以及字号的需求,一般也就是多语言处理吧。

提供界面如下:

技术分享

手机拍图,就这样凑合看吧。但是代码不打折。

紧急避让,我只提供修改UILabel以及UIPopupList 下的字体,字号。其他需求的,可以绕远了。还有哈,代码写的很渣渣,因为自己用嘛。还有所有权归我本人所有,一切用于商业用途的,请自己联系博主,上缴费用,谢谢~~~~(赚钱好方法)

 

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

//
public class ReplaceFontEditor
{
    [MenuItem("XmlTest/打开替换字体窗口", false, 10)]
    static void OpenReplaceFontEditor()
    {
        UIReplaceFontEditorWindow.Init();
    }
}
  1 using UnityEngine;
  2 using UnityEditor;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 using System;
  6 using System.IO;
  7 using System.Text;
  8 using LuaInterface;
  9 
 10 public class FontInfo
 11 {
 12     
 13 }
 14 
 15 public class UIReplaceFontEditorWindow : EditorWindow
 16 {
 17     private static UIReplaceFontEditorWindow window;
 18 
 19     private static Dictionary<string, Font> allFonts = new Dictionary<string, Font>();  // key: fontName, value : Font 
 20     private static Dictionary<string, HashSet<int>> allFontsSize = new Dictionary<string, HashSet<int>>(); // key: fontName, value :Hash<int>, Hash<int> 保存所有的字号(去重) 
 21     private static Dictionary<string, Dictionary<int, HashSet<string>>> allFontNameSizePrefabName = new Dictionary<string, Dictionary<int, HashSet<string>>>();  // key : fontName, value: < key:fontSize, value: prefabNames >
 22 
 23     private static List<GameObject> allNeedReplacePrefab = new List<GameObject>();
 24     private static string[] allFontName = new string[8];  // 预先分配
 25     private static string writefileinfo = "字体名称、字号文件(fontsizeinfo.txt)写入状态:空闲状态 ";
 26     // prefab文字信息保存目录  
 27     private static string prefabFontSizeInfoSaveDir = "dir name";
 28     private int ID = -1;
 29     // 待替换字体 
 30     public Font previousFont = null;
 31     private string previourFontSelectInfo = "";
 32     // 新字体 
 33     public Font newFont = null;
 34     private string newFontSelectInfo = "";
 35     // 字体对应文件 
 36     public string fontSizeFilePath = "file name";
 37     private string fontSizeFilePathSelectInfo = "";
 38     // 保存字号对应文件 
 39     private ArrayList fontSizeArray = new ArrayList();
 40     // 新prefab保存目录  
 41     public string newPrefabSaveDir = "dir name";
 42 
 43     private string objpath_test = "";
 44 
 45     public static void Init()
 46     {
 47         // Get existing open window or if none, make a new one:
 48         window = (UIReplaceFontEditorWindow)EditorWindow.GetWindow<UIReplaceFontEditorWindow>("替换字体窗口",true);
 49 
 50         StaticInitData();
 51     }
 52 
 53     static void StaticInitData()
 54     {
 55         allFonts.Clear();
 56         foreach (var item in allFontsSize)
 57         {
 58             item.Value.Clear();
 59         }
 60         allFontsSize.Clear();
 61         allNeedReplacePrefab.Clear();
 62         for (int i = 0; i < allFontName.Length; i++ )
 63         {
 64             allFontName[i] = "";
 65         }
 66         writefileinfo = "字体名称、字号文件(fontsizeinfo.txt)写入状态:空闲状态";
 67         prefabFontSizeInfoSaveDir = "dir name";
 68 
 69 
 70     }
 71 
 72     void InitData()
 73     {
 74         ID = -1;
 75         // 待替换字体 
 76         previousFont = null;
 77         previourFontSelectInfo = "";
 78         // 新字体 
 79         newFont = null;
 80         newFontSelectInfo = "";
 81         // 字体对应文件 
 82         fontSizeFilePath = "file name";
 83         fontSizeFilePathSelectInfo = "";
 84         // 保存字号对应文件 
 85         fontSizeArray.Clear();
 86         // 新prefab保存目录  
 87         newPrefabSaveDir = "dir name";
 88 
 89         objpath_test = "";
 90     }
 91     static void FontInit()
 92     {
 93         // 查找选中的prefab内的所有字体,以及字体所设置的所有字号  
 94 
 95         UnityEngine.Object[] selectObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
 96 
 97         foreach (UnityEngine.Object selectObj in selectObjs)
 98         {
 99             GameObject obj = null;
100             try
101             {
102                 obj = (GameObject)selectObj;
103             }
104             catch
105             {
106                 continue;
107             }
108 
109             if (obj == null || selectObj == null)
110             {
111                 Debug.LogWarning("ERROR:Obj Is Null !!!");
112                 continue;
113             }
114             string objPath = AssetDatabase.GetAssetPath(selectObj);
115            
116             if (objPath.Length < 1 || objPath.EndsWith(".prefab") == false)
117             {
118                 Debug.LogWarning("ERROR:Folder=" + objPath);
119             }
120             else
121             {
122                 string prePath = objPath.Substring(7, objPath.Length - 7).Replace("\\", "/").ToLower();
123 
124                 allNeedReplacePrefab.Add(obj);
125                 Debug.Log("Selected Folder=" + objPath);
126 
127                 // 直接修改prefab  
128                 UILabel[] labels = obj.GetComponentsInChildren<UILabel>(true);
129 
130                 foreach (UILabel label in labels)
131                 {
132                     // 保存字体 以及字号 
133 
134                     if ( label.trueTypeFont == null )
135                     {
136                         Debug.Log("font is null" + prePath);
137                     }
138                     else
139                     {
140                         if (!allFonts.ContainsKey(label.trueTypeFont.name))
141                         {
142                             allFonts.Add(label.trueTypeFont.name, label.trueTypeFont);
143                         }
144                         if (allFontsSize.ContainsKey(label.trueTypeFont.name))
145                         {
146                             allFontsSize[label.trueTypeFont.name].Add(label.fontSize);
147                         }
148                         else
149                         {
150                             allFontsSize.Add(label.trueTypeFont.name, new HashSet<int>());
151                             allFontsSize[label.trueTypeFont.name].Add(label.fontSize);
152                         }
153 
154                         if ( allFontNameSizePrefabName.ContainsKey( label.trueTypeFont.name))
155                         {
156                             if ( allFontNameSizePrefabName[label.trueTypeFont.name].ContainsKey(label.fontSize))
157                             {
158                                 allFontNameSizePrefabName[label.trueTypeFont.name][label.fontSize].Add(objPath);
159                             }
160                             else
161                             {
162                                 allFontNameSizePrefabName[label.trueTypeFont.name].Add(label.fontSize, new HashSet<string>());
163                                 allFontNameSizePrefabName[label.trueTypeFont.name][label.fontSize].Add(objPath);
164                             }
165                         }
166                         else
167                         {
168                             allFontNameSizePrefabName.Add(label.trueTypeFont.name, new Dictionary<int, HashSet<string> >());
169                             allFontNameSizePrefabName[label.trueTypeFont.name].Add(label.fontSize, new HashSet<string>());
170                             allFontNameSizePrefabName[label.trueTypeFont.name][label.fontSize].Add(objPath);
171                         }
172                     }
173                 }
174                 UIPopupList[] poplists = obj.GetComponentsInChildren<UIPopupList>(true);
175                 foreach (UIPopupList popListItem in poplists)
176                 {
177                     // 保存字体 以及字号 
178                     if (popListItem.trueTypeFont == null)
179                     {
180                         Debug.Log("font is null" + prePath);
181                     }
182                     else
183                     {
184                         if (!allFonts.ContainsKey(popListItem.trueTypeFont.name))
185                         {
186                             allFonts.Add(popListItem.trueTypeFont.name, popListItem.trueTypeFont);
187                         }
188                         if (allFontsSize.ContainsKey(popListItem.trueTypeFont.name))
189                         {
190                             allFontsSize[popListItem.trueTypeFont.name].Add(popListItem.fontSize);
191                         }
192                         else
193                         {
194                             allFontsSize.Add(popListItem.trueTypeFont.name, new HashSet<int>());
195                             allFontsSize[popListItem.trueTypeFont.name].Add(popListItem.fontSize);
196                         }
197                     }
198                 }
199             }
200         }
201         int i = 0;
202         foreach ( KeyValuePair<string,Font> kv in allFonts)
203         {
204             allFontName[i] = kv.Key;
205             i++;
206         }
207 
208         if (prefabFontSizeInfoSaveDir.Equals("") || prefabFontSizeInfoSaveDir.Equals("dir name"))
209         {
210             Debug.LogWarning("未选择目录,不保存文字信息");
211             writefileinfo = "未选择文件保存目录,因此不保存字号信息 ";
212         }
213         else
214         {
215             writefileinfo = "字体名称、字号文件(fontsizeinfo.txt)写入状态:正在写入";
216             writefileinfo = WriteFontSizeInfoToFile(prefabFontSizeInfoSaveDir);
217         }
218 
219     }
220 
221     static string WriteFontSizeInfoToFile(string path)
222     {
223 
224         FileStream fs = new FileStream( prefabFontSizeInfoSaveDir + "/fontsizeinfo.txt", FileMode.Create);
225         StreamWriter sw = new StreamWriter(fs);
226         List<int> fontSizeList = new List<int>();
227         sw.WriteLine("字体信息如下,格式为:【 字体名称:所有字号 】 ");
228         // 输出信息  
229         foreach (var item in allFontsSize)
230         {
231             sw.Write(item.Key + ":");
232             fontSizeList.Clear();
233 
234             foreach (var fontsize in item.Value)
235             {
236                 fontSizeList.Add(fontsize);
237                 //sw.Write(fontsize + " ");
238             }
239             fontSizeList.Sort();
240 
241             foreach (var fontsizeInList in fontSizeList)
242             {
243                 sw.Write(fontsizeInList + ",");
244             }
245             sw.WriteLine();
246         }
247         foreach (KeyValuePair<string, Dictionary<int,HashSet<string> > > allItemInfo in allFontNameSizePrefabName)
248         {
249             sw.WriteLine();
250             sw.Write("字体名称(fontName) :" + allItemInfo.Key +"," + "所有字号个数(allFontSizeCount):" + allItemInfo.Value.Count);
251             sw.WriteLine();
252             
253             List<KeyValuePair<int,HashSet<string> > > lst = new List<KeyValuePair<int,HashSet<string>>>(allItemInfo.Value);
254             lst.Sort(delegate(KeyValuePair<int, HashSet<string>> s1, KeyValuePair<int, HashSet<string>> s2)
255             {
256                 return s1.Key.CompareTo(s2.Key);
257             });
258 
259             foreach (var fontSizeAndPrefabName in lst)
260             {
261                 sw.WriteLine();
262                 sw.WriteLine("--字号(fontSize) : " + fontSizeAndPrefabName.Key + " : ");
263                 foreach (var prefabName in fontSizeAndPrefabName.Value)
264                 {
265                     sw.WriteLine("    " + prefabName + ";"); 
266                 }
267             }
268         }
269 
270         sw.Flush();
271         sw.Close();
272         fs.Close();
273         return "字体名称、字号文件(fontsizeinfo.txt)写入状态:保存成功";
274     }
275 
276     /// <summary> 保存.</summary>
277     void OnSelectNewFont(UnityEngine.Object obj)
278     {
279         newFont = obj as Font;
280         //NGUISettings.ambigiousFont = obj;
281         Repaint();
282     }
283 
284     void OnSelectPreviousFont(UnityEngine.Object obj)
285     {
286         previousFont = obj as Font;
287         //NGUISettings.ambigiousFont = obj;
288         Repaint();
289     }
290     void OnSelectAtlas(UnityEngine.Object obj)
291     {
292         NGUISettings.atlas = obj as UIAtlas;
293         Repaint();
294     }
295     /// <summary> 刷新窗口. </summary>
296     void OnSelectionChange() { Repaint(); }
297 
298     public static bool IsSetNullFont;
299 
300     /// <summary>UI绘制区域.</summary>
301     void OnGUI()
302     {
303         try
304         {
305             int startWidth = 10;
306             int startHeight =16;
307             EditorGUIUtility.labelWidth = 100f;
308 
309 
310             int _LineStartWidth = startWidth;
311             int _LineStartHeight = startHeight;
312 
313             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 500, 30), "1、选择Prefab内字体名称、字号信息文件保存目录(文件名默认为fontsizeinfo.txt)", EditorStyles.boldLabel);
314 
315             _LineStartWidth = startWidth + 40;
316             _LineStartHeight += 30;
317             newPrefabSaveDir = EditorGUI.TextArea(new Rect(_LineStartWidth, _LineStartHeight, 300, 20), prefabFontSizeInfoSaveDir, EditorStyles.textField);
318             _LineStartWidth += 310;
319             if (GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 80, 18), "选择目录"))
320             {
321                 prefabFontSizeInfoSaveDir = EditorUtility.SaveFolderPanel("Save newPrefab to directory", "./", "");
322             }
323             _LineStartWidth = startWidth;
324             _LineStartHeight += 30;
325 
326             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 500, 30), "2、点击按钮获取字体信息(若第1步未选择目录,则不保存文件)", EditorStyles.boldLabel);
327 
328             _LineStartWidth = startWidth + 40 ;
329             _LineStartHeight += 30;
330 
331             if (GUI.Button(new Rect(_LineStartWidth , _LineStartHeight, 300, 40), "获取所选Prefab内字体信息并将信息写入文件"))
332             {
333                 FontInit();
334             }
335             _LineStartWidth = startWidth + 40;
336             _LineStartHeight += 40;
337             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 500, 30), writefileinfo, EditorStyles.boldLabel);
338 
339             _LineStartWidth = startWidth;
340             _LineStartHeight += 40;
341             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 30), "3、选择需要替换的字体", EditorStyles.boldLabel);
342            
343             _LineStartWidth = startWidth + 40;
344             _LineStartHeight += 30;
345       
346             ID = EditorGUI.Popup(new Rect(_LineStartWidth, _LineStartHeight, 105, 15), ID, allFontName);
347             if (ID >= 0)
348             {
349                 previousFont = allFonts[allFontName[ID]];
350             }
351 
352             _LineStartWidth += 120;
353             //EditorGUI.TextArea(new Rect(_LineStartWidth, _LineStartHeight, 300, 20), prefabFontSizeInfoSaveDir, EditorStyles.textField);
354             EditorGUI.ObjectField(new Rect(_LineStartWidth, _LineStartHeight, 150, 20), previousFont, typeof(Font), false);
355             _LineStartWidth += 150;
356             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 30), previourFontSelectInfo, EditorStyles.boldLabel);
357             /***
358             _LineStartWidth = startWidth + 40;
359             _LineStartHeight += 40;
360 
361             GUILayout.BeginHorizontal();
362 
363             if (GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 80, 20), "Font       ▽"))
364             {
365                 ComponentSelector.Show<Font>(OnSelectPreviousFont);
366             }
367             _LineStartWidth += 90;
368             previousFont = EditorGUI.ObjectField(new Rect(_LineStartWidth, _LineStartHeight, 200, 20), previousFont, typeof(Font), false) as Font;
369             _LineStartWidth += 210;
370             if (previousFont != null && GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 20, 20), "X"))
371             {
372                 previousFont = null;
373             }
374             GUILayout.EndHorizontal();
375             **/
376             _LineStartWidth = startWidth;
377             _LineStartHeight += 30;
378             EditorGUI.LabelField(new Rect(startWidth, _LineStartHeight, 300, 30), "4、选择新字体(如不选择新字体,则Prefab内字体将置空)", EditorStyles.boldLabel);
379 
380             _LineStartWidth = startWidth;
381             _LineStartHeight += 20;
382             GUILayout.BeginHorizontal();
383             _LineStartWidth = startWidth + 40;
384             if (GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 80, 20), "Font       ▽"))
385             {
386                 ComponentSelector.Show<Font>(OnSelectNewFont);
387             }
388             _LineStartWidth += 90;
389             newFont = EditorGUI.ObjectField(new Rect(_LineStartWidth, _LineStartHeight, 200, 20), newFont, typeof(Font), false) as Font;
390             _LineStartWidth += 210;
391             if (newFont != null && GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 20, 20), "X"))
392             {
393                 newFont = null;
394             }
395             _LineStartWidth += 30;
396             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 30), newFontSelectInfo, EditorStyles.boldLabel);
397             GUILayout.EndHorizontal();
398 
399             _LineStartWidth = startWidth;
400             _LineStartHeight += 30;
401             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 300, 30), "5、选择字号对应文件(不选择文件,则字号不变)", EditorStyles.boldLabel);
402 
403             _LineStartWidth = startWidth + 40;
404             _LineStartHeight += 20;
405             //Rect textRect = new Rect(_LineStartWidth, _LineStartHeight, 300, 150);
406             fontSizeFilePath = EditorGUI.TextArea(new Rect(_LineStartWidth, _LineStartHeight, 300, 20), fontSizeFilePath, EditorStyl

以上是关于Unity编辑器下,界面替换NGUI字体以及字号的主要内容,如果未能解决你的问题,请参考以下文章

谁能帮我用NGUI做个简单的UNITY3D的界面,就是最普通的那种有登陆界面,确认返回,以及切换的3个界面。

Unity 3D 中NGUI插件设置中文label

关于Unity中的NGUI字体

Word已存文档中,将原有内容的字体,字号作相应改变,加标题,插入,替换某符号等word文档中怎么替换步骤

eclipse 界面设置与字体更改

FairyGUI和NGUI对比