美文网首页
Unity替换字体小工具

Unity替换字体小工具

作者: 笑烟雨 | 来源:发表于2018-08-27 16:43 被阅读0次

    前一段时间公司要搞字体替换,实在不想做无脑的手动替换工作,就自己写了个替换当前场景下UI字体的小工具,话不多说上硬货。

    using UnityEngine;

    using System.Collections;

    using UnityEditor;

    using UnityEditor.SceneManagement;

    using UnityEngine.UI;

    public class MyText : EditorWindow {

    [MenuItem("Tools/替换字体")]

    public static void Open()

    {

    EditorWindow.GetWindow (typeof(MyText));

    }

    Font change;

    static Font changeFont;

    Font toChange;

    static Font toChangeFont;

    void OnGUI()

    {

    change = (Font)EditorGUILayout.ObjectField ("目标字体",change, typeof(Font), true, GUILayout.MinWidth (100f));

    changeFont = change;

    toChange = (Font)EditorGUILayout.ObjectField ("更换字体",toChange, typeof(Font), true, GUILayout.MinWidth (100f));

    toChangeFont = toChange;

    if (GUILayout.Button ("更换")) 

    {

    ChangeText ();

    }

    }

    public static void ChangeText()

    {

    Transform canvas = GameObject.Find("Canvas").transform;

    if (!canvas)

    {

    Debug.Log("sence no canvas");

    return;

    }

    Transform[] tArray = canvas.GetComponentsInChildren<Transform>();

    for (int i = 0; i < tArray.Length; i++) 

    {

    Text t = tArray [i].GetComponent<Text> ();

    if (t) 

    {

    Undo.RecordObject (t,t.gameObject.name);

    if (t.font == changeFont) 

    {

    t.font = toChangeFont;

    Debug.Log ("替换成功" + t.gameObject.name);

    EditorUtility.SetDirty (t);

    }

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:Unity替换字体小工具

          本文链接:https://www.haomeiwen.com/subject/jqdtwftx.html