Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using Unity.VisualScripting;
- using System.Globalization;
- using UnityEngine.UIElements;
- using UnityEditor;
- public class TextDev : MonoBehaviour
- {
- [Space(10)]
- [TextArea(2, 2)]
- public string ObjectStats;
- // Assuming you have a TMP_Text component named myText
- [SerializeField]
- private TMP_Text m_TextComponent;
- private Transform m_Transform;
- private TMP_TextInfo m_TextInfo;
- private float m_ScaleMultiplier;
- private float m_HandleSize;
- // Start is called before the first frame update
- void Start()
- {
- if (m_TextComponent == null)
- {
- m_TextComponent = GetComponent<TMP_Text>();
- if (m_TextComponent == null)
- return;
- }
- m_Transform = m_TextComponent.transform;
- // Get a reference to the text object's textInfo
- m_TextInfo = m_TextComponent.textInfo;
- // Update Text Statistics
- ObjectStats =
- "Characters: " + m_TextInfo.characterCount +
- " Words: " + m_TextInfo.wordCount +
- " Spaces: " + m_TextInfo.spaceCount +
- " Sprites: " + m_TextInfo.spriteCount +
- " Links: " + m_TextInfo.linkCount +
- " Lines: " + m_TextInfo.lineCount +
- " Pages: " + m_TextInfo.pageCount;
- // Get the handle size for drawing the various
- m_ScaleMultiplier = m_TextComponent.GetType() == typeof(TextMeshPro) ? 1 : 0.1f;
- m_HandleSize = HandleUtility.GetHandleSize(m_Transform.position) * m_ScaleMultiplier;
- for (int i = 0; i < m_TextInfo.wordCount; i++)
- {
- TMP_WordInfo wInfo = m_TextInfo.wordInfo[i];
- bool isBeginRegion = false;
- Vector3 bottomLeft = Vector3.zero;
- Vector3 topLeft = Vector3.zero;
- Vector3 bottomRight = Vector3.zero;
- Vector3 topRight = Vector3.zero;
- float maxAscender = -Mathf.Infinity;
- float minDescender = Mathf.Infinity;
- Color wordColor = Color.green;
- // Iterate through each character of the word
- for (int j = 0; j < wInfo.characterCount; j++)
- {
- int characterIndex = wInfo.firstCharacterIndex + j;
- TMP_CharacterInfo currentCharInfo = m_TextInfo.characterInfo[characterIndex];
- int currentLine = currentCharInfo.lineNumber;
- bool isCharacterVisible = characterIndex > m_TextComponent.maxVisibleCharacters ||
- currentCharInfo.lineNumber > m_TextComponent.maxVisibleLines ||
- (m_TextComponent.overflowMode == TextOverflowModes.Page && currentCharInfo.pageNumber + 1 != m_TextComponent.pageToDisplay) ? false : true;
- // Track Max Ascender and Min Descender
- maxAscender = Mathf.Max(maxAscender, currentCharInfo.ascender);
- minDescender = Mathf.Min(minDescender, currentCharInfo.descender);
- if (isBeginRegion == false && isCharacterVisible)
- {
- isBeginRegion = true;
- bottomLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.descender, 0);
- topLeft = new Vector3(currentCharInfo.bottomLeft.x, currentCharInfo.ascender, 0);
- //Debug.Log("Start Word Region at [" + currentCharInfo.character + "]");
- // If Word is one character
- if (wInfo.characterCount == 1)
- {
- isBeginRegion = false;
- topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
- bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
- bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
- topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
- // Draw Region
- //DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
- Debug.Log(bottomLeft);
- Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
- }
- }
- // Last Character of Word
- if (isBeginRegion && j == wInfo.characterCount - 1)
- {
- isBeginRegion = false;
- topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
- bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
- bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
- topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
- // Draw Region
- // DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
- Debug.Log(bottomLeft);
- Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
- }
- // If Word is split on more than one line.
- else if (isBeginRegion && currentLine != m_TextInfo.characterInfo[characterIndex + 1].lineNumber)
- {
- isBeginRegion = false;
- topLeft = m_Transform.TransformPoint(new Vector3(topLeft.x, maxAscender, 0));
- bottomLeft = m_Transform.TransformPoint(new Vector3(bottomLeft.x, minDescender, 0));
- bottomRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, minDescender, 0));
- topRight = m_Transform.TransformPoint(new Vector3(currentCharInfo.topRight.x, maxAscender, 0));
- // Draw Region
- //DrawRectangle(bottomLeft, topLeft, topRight, bottomRight, wordColor);
- Debug.Log(bottomLeft);
- Debug.Log("End Word Region at [" + currentCharInfo.character + "]");
- maxAscender = -Mathf.Infinity;
- minDescender = Mathf.Infinity;
- }
- }
- Debug.Log(m_TextInfo.characterInfo);
- //Debug.Log(wInfo.GetWord(m_TextMeshPro.textInfo.characterInfo));
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- void LogTextCharacters()
- {
- if (m_TextComponent != null)
- {
- TMP_TextInfo textInfo = m_TextComponent.textInfo;
- for (int i = 0; i < textInfo.characterCount; i++)
- {
- char character = textInfo.characterInfo[i].character;
- Debug.Log("Character at index " + i + ": " + character);
- }
- }
- else
- {
- Debug.LogWarning("myText is not assigned!");
- }
- }
- }
Add Comment
Please, Sign In to add comment