DatePickerText.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Bitsplash.DatePicker;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Bitsplash.DatePicker
  7. {
  8. [ExecuteInEditMode]
  9. public partial class DatePickerText : DatePickerElement
  10. {
  11. //[SerializeField]
  12. private RectTransform contentGameObject = null;
  13. // [HideInInspector]
  14. [SerializeField]
  15. private string text;
  16. [SerializeField]
  17. private Color color = Color.white;
  18. [SerializeField]
  19. private int textSize = 14;
  20. [SerializeField]
  21. private FontStyle fontStyle = FontStyle.Normal;
  22. [SerializeField]
  23. private TextAnchor alignment = TextAnchor.MiddleLeft;
  24. protected DatePickerContent Content { get; set; }
  25. bool mValidate = false;
  26. public RectTransform ContentGameObject
  27. {
  28. get { return contentGameObject; }
  29. set
  30. {
  31. contentGameObject = value;
  32. RecreateTextObject();
  33. }
  34. }
  35. public string Text
  36. {
  37. get { return text; }
  38. set { SetText(value); }
  39. }
  40. public FontStyle FontStyle
  41. {
  42. get { return fontStyle; }
  43. set
  44. {
  45. SetStyle(value);
  46. }
  47. }
  48. public Color Color
  49. {
  50. get { return color; }
  51. set { SetColor(value); }
  52. }
  53. public TextAnchor Alignment
  54. {
  55. get { return alignment; }
  56. set
  57. {
  58. SetAlignment(value);
  59. }
  60. }
  61. public int TextSize
  62. {
  63. get { return textSize; }
  64. set
  65. {
  66. SetTextSize(value);
  67. }
  68. }
  69. DatePickerSettings mMain;
  70. [HideInInspector]
  71. [SerializeField]
  72. UnityEngine.Object mTextObject;
  73. protected override void SetContent(DatePickerContent content)
  74. {
  75. Content = content;
  76. }
  77. protected override void SetMain(DatePickerSettings main)
  78. {
  79. mMain = main;
  80. InnerSetMain();
  81. if(mTextObject == null)
  82. RecreateTextObject();
  83. }
  84. void OnEnable()
  85. {
  86. if(mTextObject == null)
  87. RecreateTextObject(true);
  88. }
  89. void RecreateTextObject(bool overrideCheck = false)
  90. {
  91. if(overrideCheck || isActiveAndEnabled)
  92. StartCoroutine(RecreateTextObjectCorout());
  93. }
  94. partial void DestroyTextMesh();
  95. IEnumerator RecreateTextObjectCorout()
  96. {
  97. bool res = false;
  98. CheckTextMesh(ref res);
  99. if(mValidate)
  100. yield return 0;
  101. if (mTextObject != null || GetComponent<Text>() != null || res == true)
  102. {
  103. CommonMethods.SafeDestroy(mTextObject);
  104. CommonMethods.SafeDestroy(GetComponent<Text>());
  105. DestroyTextMesh();
  106. mTextObject = null;
  107. yield return 0;
  108. }
  109. VerifyTextObject();
  110. }
  111. void VerifyTextObject()
  112. {
  113. if (mMain == null)
  114. return;
  115. InnerVerifyTextObject();
  116. if(mTextObject == null)
  117. {
  118. var obj = gameObject;
  119. if (contentGameObject != null)
  120. obj = contentGameObject.gameObject;
  121. var text = CommonMethods.EnsureComponent<Text>(obj,true);
  122. text.font = mMain.TextFont;
  123. mTextObject = text;
  124. }
  125. ApplyText();
  126. }
  127. void ApplyText()
  128. {
  129. SetText(text);
  130. SetTextSize(textSize);
  131. SetColor(color);
  132. SetAlignment(alignment);
  133. SetStyle(fontStyle);
  134. }
  135. partial void InnerSetMain();
  136. partial void InnerVerifyTextObject();
  137. partial void MediateTextMeshProText(string text);
  138. partial void MediateTextMeshProColor(Color color);
  139. partial void MediateTextMeshAlignment(TextAnchor alignment);
  140. partial void MediateTextMeshSize(int size);
  141. partial void MediateTextMeshStyle(FontStyle style);
  142. partial void CheckTextMesh(ref bool res);
  143. private void SetAlignment(TextAnchor alignment)
  144. {
  145. this.alignment = alignment;
  146. MediateTextMeshAlignment(alignment);
  147. var comp = mTextObject as Text;
  148. if (comp != null)
  149. comp.alignment = alignment;
  150. }
  151. private void SetStyle(FontStyle style)
  152. {
  153. this.fontStyle = style;
  154. MediateTextMeshStyle(style);
  155. var comp = mTextObject as Text;
  156. if (comp != null)
  157. comp.fontStyle = style;
  158. }
  159. private void SetTextSize(int size)
  160. {
  161. this.textSize = size;
  162. MediateTextMeshSize(size);
  163. var comp = mTextObject as Text;
  164. if (comp != null)
  165. comp.fontSize = size;
  166. }
  167. public override void OnValidate()
  168. {
  169. mValidate = true;
  170. bool create = mTextObject != null;
  171. base.OnValidate();
  172. if(create)
  173. RecreateTextObject(false);
  174. ApplyText();
  175. mValidate = false;
  176. }
  177. private void SetText(string text)
  178. {
  179. this.text = text;
  180. MediateTextMeshProText(text);
  181. var comp = mTextObject as Text;
  182. if (comp != null)
  183. comp.text = text;
  184. }
  185. private void SetColor(Color color)
  186. {
  187. this.color = color;
  188. MediateTextMeshProColor(color);
  189. var comp = mTextObject as Text;
  190. if (comp != null)
  191. comp.color = color;
  192. }
  193. }
  194. }