我的Unity(19)查找替换字体工具

新学到的东西
EditorUtility.DisplayProgressBar (“Hold on”, go.name, (float)(game_list.IndexOf(go)+ 1) / game_list.Count);
EditorUtility.ClearProgressBar ();
出现Unity 进度条

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Text;
using System.Linq;

public class TakeplaceFont : EditorWindow
{
    [MenuItem ("Tools/查找替换字体")]
    public static void SearchTakeplaceFont ()
    {
        GetWindow<TakeplaceFont> ("查找替换字体").Show ();
    }
    private Font targetFont;
    private Font currentFont;

    private bool IsDebugLocal = false;
    private bool IsContainEnmpty = true;
    private string btnName;
    private enum TextType
    {
        Nothing = 0,
        UGUI,
        NGUI
    }
    private TextType textType = TextType.UGUI;

    private bool IsAllChangeFont = true;
    private void OnGUI ()
    {
        EditorGUILayout.LabelField ("功能:将所选择的Prefabs的丢失,默认字体替换成目标字体");
        EditorGUILayout.BeginHorizontal ();
        IsAllChangeFont = EditorGUILayout.Toggle ("字体全部替换",IsAllChangeFont);
        if (!IsAllChangeFont)
        {
            EditorGUILayout.LabelField ("要替换的字体:");
            currentFont = (Font)EditorGUILayout.ObjectField (currentFont, typeof (Font));
        }    
        EditorGUILayout.EndHorizontal ();
        EditorGUILayout.BeginHorizontal ();
        IsContainEnmpty = EditorGUILayout.Toggle ("是否包含Miss的Text", IsContainEnmpty);
        textType = (TextType)EditorGUILayout.EnumPopup ("UGUI或者NGUI", textType);
        EditorGUILayout.EndHorizontal ();
        EditorGUILayout.BeginHorizontal ();
        EditorGUILayout.LabelField ("目标字体");
        targetFont = (Font)EditorGUILayout.ObjectField (targetFont, typeof (Font));
        EditorGUILayout.EndHorizontal ();
        EditorGUILayout.BeginHorizontal ();
        EditorGUILayout.LabelField ("当前选中的物体:");
        EditorGUILayout.EndHorizontal ();
        for (int i = 0; i < Selection.objects.Length; i++)
        {
            EditorGUILayout.ObjectField (Selection.objects[i], typeof (Object));
        }
        EditorGUILayout.BeginHorizontal ();
        if (GUILayout.Button (btnName))
        {
            FindFontAndChange ();
        }
        EditorGUILayout.LabelField ("是否打印修改位置");
        IsDebugLocal = EditorGUILayout.Toggle (IsDebugLocal);
        if (IsDebugLocal)
            btnName = "查找替换";
        else
            btnName = "查找替换并打印";
        EditorGUILayout.EndHorizontal ();
    }

    private void FindFontAndChange ()
    {
        //TODO  包含NGUI和UGUI
        List<GameObject> game_list = Selection.gameObjects.ToList ();
        int count = 0;
        Text[] Text_Arr;
        StringBuilder sb = new StringBuilder ();
        foreach (var go in game_list)
        {
            EditorUtility.DisplayProgressBar ("Hold on", go.name, (float)(game_list.IndexOf(go)+ 1) / game_list.Count);
            Text_Arr = go.GetComponentsInChildren<Text> (true);
            foreach(var item in Text_Arr)
            {
               
                if (IsAllChangeFont)
                {
                    sb.AppendLine (go.name + "/" + item.name + "/" + (item.font==null?"Missing": item.font.name));
                    count++;
                    item.font = targetFont;
                }else
                {
                    if (IsContainEnmpty)
                    {
                        if (item.font == null)
                        {
                            count++;
                            item.font = targetFont;
                            sb.AppendLine (go.name + "/" + item.name + "/" + (item.font == null ? "Missing" : item.font.name));
                        }
                    }
                    if (item.font == currentFont||item.font.name== "Arial")
                    {
                        item.font = targetFont;
                        count++;
                        sb.AppendLine (go.name + "/" + item.name + "/" + (item.font == null ? "Missing" : item.font.name));
                    }
                }              
            }
            EditorUtility.ClearProgressBar ();
        }
        Debug.Log (string.Format("共有{0}处替换完成",count.ToString()));
        Debug.Log (sb.ToString ());
    }
}

效果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m_cainiaokuaifei/article/details/95228777