Unity 文本解析工具

using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class TextParse : EditorWindow
{
    public static GameObject target;
    static TMP_Text[] TMPTexts;
    static Text[] Texts;
    static GUIStyle wrapStyle;
    Vector2 scrollBarPos;

    TextParse()
    {
        titleContent = new GUIContent("TextPrase");
    }

    [MenuItem("Assets/Text Prase")]
    static void OnClick()
    {
        target = Selection.activeObject as GameObject;
        GetWindow(typeof(TextParse), false, "TextPrase");
        TMPTexts = target.GetComponentsInChildren<TMP_Text>();
        Texts = target.GetComponentsInChildren<Text>();
    }

    private string[] options = new string[] { "TMP文本", "文本", "多语言" };
    private int selectedIndex = 0;

    private void OnGUI()
    {
        if (wrapStyle == null)
        {
            wrapStyle = new GUIStyle(GUI.skin.textArea);
            wrapStyle.wordWrap = true;
            wrapStyle.padding.top = 4;
            wrapStyle.padding.bottom = 4;
            wrapStyle.padding.left = 5;
        }

        if (TMPTexts.Length > 0)
        {
            GUILayout.BeginVertical();
            selectedIndex = EditorGUILayout.Popup("Options", selectedIndex, options);
            scrollBarPos = GUILayout.BeginScrollView(scrollBarPos);

            if (selectedIndex == 0)
                DisplayTMPText();
            else if (selectedIndex == 1)
                DisplayText();
            else if (selectedIndex == 2)
                DisplayMultiLanguageID();

            GUILayout.EndScrollView();
            GUILayout.EndVertical();
        }

        GUILayout.BeginHorizontal();

        if (GUILayout.Button("保存"))
        {
            EditorUtility.SetDirty(target);
            AssetDatabase.SaveAssets();
        }
        GUILayout.EndHorizontal();
    }

    private void DisplayText()
    {
        foreach (var i in Texts)
        {
            GUILayout.Label(new GUIContent(i.name));
            i.text = EditorGUILayout.TextArea(i.text, wrapStyle);
            GUILayout.Space(2);
        }
    }

    void DisplayTMPText()
    {
        foreach (var i in TMPTexts)
        {
            GUILayout.Label(new GUIContent(i.name));
            i.text = EditorGUILayout.TextArea(i.text, wrapStyle);
            GUILayout.Space(2);
        }
    }

    void DisplayMultiLanguageID()
    {
        //foreach (MyText i in TMPTexts)
        //{
        //    GUILayout.Label(new GUIContent(i.name));
        //    var id = i.GetMultiLanguageKey();
        //    id = EditorGUILayout.TextArea(i.GetMultiLanguageKey(), wrapStyle);
        //    i.SetMultiLanguageKey(id);

        //    GUILayout.Space(2);
        //}
    }
}

说明

选中一个预制体,右键Text Parse

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/130064920