using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; public class FontReplese { //批量替換場景中的字體 [MenuItem("Custom/Label/ChangeSceneLabel")] public static void ChangeSceneFont() { List<UILabel> labelList = NGUIEditorTools.FindAll<UILabel>(); ChangeLabelFont(labelList); } public static void ChangeLabelFont(List<UILabel> labelList) { Font mFont = NGUIEditorTools.LoadAsset<Font>("Assets/font/DroidSansFallback.TTF");//注意這個地方是要替換成的字體的路徑 if (mFont == null) { Debug.LogError(" Font not found ! "); return; } foreach (var label in labelList) { if (label != null && label.trueTypeFont != null && label.trueTypeFont.name == "Arial")//這個地方的name能夠改成原來的字體的名稱 { label.trueTypeFont = mFont; } } Debug.LogError(labelList.Count); } //改變選中的預設上邊的字體 [MenuItem("Custom/Label/ChangeSelectionLabelFont")] public static void ChangeSelectObjFont() { GetTypeList(); //ChangeLabelFont ( ); } public static List<UILabel> GetTypeList() { Object[] objList = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); //Debug.LogError (Selection.activeObject.name); Debug.LogError(objList.Length); List<UILabel> tmpList = new List<UILabel>(); foreach (var tmp in objList) { if ((tmp as GameObject) != null) { string assetstr = AssetDatabase.GetAssetPath(tmp); GameObject obj = PrefabUtility.InstantiatePrefab(tmp) as GameObject; List<UILabel> tmpLabel = GetTypeIncludeChildren(obj); if (tmpLabel.Count > 0) { ChangeLabelFont(tmpLabel); try { PrefabUtility.ReplacePrefab(obj, tmp); } catch (System.Exception ex) { Debug.LogError(ex.ToString() + tmp.name); } //Debug.LogError ( AssetDatabase.RenameAsset ( assetstr, name ) ); } GameObject.DestroyImmediate(obj); } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return tmpList; } public static List<UILabel> GetTypeIncludeChildren(Object obj) { GameObject tmp = obj as GameObject; if (tmp == null) { //return new List<UILabel> (); } List<UILabel> tmpList = new List<UILabel>(); UILabel tmpLabel = tmp.GetComponent<UILabel>(); if (tmpLabel != null) { tmpList.Add(tmpLabel); } foreach (Transform child in tmp.transform) { tmpList.AddRange(GetTypeIncludeChildren(child.gameObject)); } return tmpList; } }
轉載http://blog.csdn.net/oskytonight/article/details/45250071c#