Untiy插件组件编写【4】Unity批量替换预设字体 for NGUI

 

 

当美术感觉当前用的字体不是很合适。准备换字体时。那我们需要把预设里用的字体都对他进行修改。

那么这时。如果我们手动去修改。不仅工作量大。而且繁琐。这时我们就可以写插件。把这些繁琐的事情变的轻松而且愉快。

如下图:我写了这几个功能。

断开关联:选择指定的字体。对选中的预设中用到该字体的关联去掉。

然后重新指定关联。用于:一个预设用了多种字体。

1

 

代码简单。如下(如果没用NGUI的可以参考一下。改一下就能用了):

ResourceEdit.cs  放到任意名为:Editor 的文件夹即可。 下同

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;


//%代表ctrl,#代表Shift,&代表Alt
public class ResourceEdit 
{
    [MenuItem("Plugin/打开字体图集选择面板", false, 9)]
    [MenuItem("Assets/Custom/打开字体图集选择面板", false, 0)]
    static public void OpenConnectAtlasPanel()
    {
        EditorWindow.GetWindow<UIOnKeyRelation>(false, "ConnectAtlasPanel", true);
    }
    [MenuItem("Plugin/断开字体关联", false, 9)]
    [MenuItem("Assets/Custom/断开字体关联", false, 1)]
    public static void CorrectionPublicDisconnectFontFunction()
    {
        if (NGUISettings.ambigiousFont == null)
        {
            Debug.LogError("对不起!你没有指定字体!");
        }
        else
        {
            CorrectionPublicDisconnectFont();
        }
    }
    [MenuItem("Plugin/重新指定字体", false, 9)]
    [MenuItem("Assets/Custom/重新指定字体", false, 2)]
    public static void CorrectionPublicFontFunction()
    {
        if (NGUISettings.ambigiousFont == null)
        {
            Debug.LogError("对不起!你没有指定字体!");
        }
        else
        {
            CorrectionOfTheFont();
        }
    }

    [MenuItem("Assets/Custom/替换所有字体", false, 2)]
    public static void CorrectionPublicFontAllFunction()
    {
        if (NGUISettings.ambigiousFont == null)
        {
            Debug.LogError("对不起!你没有指定字体!");
        }
        else
        {
            CorrectionPublicFont(NGUISettings.ambigiousFont as Font, null);
        }
    }


    private static void SaveDealFinishPrefab(GameObject go, string path)
    {
        if (File.Exists(path) == true)
        {
            Object prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
            PrefabUtility.ReplacePrefab(go, prefab);
        }
        else
        {
            PrefabUtility.CreatePrefab(path, go);
        }
    }


    private static void CorrectionOfTheFont()
    {

        CorrectionPublicFont(NGUISettings.ambigiousFont as Font,null);
    }

    private static void CorrectionPublicDisconnectFont()
    {
        CorrectionPublicFont(null, NGUISettings.ambigiousFont as Font);
    }

    private static void CorrectionPublicFont(Font replace, Font matching)
    {
        if (NGUISettings.ambigiousFont == null)
        {
            Debug.LogError("Select Font Is Null...");
            return;
        }
        else
        {
            Object[] selectObjs = Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets);
            foreach (Object selectObj in selectObjs)
            {
                GameObject obj = (GameObject)selectObj;
                if (obj == null || selectObj == null)
                {
                    Debug.LogWarning("ERROR:Obj Is Null !!!");
                    continue;
                }
                string path = AssetDatabase.GetAssetPath(selectObj);
                if (path.Length < 1 || path.EndsWith(".prefab") == false)
                {
                    Debug.LogWarning("ERROR:Folder=" + path);
                }
                else
                {
                    Debug.Log("Selected Folder=" + path);
                    GameObject clone = GameObject.Instantiate(obj) as GameObject;
                    UILabel[] labels = clone.GetComponentsInChildren<UILabel>(true);
                    foreach (UILabel label in labels)
                    {
                        if (label.trueTypeFont == matching)
                        {
                            label.trueTypeFont = replace;
                        }
                    }
                    SaveDealFinishPrefab(clone, path);
                    GameObject.DestroyImmediate(clone);
                    Debug.Log("Connect Font Success=" + path);
                }
            }
            AssetDatabase.Refresh();
        }
    }
}

 

UIOnKeyRelation.cs

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

public class UIOnKeyRelation : EditorWindow
{
    /// <summary> 保存到内存.</summary>
    void OnSelectNewFont(Object obj)
    {
        NGUISettings.ambigiousFont = obj as Font;
        Repaint();
    }
    
    void OnSelectAtlas(Object obj)
    {
        NGUISettings.atlas = obj as UIAtlas;
        Repaint();
    }
    /// <summary> 刷新窗口. </summary>
    void OnSelectionChange() { Repaint(); }

    public static bool IsSetNullFont;

    /// <summary>UI绘制区域.</summary>
    void OnGUI()
    {
        try
        {
            EditorGUIUtility.labelWidth = 80f;
            NGUIEditorTools.DrawHeader("选择断开(关联)字体");
            ComponentSelector.Draw<Font>("Font", (Font)NGUISettings.ambigiousFont, OnSelectNewFont, true, GUILayout.MinWidth(200f));

            //EditorGUIUtility.labelWidth = 160f;
            //NGUIEditorTools.DrawHeader("选择图集");
            //ComponentSelector.Draw<UIAtlas>("UIAtlas", NGUISettings.atlas, OnSelectAtlas, true, GUILayout.MinWidth(200f));

            NGUIEditorTools.DrawSeparator();
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.Message);
            NGUISettings.ambigiousFont = null;
            NGUISettings.atlas = null;
        }
    }
}

以上就是这个插件的全部内容了。很是简单。如果不懂里的的一些方法的含义。可以看下前面的文章。有介绍。

本文链接:

https://bobsong.cn/595.html
1 + 3 =
快来做第一个评论的人吧~