TMP_ExampleScript_01.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using TMPro;
  5. namespace TMPro.Examples
  6. {
  7. public class TMP_ExampleScript_01 : MonoBehaviour
  8. {
  9. public enum objectType { TextMeshPro = 0, TextMeshProUGUI = 1 };
  10. public objectType ObjectType;
  11. public bool isStatic;
  12. private TMP_Text m_text;
  13. //private TMP_InputField m_inputfield;
  14. private const string k_label = "The count is <#0080ff>{0}</color>";
  15. private int count;
  16. void Awake()
  17. {
  18. // Get a reference to the TMP text component if one already exists otherwise add one.
  19. // This example show the convenience of having both TMP components derive from TMP_Text.
  20. if (ObjectType == 0)
  21. m_text = GetComponent<TextMeshPro>() ?? gameObject.AddComponent<TextMeshPro>();
  22. else
  23. m_text = GetComponent<TextMeshProUGUI>() ?? gameObject.AddComponent<TextMeshProUGUI>();
  24. // Load a new font asset and assign it to the text object.
  25. m_text.font = Resources.Load<TMP_FontAsset>("Fonts & Materials/Anton SDF");
  26. // Load a new material preset which was created with the context menu duplicate.
  27. m_text.fontSharedMaterial = Resources.Load<Material>("Fonts & Materials/Anton SDF - Drop Shadow");
  28. // Set the size of the font.
  29. m_text.fontSize = 120;
  30. // Set the text
  31. m_text.text = "A <#0080ff>simple</color> line of text.";
  32. // Get the preferred width and height based on the supplied width and height as opposed to the actual size of the current text container.
  33. Vector2 size = m_text.GetPreferredValues(Mathf.Infinity, Mathf.Infinity);
  34. // Set the size of the RectTransform based on the new calculated values.
  35. m_text.rectTransform.sizeDelta = new Vector2(size.x, size.y);
  36. }
  37. void Update()
  38. {
  39. if (!isStatic)
  40. {
  41. m_text.SetText(k_label, count % 1000);
  42. count += 1;
  43. }
  44. }
  45. }
  46. }