• 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, EditorStyles.textField);
    407             _LineStartWidth += 310;
    408 
    409             if (GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 80, 18), "打开文件"))
    410             {
    411                 fontSizeFilePath = EditorUtility.OpenFilePanel("Open file directory", "./", "txt");
    412             }
    413             _LineStartWidth = startWidth + 40;
    414             _LineStartHeight += 20;
    415             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 30), fontSizeFilePathSelectInfo, EditorStyles.boldLabel);
    416 
    417             _LineStartWidth = startWidth + 40;
    418             _LineStartHeight += 15;
    419             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 300, 20), "字号对应文件格式为:序号,老字号,新字号", EditorStyles.boldLabel);
    420 
    421             _LineStartWidth = startWidth + 40;
    422             _LineStartHeight += 20;
    423             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 20), "示例:", EditorStyles.boldLabel);
    424             _LineStartWidth += 30;
    425             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 20), "1,20,30", EditorStyles.boldLabel);
    426             _LineStartWidth = startWidth + 70;
    427             _LineStartHeight += 20;
    428             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 20), "2,25,40", EditorStyles.boldLabel);
    429 
    430             _LineStartWidth = startWidth + 70;
    431             _LineStartHeight += 20;
    432             EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 20), "3,18,28", EditorStyles.boldLabel);
    433 
    434             //_LineStartWidth = startWidth;
    435             //_LineStartHeight += 30;
    436 
    437             //EditorGUI.LabelField(new Rect(_LineStartWidth, _LineStartHeight, 150, 150), "选择新Prefab保存目录", EditorStyles.boldLabel);
    438 
    439             //_LineStartWidth = startWidth;
    440             //_LineStartHeight += 20;
    441             //newPrefabSaveDir = EditorGUI.TextArea(new Rect(_LineStartWidth, _LineStartHeight, 300, 20), newPrefabSaveDir, EditorStyles.textField);
    442             //_LineStartWidth += 310;
    443             //if (GUI.Button(new Rect(_LineStartWidth, _LineStartHeight, 80, 18), "选择目录"))
    444             //{
    445             //    newPrefabSaveDir = EditorUtility.SaveFolderPanel("Save newPrefab to directory", "./", "");
    446             //}
    447 
    448             _LineStartWidth = startWidth;
    449             _LineStartHeight += 30;
    450             EditorGUI.LabelField(new Rect(startWidth, _LineStartHeight, 150, 30), "6、点击按钮替换文字信息", EditorStyles.boldLabel);
    451 
    452             _LineStartWidth = startWidth + 40;
    453             _LineStartHeight += 30;
    454 
    455             if (GUI.Button( new Rect(_LineStartWidth , _LineStartHeight,300, 36),"开始替换"))
    456             {
    457                 ReplaceFontAndFontSize();
    458             }
    459         }
    460         catch (System.Exception ex)
    461         {
    462             Debug.LogError(ex.Message + objpath_test);
    463             previousFont = null;
    464             newFont = null;
    465             fontSizeFilePath = "file name";
    466             
    467         }
    468     }
    469 
    470     private void ReplaceFontAndFontSize()
    471     {
    472         if (previousFont == null)
    473         {
    474             previourFontSelectInfo = "未选择需要替换的字体,请选择...";
    475             Debug.LogError(previourFontSelectInfo);
    476             return;
    477         }
    478 
    479         if (newFont == null)
    480         {
    481             newFontSelectInfo = "未选择新字体,字体将置空";
    482             Debug.LogWarning(newFontSelectInfo);
    483         }
    484 
    485         if (fontSizeFilePath.Equals("") || fontSizeFilePath.Equals("file name"))
    486         {
    487             fontSizeFilePathSelectInfo = "你没有选择对应字号文件,将不替换字号 ";
    488             Debug.LogWarning(fontSizeFilePathSelectInfo);
    489             CorrectionPublicFont( newFont, previousFont, false);
    490         }
    491         else
    492         {
    493             //读取文件内容 
    494             StreamReader sr = new StreamReader(fontSizeFilePath, Encoding.Default);
    495 
    496             string strLine = null;
    497             fontSizeArray.Clear();
    498             while ((strLine = sr.ReadLine()) != null)
    499             {
    500                 if (!string.IsNullOrEmpty(strLine))
    501                 {
    502                     string[] newArray = strLine.Split(',');
    503                     fontSizeArray.Add(newArray);
    504                 }
    505 
    506             }
    507             sr.Close();
    508             // 具体替换 
    509             CorrectionPublicFont(newFont, previousFont, true);
    510         }
    511         StaticInitData();
    512         InitData();
    513     }
    514 
    515     private void CorrectionPublicFont(Font replace, Font matching, bool isReplaceFontSize)
    516     {
    517         foreach (GameObject obj in allNeedReplacePrefab)
    518         {
    519             // 直接修改prefab  
    520             UILabel[] labels = obj.GetComponentsInChildren<UILabel>(true);
    521             int newFontSize = 0;
    522             foreach (UILabel label in labels)
    523             {
    524                 if (label.trueTypeFont == matching)
    525                 {
    526                     label.trueTypeFont = replace;
    527                     newFontSize = GetNewFontSizeByOldSize(label.fontSize);
    528                     label.fontSize = newFontSize;
    529                 }
    530             }
    531             UIPopupList[] poplists = obj.GetComponentsInChildren<UIPopupList>(true);
    532             foreach (UIPopupList popListItem in poplists)
    533             {
    534                 if (popListItem.trueTypeFont == matching)
    535                 {
    536                     //    NGUISettings.ambigiousFont = replace;
    537                     popListItem.trueTypeFont = replace;
    538                     newFontSize = GetNewFontSizeByOldSize(popListItem.fontSize);
    539                     popListItem.fontSize = newFontSize;
    540                 }
    541             }
    542             EditorUtility.SetDirty(obj);
    543             /**** 创建新prefab 失败  ******/
    544             /****
    545             GameObject clone = GameObject.Instantiate(obj) as GameObject;
    546             UILabel[] labels = clone.GetComponentsInChildren<UILabel>(true);
    547             int newFontSize = 0;
    548             foreach (UILabel label in labels)
    549             {
    550                 if (label.trueTypeFont == matching)
    551                 {
    552                     label.trueTypeFont = replace;
    553                     newFontSize = GetNewFontSizeByOldSize(label.fontSize);
    554                     label.fontSize = newFontSize;
    555                 }
    556             }
    557             UIPopupList[] poplists = clone.GetComponentsInChildren<UIPopupList>(true);
    558             foreach (UIPopupList popListItem in poplists)
    559             {
    560                 if (popListItem.trueTypeFont == matching)
    561                 {
    562                     //    NGUISettings.ambigiousFont = replace;
    563                     popListItem.trueTypeFont = replace;
    564                     newFontSize = GetNewFontSizeByOldSize(popListItem.fontSize);
    565                     popListItem.fontSize = newFontSize;
    566                 }
    567             }
    568             SaveDealFinishPrefab(clone, path);
    569             GameObject.DestroyImmediate(clone);
    570              ******/
    571             Debug.Log("Connect Font Success=" + obj.name);
    572 
    573         }
    574         EditorApplication.SaveAssets();
    575         AssetDatabase.SaveAssets();
    576         AssetDatabase.Refresh();
    577     }
    578     private int GetNewFontSizeByOldSize(int fontSize)
    579     {
    580         int newFontSize = fontSize;
    581         if (fontSizeArray.Count > 0 )
    582         {
    583             int nRow = fontSizeArray.Count;
    584             int nCol = ((string[])fontSizeArray[0]).Length;
    585             for ( int i = 0; i < nRow; i++ )
    586             {
    587                 string[] data = (string[])fontSizeArray[i];
    588                 for (int j = 0; j < nCol;j++ )
    589                 {
    590                     if (fontSize == int.Parse(data[1]))
    591                     {
    592                         newFontSize = int.Parse(data[2]);
    593                         return newFontSize;
    594                     }
    595                 }
    596                 
    597             }
    598         }
    599         return newFontSize;
    600     }
    601     //private void SaveDealFinishPrefab(GameObject go, string path)
    602     //{
    603     //    if (File.Exists(path) == true)
    604     //    {
    605     //        UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
    606     //        PrefabUtility.ReplacePrefab(go, prefab);
    607     //    }
    608     //    else
    609     //    {
    610     //        PrefabUtility.CreatePrefab(path, go);
    611     //    }
    612     //}
    613 }

    直接复制到代码内,然后放在Editor下,就可以用辣

    至于步骤,在界面上说的也很清楚了。不明白的,怪自己智商咯。

  • 相关阅读:
    内存映射mmap的几个api及其使用
    hiredis的安装
    Linux 下解压大全
    redis内存数据库C客户端hiredis API 中文说明
    C/C++使用MySQL
    搜索引擎的缓存(cache)机制
    快速排序(QuickSort)
    冒泡排序
    spring核心之AOP学习总结一
    Spring学习总结六——SpringMVC一
  • 原文地址:https://www.cnblogs.com/slysky/p/5340629.html
Copyright © 2020-2023  润新知