본문 바로가기
프로그래밍/Unity

Text To TextMeshProUGUI

by neive 2024. 5. 14.
728x90

 

 

오딘 인스펙터를 사용해서 인스펙터상에

서 버튼을 간편하게 추가해서 사용했기

때문에 오딘 인스펙터를 안쓰는 분들은

다른 방식으로 구현이 필요 합니다

 

우선 리무브를 실행 시키고 에드 버튼을 

통해서 복구하는...

 


예제

using UnityEngine;
using System.Collections.Generic;
using TMPro;
using UnityEngine.UI;
using Sirenix.OdinInspector;

public class TextToTextMeshProUGUI : MonoBehaviour
{
    private class TextInfo
    {
        public string Text;
        public Color Color;
        public bool RaycastTarget;
        public int FontSize;
        public RectTransform RectTransform;
        public TextAnchor Alignment;

        public TextInfo(Text textComponent)
        {
            Text = textComponent.text;
            Color = textComponent.color;
            RaycastTarget = textComponent.raycastTarget;
            FontSize = textComponent.fontSize;
            RectTransform = textComponent.GetComponent<RectTransform>();
            Alignment = textComponent.alignment;
        }
    }
    
    private Dictionary<GameObject, TextInfo> m_Dic = new ();
    public List<GameObject> m_List = new ();

    [Button(Name = "Remove Text")]
    public void StoreAndDeleteTextComponents()
    {
        m_Dic.Clear();
        m_List.Clear();
        
        // 하위의 모든 Text Component 찾기
        var textComponents = GetComponentsInChildren<Text>(includeInactive: true);

        foreach (var textComponent in textComponents)
        {
            // Text Component 가 있던 GameObject 와 내용 저장
            var textInfo = new TextInfo(textComponent);
            m_Dic.Add(textComponent.gameObject, textInfo);
            m_List.Add(textComponent.gameObject);

            // Text Component 삭제
            DestroyImmediate(textComponent);
        }
    }

    [Button(Name = "Add TextMeshPro")]
    public void RestoreTextMeshProComponents()
    {
        // 저장 해둔 것을 바탕으로 TextMeshProUGUI 추가
        foreach (var kvp in m_Dic)
        {
            var textInfo = kvp.Value;

            // TextMeshProUGUI 추가
            var tmp = kvp.Key.AddComponent<TextMeshProUGUI>();
            tmp.text = textInfo.Text;
            tmp.color = textInfo.Color;
            tmp.raycastTarget = textInfo.RaycastTarget;
            tmp.fontSize = textInfo.FontSize;
            tmp.rectTransform.sizeDelta = textInfo.RectTransform.sizeDelta;

            // Convert TextAnchor to TextAlignmentOptions
            switch (textInfo.Alignment)
            {
                case TextAnchor.UpperLeft:
                    tmp.alignment = TextAlignmentOptions.TopLeft;
                    break;
                case TextAnchor.UpperCenter:
                    tmp.alignment = TextAlignmentOptions.Top;
                    break;
                case TextAnchor.UpperRight:
                    tmp.alignment = TextAlignmentOptions.TopRight;
                    break;
                case TextAnchor.MiddleLeft:
                    tmp.alignment = TextAlignmentOptions.Left;
                    break;
                case TextAnchor.MiddleCenter:
                    tmp.alignment = TextAlignmentOptions.Center;
                    break;
                case TextAnchor.MiddleRight:
                    tmp.alignment = TextAlignmentOptions.Right;
                    break;
                case TextAnchor.LowerLeft:
                    tmp.alignment = TextAlignmentOptions.BottomLeft;
                    break;
                case TextAnchor.LowerCenter:
                    tmp.alignment = TextAlignmentOptions.Bottom;
                    break;
                case TextAnchor.LowerRight:
                    tmp.alignment = TextAlignmentOptions.BottomRight;
                    break;
            }
        }
    }
}

 

728x90

댓글