Unity Text&TMP互相替换小工具

这篇文章介绍了名为SwitchTextTMP的Unity编辑器插件,它允许用户在Text和TMP_FontAsset对象间转换,并提供了保存和加载设置的功能。
摘要由CSDN通过智能技术生成

Text&TMP替换小工具,工具功能很简单,主要记录下保存编辑器工具数据的实现


SwitchTextTMP.cs:

using TMPro;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.UI;
using ZWaveTools;

public class SwitchTextTMP : EditorWindow
{
    [MenuItem("ZWaveTools/SwitchTextTMP")]
    public static void ShowWindow()
    {
        var window = GetWindowWithRect<SwitchTextTMP>(new Rect(0, 0, 400, 300));
        window.titleContent = new GUIContent("SwitchTextTMP");
        window.Show();
    }
    
    private void OnGUI()
    {
        EditorGUILayout.BeginVertical();
        
        SwitchTextTMPSetting.Instance.textFont = EditorGUILayout.ObjectField("Text字体:",
            SwitchTextTMPSetting.Instance.textFont, typeof(Font), false) as Font;
        SwitchTextTMPSetting.Instance.tmpFont = EditorGUILayout.ObjectField("TMP字体:",
            SwitchTextTMPSetting.Instance.tmpFont, typeof(TMP_FontAsset), false) as TMP_FontAsset;

        if (GUILayout.Button("TMP2Text"))
        {
            TMPToText();
            Debug.Log("TMPToText Complete!!!");
        }

        if (GUILayout.Button("Text2TMP"))
        {
            TextToTMP();
            Debug.Log("TextToTMP Complete!!!");
        }
        
        EditorGUILayout.EndVertical();
    }

    private void OnDisable()
    {
        if (EditorUtility.IsDirty(SwitchTextTMPSetting.Instance))
        {
            if (EditorUtility.DisplayDialog("提示", "未保存设置,是否保存?", "保存", "取消"))
            {
                SaveSetting();
            }
        }
    }

    private void TMPToText()
    {
        var scenes = EditorBuildSettings.scenes;
        foreach (var scene in scenes)
        {
            var activeScene = EditorSceneManager.OpenScene(scene.path);
            bool isChange = false;
            var tmpList = Resources.FindObjectsOfTypeAll<TextMeshProUGUI>();
            foreach (var tmp in tmpList)
            {
                var text = ConvertToText(tmp);
                EditorUtility.SetDirty(text.gameObject);
                if (!isChange)
                    isChange = true;
            }

            if (isChange)
                EditorSceneManager.SaveOpenScenes();
            EditorSceneManager.CloseScene(activeScene, false);
        }

        var guidList = AssetDatabase.FindAssets("t:Prefab");
        foreach (var guid in guidList)
        {
            bool isChange = false;
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var prefabGameObject = PrefabUtility.LoadPrefabContents(path);
            var tmpList = prefabGameObject.GetComponentsInChildren<TextMeshProUGUI>(true);
            foreach (var tmp in tmpList)
            {
                var text = ConvertToText(tmp);
                if (!isChange)
                    isChange = true;
            }

            if (isChange)
            {
                PrefabUtility.SaveAsPrefabAsset(prefabGameObject, path);
                PrefabUtility.UnloadPrefabContents(prefabGameObject);
            }
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
    private void TextToTMP()
    {
        var scenes = EditorBuildSettings.scenes;
        foreach (var scene in scenes)
        {
            var activeScene = EditorSceneManager.OpenScene(scene.path);
            bool isChange = false;
            var textArray = Resources.FindObjectsOfTypeAll<Text>();
            foreach (var text in textArray)
            {
                Debug.Log($"InScene: {scene.path} text: {text.name}");

                var tmp = ConvertToTMP(text);
                

                EditorUtility.SetDirty(tmp.gameObject);
                if (!isChange)
                    isChange = true;
            }

            if (isChange)
                EditorSceneManager.SaveOpenScenes();
            EditorSceneManager.CloseScene(activeScene, false);
        }

        var guidList = AssetDatabase.FindAssets("t:Prefab");
        foreach (var guid in guidList)
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var prefabGameObject = PrefabUtility.LoadPrefabContents(path);
            var textList = prefabGameObject.GetComponentsInChildren<Text>(true);
            bool isChange = false;
            foreach (var text in textList)
            {
                Debug.Log($"InPrefab: {prefabGameObject.name} text: {text.name}");
                var tmp = ConvertToTMP(text);
                

                if (!isChange)
                    isChange = true;
            }

            if (isChange)
            {
                PrefabUtility.SaveAsPrefabAsset(prefabGameObject, path);
                PrefabUtility.UnloadPrefabContents(prefabGameObject);
            }
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
    private void SaveSetting()
    {
        EditorUtility.SetDirty(SwitchTextTMPSetting.Instance);
        AssetDatabase.SaveAssets();
    }
    

    #region CONVERT

    private TextAlignmentOptions ConvertToAlignment(TextAnchor alignment)
    {
        switch (alignment)
        {
            case TextAnchor.UpperLeft:
                return TextAlignmentOptions.TopLeft;
            case TextAnchor.UpperCenter:
                return TextAlignmentOptions.Top;
            case TextAnchor.UpperRight:
                return TextAlignmentOptions.TopRight;
            case TextAnchor.MiddleLeft:
                return TextAlignmentOptions.Left;
            case TextAnchor.MiddleCenter:
                return TextAlignmentOptions.Center;
            case TextAnchor.MiddleRight:
                return TextAlignmentOptions.Right;
            case TextAnchor.LowerLeft:
                return TextAlignmentOptions.BottomLeft;
            case TextAnchor.LowerCenter:
                return TextAlignmentOptions.Bottom;
            case TextAnchor.LowerRight:
                return TextAlignmentOptions.BottomRight;
            default:
                return TextAlignmentOptions.Center;
        }
    }

    private TextAnchor ConvertToAnchor(TextAlignmentOptions alignment)
    {
        switch (alignment)
        {
            case TextAlignmentOptions.TopLeft:
                return TextAnchor.UpperLeft;
            case TextAlignmentOptions.Top:
                return TextAnchor.UpperCenter;
            case TextAlignmentOptions.TopRight:
                return TextAnchor.UpperRight;
            case TextAlignmentOptions.Left:
                return TextAnchor.MiddleLeft;
            case TextAlignmentOptions.Center:
                return TextAnchor.MiddleCenter;
            case TextAlignmentOptions.Right:
                return TextAnchor.MiddleRight;
            case TextAlignmentOptions.BottomLeft:
                return TextAnchor.LowerLeft;
            case TextAlignmentOptions.Bottom:
                return TextAnchor.LowerCenter;
            case TextAlignmentOptions.BottomRight:
                return TextAnchor.LowerRight;
            default:
                return TextAnchor.MiddleCenter;
        }
    }

    private TextMeshProUGUI ConvertToTMP(Text text)
    {
        var gameObject = text.gameObject;
        var content = text.text;
        var fontSize = text.fontSize;
        var alignment = text.alignment;
        var color = text.color;
        var autoSize = text.resizeTextForBestFit;
        var maxFontSize = text.resizeTextMaxSize;
        var minFontSize = text.resizeTextMinSize;

        DestroyImmediate(text, true);

        var tmp = gameObject.AddComponent<TextMeshProUGUI>();
        tmp.text = content;
        tmp.fontSize = fontSize;
        tmp.alignment = ConvertToAlignment(alignment);
        tmp.color = color;
        tmp.enableAutoSizing = autoSize;
        tmp.fontSizeMax = maxFontSize;
        tmp.fontSizeMin = minFontSize;
       
        return tmp;
    }

    private Text ConvertToText(TextMeshProUGUI tmp)
    {
        var gameObject = tmp.gameObject;
        var content = tmp.text;
        var fontSize = tmp.fontSize;
        var alignment = tmp.alignment;
        var color = tmp.color;
        var autoSize = tmp.enableAutoSizing;
        var maxFontSize = tmp.fontSizeMax;
        var minFontSize = tmp.fontSizeMin;

        DestroyImmediate(tmp, true);

        var text = gameObject.AddComponent<Text>();
        text.text = content;
        text.fontSize = (int)fontSize;
        text.alignment = ConvertToAnchor(alignment);
        text.color = color;
        text.resizeTextForBestFit = autoSize;
        text.resizeTextMaxSize = (int)maxFontSize;
        text.resizeTextMinSize = (int)minFontSize;

        return text;
    }

  
    
    
    #endregion
}

SwitchTextTMPSetting.cs

// Author: ZWave
// Time: 2024/01/09 15:01
// --------------------------------------------------------------------------

using System.IO;
using TMPro;
using UnityEditor;
using UnityEngine;

namespace ZWaveTools
{
    public class SwitchTextTMPSetting : ScriptableObject
    {
        private const string SettingPath = "Assets/ZWaveTools/SOConfig/SwitchTextTMPSetting.asset";
        
        
        private static SwitchTextTMPSetting _instance;

        public static SwitchTextTMPSetting Instance
        {
            get
            {
                if (_instance == null)
                {
                    if (File.Exists(SettingPath))
                        _instance = AssetDatabase.LoadAssetAtPath<SwitchTextTMPSetting>(SettingPath);
                    else
                    {
                        _instance = CreateInstance<SwitchTextTMPSetting>();
                        var directoryPath = Path.GetDirectoryName(SettingPath);
                        if (!Directory.Exists(directoryPath))
                        {
                            Directory.CreateDirectory(directoryPath);
                        }
                        AssetDatabase.CreateAsset(_instance, SettingPath);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                    }
                }

                return _instance;
            }
        }
        
        
        public Font textFont;
        public TMP_FontAsset tmpFont;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值