본문 바로가기
프로그래밍/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> dictionary = new ();
    public List<GameObject> displayList = new ();

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

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

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

    [Button(Name = "Add TextMeshPro")]
    public void RestoreTextMeshProComponents()
    {
        // 저장 해둔 것을 바탕으로 TextMeshProUGUI 추가
        foreach (var kvp in dictionary)
        {
            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

댓글