1. 程式人生 > 其它 >Unity 統一替換font字型工具

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。MonoBehaviour
ScriptableObject不自動做這個,因此如果你想值被儲存,必須呼叫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字型"); } } } }