ユーザ用ツール

サイト用ツール

wiki:unity:asset:ngui:3.9:007

Assets以下のUILabelに設定されたFontを一括変更する(NGUI対応)

「Assets/Resources/Prefabs」以下のUILabelに設定されたFontを一括で設定できるツールです。Editor拡張で作成。

FontReplace.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
 
public class FontReplace : EditorWindow 
{
    private UIFont font;
 
    [MenuItem("Tools/Replace Font")]
    public static void ReplaceFont() {
        EditorWindow.GetWindow(typeof(FontReplace)).Show();
    }    
 
    void OnGUI()
    {
        font = EditorGUILayout.ObjectField("UIFont", font, typeof(UIFont), true) as UIFont;
        if (font != null) {
            if (GUILayout.Button("Replace font in all assets")) {
                string title = "Replacing [" + font.name + "]";
                EachAsset (title,font);
            }
        }
    }
 
    private static void EachAsset(string _title, UIFont _font)
    {        
            string[] sarchDir = { "Assets/Resources/Prefabs"};
        string[] guids = AssetDatabase.FindAssets("", sarchDir);
 
        for (int ii = 0; ii < guids.Length; ii++)
        {
            string guid = guids [ii];
            string guidPath = AssetDatabase.GUIDToAssetPath(guid);
 
            EditorUtility.DisplayProgressBar(_title, guidPath, (float)ii / (float)guids.Length);
 
            GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(guidPath);
 
            if(go != null)
            {
                List<UILabel> uiLabelList = go.GetComponentsInParentAndChildren <UILabel> ();
                for (int jj = 0; jj < uiLabelList.Count; jj++)
                {
                    UILabel uiLabel = uiLabelList[jj];
                    if (uiLabel != null)
                    {
                        uiLabel.bitmapFont = _font;
                        EditorUtility.SetDirty(uiLabel);
                    }
                }
                AssetDatabase.SaveAssets();
            }
        }
        EditorUtility.ClearProgressBar();
    }
 
    public static class GameObjectExtension
    {
        public static T GetComponentInParentAndChildren<T>(this GameObject gameObject) where T : UnityEngine.Component
        {
            if(gameObject.GetComponentInParent<T>() != null)
            {
                return gameObject.GetComponentInParent<T>();
            }
 
            if(gameObject.GetComponentInChildren<T>() != null)
            {
                return gameObject.GetComponentInChildren<T>();
            }
            return gameObject.GetComponent<T>();
        }
 
        public static List<T> GetComponentsInParentAndChildren<T>(this GameObject gameObject) where T : UnityEngine.Component
        {
            List<T> _list = new List<T> (gameObject.GetComponents<T> ());
 
            _list.AddRange (new List<T> (gameObject.GetComponentsInChildren<T> ()));
            _list.AddRange (new List<T> (gameObject.GetComponentsInParent<T> ()));
 
            return _list;
        }
    }
 
}
Permalink wiki/unity/asset/ngui/3.9/007.txt · 最終更新: 2016/10/17 23:20 by step

oeffentlich