RichtextAlphaAdapter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine.EventSystems;
  5. namespace UnityEngine.UI
  6. {
  7. [ExecuteInEditMode]
  8. public class RichtextAlphaAdapter : UIBehaviour
  9. {
  10. private Text targetText
  11. {
  12. get
  13. {
  14. if (this.m_TargetText == null)
  15. {
  16. this.m_TargetText = base.GetComponent<Text>();
  17. }
  18. return this.m_TargetText;
  19. }
  20. }
  21. public string inputText
  22. {
  23. get
  24. {
  25. return this.m_InputText;
  26. }
  27. set
  28. {
  29. this.m_InputText = value;
  30. this.UpdateAlpha();
  31. }
  32. }
  33. protected override void OnRectTransformDimensionsChange()
  34. {
  35. base.OnRectTransformDimensionsChange();
  36. if (this.targetText == null)
  37. {
  38. return;
  39. }
  40. this.UpdateAlpha();
  41. }
  42. private void UpdateAlpha()
  43. {
  44. float a = this.targetText.color.a;
  45. string str = this.ConvertTo16(a);
  46. string replacement = "${1}${3}" + str + "${5}${6}</color>";
  47. string text = this.inputText;
  48. text = RichtextAlphaAdapter.regexIndention.Replace(text, string.Empty);
  49. text = RichtextAlphaAdapter.regexBrTag.Replace(text, Environment.NewLine);
  50. text = RichtextAlphaAdapter.regexColor.Replace(text, replacement);
  51. text = RichtextAlphaAdapter.regexSpecialCharacter.Replace(text, new MatchEvaluator(RichtextAlphaAdapter.ReplaceSpecialCharacter));
  52. text = Regex.Replace(text, "</?u>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  53. this.targetText.text = text;
  54. }
  55. private string ConvertTo16(float a)
  56. {
  57. int num = (int)(a * 255f);
  58. string text = Convert.ToString(num, 16);
  59. if (num < 16)
  60. {
  61. text = "0" + text;
  62. }
  63. return text;
  64. }
  65. private static string GetSpecialCharacter(string str)
  66. {
  67. RichtextAlphaAdapter.CreateEntities();
  68. if (!RichtextAlphaAdapter.Entities.ContainsKey(str))
  69. {
  70. Debug.LogWarning(str + "に対応する特殊文字が見つからない");
  71. return string.Empty;
  72. }
  73. return RichtextAlphaAdapter.Entities[str];
  74. }
  75. private static string ReplaceSpecialCharacter(Match m)
  76. {
  77. return RichtextAlphaAdapter.GetSpecialCharacter(m.Value);
  78. }
  79. private static Regex regexColor
  80. {
  81. get
  82. {
  83. if (RichtextAlphaAdapter.m_RegexColor == null)
  84. {
  85. RichtextAlphaAdapter.CreateRegex();
  86. }
  87. return RichtextAlphaAdapter.m_RegexColor;
  88. }
  89. }
  90. private static Regex regexIndention
  91. {
  92. get
  93. {
  94. if (RichtextAlphaAdapter.m_RegexIndention == null)
  95. {
  96. RichtextAlphaAdapter.CreateRegex();
  97. }
  98. return RichtextAlphaAdapter.m_RegexIndention;
  99. }
  100. }
  101. private static Regex regexBrTag
  102. {
  103. get
  104. {
  105. if (RichtextAlphaAdapter.m_RegexBrTag == null)
  106. {
  107. RichtextAlphaAdapter.CreateRegex();
  108. }
  109. return RichtextAlphaAdapter.m_RegexBrTag;
  110. }
  111. }
  112. private static Regex regexSpecialCharacter
  113. {
  114. get
  115. {
  116. if (RichtextAlphaAdapter.m_RegexSpecialCharacter == null)
  117. {
  118. RichtextAlphaAdapter.CreateRegex();
  119. }
  120. return RichtextAlphaAdapter.m_RegexSpecialCharacter;
  121. }
  122. }
  123. private static void CreateRegex()
  124. {
  125. RichtextAlphaAdapter.m_RegexColor = new Regex(RichtextAlphaAdapter.patternColor, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  126. RichtextAlphaAdapter.m_RegexIndention = new Regex(RichtextAlphaAdapter.patternIndention, RegexOptions.Singleline);
  127. RichtextAlphaAdapter.m_RegexBrTag = new Regex(RichtextAlphaAdapter.patternBrTag, RegexOptions.Singleline);
  128. RichtextAlphaAdapter.m_RegexSpecialCharacter = new Regex(RichtextAlphaAdapter.patternSpecialCharacter, RegexOptions.Singleline);
  129. }
  130. private static void CreateEntities()
  131. {
  132. if (RichtextAlphaAdapter.Entities != null)
  133. {
  134. return;
  135. }
  136. RichtextAlphaAdapter.Entities = new SortedDictionary<string, string>();
  137. RichtextAlphaAdapter.Entities.Add("&copy;", "©");
  138. RichtextAlphaAdapter.Entities.Add("&lt;", "<");
  139. RichtextAlphaAdapter.Entities.Add("&gt;", ">");
  140. }
  141. [SerializeField]
  142. private Text m_TargetText;
  143. [SerializeField]
  144. [TextArea(3, 10)]
  145. private string m_InputText = string.Empty;
  146. private static SortedDictionary<string, string> Entities;
  147. private static readonly string patternColor = "(<)(FONT\\s?)?(color='?\"?#[a-f0-9]{6})([a-f0-9]{2})?('?\"?>)(.*?)(</FONT>|</color>)";
  148. private static readonly string patternIndention = "\n|\r";
  149. private static readonly string patternBrTag = "<br>";
  150. private static readonly string patternSpecialCharacter = "(&[a-zA-Z]+?;)";
  151. private static Regex m_RegexColor;
  152. private static Regex m_RegexIndention;
  153. private static Regex m_RegexBrTag;
  154. private static Regex m_RegexSpecialCharacter;
  155. }
  156. }