Modules_TooltipUI.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using System;
  6. namespace SC.XR.Unity.Module_Tooltip
  7. {
  8. [ExecuteAlways]
  9. public class Modules_TooltipUI : MonoBehaviour
  10. {
  11. [TextArea]
  12. [SerializeField]
  13. [Tooltip("Text for the ToolTip to display")]
  14. private string toolTipText;
  15. private string ToolTipText
  16. {
  17. set { toolTipText = value; }
  18. get { return toolTipText; }
  19. }
  20. [SerializeField]
  21. [Range(0f, 100f)]
  22. [Tooltip("Size of text")]
  23. private float textSize;
  24. private float TextSize
  25. {
  26. set { textSize = value; }
  27. get { return textSize; }
  28. }
  29. [SerializeField]
  30. [Range(0f, 100f)]
  31. [Tooltip("The width of lable")]
  32. private float lableWidth;
  33. private float LableWidth
  34. {
  35. set { lableWidth = value; }
  36. get { return lableWidth; }
  37. }
  38. [SerializeField]
  39. [Range(0f, 100f)]
  40. [Tooltip("The height of lable")]
  41. private float lableHeight;
  42. private float LableHeight
  43. {
  44. set { lableHeight = value; }
  45. get { return lableHeight; }
  46. }
  47. [SerializeField]
  48. [Range(0, 0.05f)]
  49. [Tooltip("The scale of lable")]
  50. private float lableScale;
  51. private float LableScale
  52. {
  53. set { lableScale = value; }
  54. get { return lableScale; }
  55. }
  56. private Transform lable;
  57. private BoxCollider boxCollider;
  58. private Transform background;
  59. private TextMeshPro textMeshPro;
  60. private RectTransform textsize;
  61. private bool getready = false;
  62. private void GetReady()
  63. {
  64. lable = transform.Find("Lable");
  65. boxCollider = lable.GetComponent<BoxCollider>();
  66. background = lable.Find("Background");
  67. textMeshPro = GetComponentInChildren<TextMeshPro>();
  68. textsize = gameObject.GetComponentInChildren<RectTransform>();
  69. getready = true;
  70. }
  71. private void OnEnable()
  72. {
  73. GetReady();
  74. }
  75. protected virtual void Update()
  76. {
  77. textMeshPro.text = ToolTipText;
  78. textMeshPro.fontSize = TextSize;
  79. background.localScale = new Vector3(LableWidth, LableHeight, 1);
  80. boxCollider.size = new Vector3(LableWidth, LableHeight, 1);
  81. textsize.sizeDelta = new Vector2(LableWidth, LableHeight);
  82. lable.localScale = new Vector3(LableScale, LableScale, LableScale);
  83. }
  84. public string API_GetLableText()
  85. {
  86. if (!getready)
  87. {
  88. GetReady();
  89. }
  90. return ToolTipText;
  91. }
  92. public void API_SetLableText(string newText)
  93. {
  94. if (!getready)
  95. {
  96. GetReady();
  97. }
  98. ToolTipText = newText;
  99. }
  100. public void API_SetLableTextColor(Color color)
  101. {
  102. if (!getready)
  103. {
  104. GetReady();
  105. }
  106. textMeshPro.color = color;
  107. }
  108. public void API_SetLableTextSize(float size)
  109. {
  110. if (!getready)
  111. {
  112. GetReady();
  113. }
  114. TextSize = size;
  115. }
  116. public void API_SetLableSize(float width, float height)
  117. {
  118. LableWidth = width;
  119. LableHeight = height;
  120. }
  121. public void API_SetLableScale(float scale)
  122. {
  123. LableScale = scale;
  124. }
  125. }
  126. }