Unity 统一替换font字体工具

Selection.GetFiltered

1.Selection.GetFiltered 过滤选中的文件

    常用方法Selection.GetFiltered(typeof(Unity.Object),SelectionMode.Assets),拿到选中的文件路径,可以多选操作,得到Assets目录下的路径名。

 

SelectionMode选择模式

SelectionMode.Unfiltered:

返回整个选择

SelectionMode.TopLevel

仅返回最顶层选择的变换;其他选择的子对象将被过滤出。

SelectionMode.Deep

能返回所有包括文件夹和预制件的集合

SelectionMode.ExcludePrefab

从选择中排除任何预制

SelectionMode.Editable
排除任何对象不得修改。

SelectionMode.Assets

仅返回资源目录中的资源对象。

SelectionMode.DeepAssets
如果选择包含文件夹,也包含所有资源和子目录。

 

2.EditorUtility.SetDirty 设置已改变

当资源已改变并需要保存到磁盘,Unity内部使用dirty标识来查找。

例如,如果修改一个prefab的MonoBehaviourScriptableObject变量,必须告诉Unity该值已经改变。每当一个属性发生变化,Unity内置组件在内部调用setDirty。MonoBehaviourScriptableObject不自动做这个,因此如果你想值被保存,必须调用SetDirty。

 

例子:修改选中文件夹下Prefab字体

 

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

/// <summary>
/// ui工具
/// </summary>
public class ExchangeFont: EditorWindow
{
    [MenuItem("UnityExt/UI/更换字体")]
    public static void Open()
    {
        EditorWindow.GetWindow(typeof(ExchangeFont), true);
    }

    public Font SelectOldFont;
    static Font OldFont;

    public Font SelectNewFont;
    static Font NewFont;

    static float NewLineSpacing;

    private void OnGUI()
    {
        SelectOldFont = (Font)EditorGUILayout.ObjectField("请选择想更换的字体",SelectOldFont,typeof(Font),true,GUILayout.MinWidth(100));
        OldFont = SelectOldFont;
        SelectNewFont = (Font)EditorGUILayout.ObjectField("请选择新的字体", SelectNewFont, typeof(Font), true, GUILayout.MinWidth(100));
        NewFont = SelectNewFont;
        NewLineSpacing = 1;
        NewLineSpacing = EditorGUILayout.FloatField("新行间距", NewLineSpacing);
        
        if (GUILayout.Button("更换选中的预制体"))
        {
            if(SelectOldFont==null||SelectNewFont==null)
            {
                Debug.LogError("请选择字体!");
            }
            else
            {
                Change();
            }
        }
        if(GUILayout.Button("更换文件夹下所有的预制体"))
        {
            if (SelectOldFont == null || SelectNewFont == null)
            {
                Debug.LogError("请选择字体!");
            }
            else
            {
                ChangeSelectFloud();
            }
        }
    }

    public static void Change()
    {
        Object[] Texts = Selection.GetFiltered(typeof(Text), SelectionMode.Deep);
        Debug.Log("找到" + Texts.Length + "个Text,即将处理");
        int count = 0;
        foreach (Object text in Texts)
        {
            if(text)
            {
                Text AimText = (Text)text;
                Undo.RecordObject(AimText, AimText.gameObject.name);
                if(AimText.font == OldFont)
                {
                    AimText.font = NewFont;
                    AimText.lineSpacing = NewLineSpacing;
                    //Debug.Log(AimText.name + ":" + AimText.text);
                    EditorUtility.SetDirty(AimText);
                    count++;
                }
            }
        }
        Debug.Log("字体更换完毕!更换了"+count+"");
    }

    public static void ChangeSelectFloud()
    {
       
        object[] objs = Selection.GetFiltered(typeof(object), SelectionMode.DeepAssets);
        for(int i = 0;i < objs.Length;i++)
        {
            string ext = System.IO.Path.GetExtension(objs[i].ToString());
            if (!ext.Contains(".GameObject"))
            {
                continue;
            }
            GameObject go = (GameObject)objs[i];
            var Texts = go.GetComponentsInChildren<Text>(true);
            int count = 0;
            foreach (Text text in Texts)
            {
                Undo.RecordObject(text, text.gameObject.name);
                if (text.font == OldFont)
                {
                    text.font = NewFont;
                    text.lineSpacing = NewLineSpacing;
                    EditorUtility.SetDirty(text);
                    count++;
                }
            }
            if (count > 0)
            {
                AssetDatabase.SaveAssets();
                Debug.Log(go.name + "界面有:" + count + "个Arial字体");
            }
                
        }
    }

}

 

posted @ 2021-07-08 16:31  奕心1999  阅读(884)  评论(0)    收藏  举报