Unity批量替换字体

Unity编辑器   2023-03-28 09:58   1148   0  

webGL好多字体不支持,写一个批量替换字体的小工具,替换成支持显示的

 [MenuItem("GM/批量替换字体 %E")]
        public static void ChangeFonts()
        {
            string newFontPath = "Assets/Font/HuaKangJinGangHei.ttf";
            Font font = AssetDatabase.LoadAssetAtPath<Font>(newFontPath);
            string[] guids = AssetDatabase.FindAssets("t:Prefab");
            for (int i = 0; i < guids.Length; i++)
            {
                bool isChange = false;
                string prefabPath = AssetDatabase.GUIDToAssetPath(guids[i]);
                if (EditorUtility.DisplayCancelableProgressBar($"扫描中{i + 1}/{guids.Length}", prefabPath, (float)(i + 1) / guids.Length))
                {
                    EditorUtility.ClearProgressBar();
                    return;
                }
                GameObject gameObject = PrefabUtility.LoadPrefabContents(prefabPath);
                //获取隐藏的对象上的Text组件,需要在GetComponentsInChildren()函数中添加true参数以查找禁用的对象
                Text[] texts = gameObject.transform.GetComponentsInChildren<Text>(true);
                foreach (var item in texts)
                {
                    if (item.font != font)
                    {
                        item.font = font;
                        isChange = true;
                    }
                }
                if (isChange)
                {
                    PrefabUtility.SaveAsPrefabAsset(gameObject, prefabPath);
                    PrefabUtility.UnloadPrefabContents(gameObject);
                }
            }
            EditorUtility.ClearProgressBar();
            UnityEngine.Debug.Log("Done");

        }


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。
QQ
微信
打赏
扫一扫