Dieton

TMP Dev Test

Aug 19th, 2023 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.06 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Unity.VisualScripting;
  6. using System.Globalization;
  7. using UnityEngine.UIElements;
  8. using UnityEditor;
  9.  
  10. public class TextDev : MonoBehaviour
  11. {
  12.  
  13.     [Space(10)]
  14.     [TextArea(2, 2)]
  15.     public string ObjectStats;
  16.  
  17.     // Assuming you have a TMP_Text component named myText
  18.     [SerializeField]
  19.     private TMP_Text m_TextComponent;
  20.  
  21.     private Transform m_Transform;
  22.     private TMP_TextInfo m_TextInfo;
  23.  
  24.     private float m_ScaleMultiplier;
  25.     private float m_HandleSize;
  26.  
  27.     // Start is called before the first frame update
  28.     void Start()
  29.     {
  30.         if (m_TextComponent == null)
  31.         {
  32.             m_TextComponent = GetComponent<TMP_Text>();
  33.  
  34.             if (m_TextComponent == null)
  35.                 return;
  36.         }
  37.  
  38.         m_Transform = m_TextComponent.transform;
  39.  
  40.         // Get a reference to the text object's textInfo
  41.         m_TextInfo = m_TextComponent.textInfo;
  42.  
  43.        // Update Text Statistics
  44.         ObjectStats =
  45.         "Characters: " + m_TextInfo.characterCount +
  46.         " Words: " + m_TextInfo.wordCount +
  47.         " Spaces: " + m_TextInfo.spaceCount +
  48.         " Sprites: " + m_TextInfo.spriteCount +
  49.         " Links: " + m_TextInfo.linkCount +
  50.         " Lines: " + m_TextInfo.lineCount +
  51.         " Pages: " + m_TextInfo.pageCount;
  52.  
  53.         // Get the handle size for drawing the various
  54.         m_ScaleMultiplier = m_TextComponent.GetType() == typeof(TextMeshPro) ? 1 : 0.1f;
  55.         m_HandleSize = HandleUtility.GetHandleSize(m_Transform.position) * m_ScaleMultiplier;
  56.  
  57.         for (int i = 0; i < m_TextInfo.wordCount; i++)
  58.         {
  59.             TMP_WordInfo wInfo = m_TextInfo.wordInfo[i];
  60.  
  61.             bool isBeginRegion = false;
  62.  
  63.             Vector3 bottomLeft = Vector3.zero;
  64.             Vector3 topLeft = Vector3.zero;
  65.             Vector3 bottomRight = Vector3.zero;
  66.             Vector3 topRight = Vector3.zero;
  67.  
  68.             float maxAscender = -Mathf.Infinity;
  69.             float minDescender = Mathf.Infinity;
  70.  
  71.             Color wordColor = Color.green;
  72.  
  73.             // Iterate through each character of the word
  74.             for (int j = 0; j < wInfo.characterCount; j++)
  75.             {
  76.                 int characterIndex = wInfo.firstCharacterIndex + j;
  77.                 TMP_CharacterInfo currentCharInfo = m_TextInfo.characterInfo[characterIndex];
  78.                 int currentLine = currentCharInfo.lineNumber;
  79.  
  80.                 bool isCharacterVisible = characterIndex > m_TextComponent.maxVisibleCharacters ||
  81.                                           currentCharInfo.lineNumber > m_TextComponent.maxVisibleLines ||
  82.                                          (m_TextComponent.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
  83.  
  84.                 // Track Max Ascender and Min Descender
  85.                 maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
  86.                 minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
  87.  
  88.                 if (isBeginRegion == false && isCharacterVisible)
  89.                 {
  90.                     isBeginRegion = true;
  91.  
  92.                     bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
  93.                     topLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
  94.  
  95.                     //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
  96.  
  97.                     // If Word is one character
  98.                     if (wInfo.characterCount == 1)
  99.                     {
  100.                         isBeginRegion = false;
  101.  
  102.                         topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  103.                         bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  104.                         bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  105.                         topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  106.  
  107.                         // Draw Region
  108.                         //DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  109.                         Debug.Log(bottomLeft);
  110.                         Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  111.                     }
  112.                 }
  113.  
  114.                 // Last Character of Word
  115.                 if (isBeginRegion && j == wInfo.characterCount - 1)
  116.                 {
  117.                     isBeginRegion = false;
  118.  
  119.                     topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  120.                     bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  121.                     bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  122.                     topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  123.  
  124.                     // Draw Region
  125.                     // DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  126.                     Debug.Log(bottomLeft);
  127.                     Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  128.                 }
  129.                 // If Word is split on more than one line.
  130.                 else if (isBeginRegion && currentLine != m_TextInfo.characterInfo[characterIndex + 1].lineNumber)
  131.                 {
  132.                     isBeginRegion = false;
  133.  
  134.                     topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
  135.                     bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
  136.                     bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
  137.                     topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
  138.  
  139.                     // Draw Region
  140.                     //DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
  141.                     Debug.Log(bottomLeft);
  142.                     Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
  143.                     maxAscender = -Mathf.Infinity;
  144.                     minDescender = Mathf.Infinity;
  145.  
  146.                 }
  147.             }
  148.  
  149.             Debug.Log(m_TextInfo.characterInfo);
  150.             //Debug.Log(wInfo.GetWord(m_TextMeshPro.textInfo.characterInfo));
  151.         }
  152.     }
  153.  
  154.     // Update is called once per frame
  155.     void Update()
  156.     {
  157.        
  158.     }
  159.  
  160.     void LogTextCharacters()
  161.     {
  162.         if (m_TextComponent != null)
  163.         {
  164.             TMP_TextInfo textInfo = m_TextComponent.textInfo;
  165.  
  166.             for (int i = 0; i < textInfo.characterCount; i++)
  167.             {
  168.                 char character = textInfo.characterInfo[i].character;
  169.  
  170.                 Debug.Log("Character at index " + i + ": " + character);
  171.             }
  172.         }
  173.         else
  174.         {
  175.             Debug.LogWarning("myText is not assigned!");
  176.         }
  177.     }
  178. }
  179.  
Tags: Unity tmp
Add Comment
Please, Sign In to add comment