123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using UnityEngine.EventSystems;
- namespace UnityEngine.UI
- {
- [ExecuteInEditMode]
- public class RichtextAlphaAdapter : UIBehaviour
- {
- private Text targetText
- {
- get
- {
- if (this.m_TargetText == null)
- {
- this.m_TargetText = base.GetComponent<Text>();
- }
- return this.m_TargetText;
- }
- }
- public string inputText
- {
- get
- {
- return this.m_InputText;
- }
- set
- {
- this.m_InputText = value;
- this.UpdateAlpha();
- }
- }
- protected override void OnRectTransformDimensionsChange()
- {
- base.OnRectTransformDimensionsChange();
- if (this.targetText == null)
- {
- return;
- }
- this.UpdateAlpha();
- }
- private void UpdateAlpha()
- {
- float a = this.targetText.color.a;
- string str = this.ConvertTo16(a);
- string replacement = "${1}${3}" + str + "${5}${6}</color>";
- string text = this.inputText;
- text = RichtextAlphaAdapter.regexIndention.Replace(text, string.Empty);
- text = RichtextAlphaAdapter.regexBrTag.Replace(text, Environment.NewLine);
- text = RichtextAlphaAdapter.regexColor.Replace(text, replacement);
- text = RichtextAlphaAdapter.regexSpecialCharacter.Replace(text, new MatchEvaluator(RichtextAlphaAdapter.ReplaceSpecialCharacter));
- text = Regex.Replace(text, "</?u>", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);
- this.targetText.text = text;
- }
- private string ConvertTo16(float a)
- {
- int num = (int)(a * 255f);
- string text = Convert.ToString(num, 16);
- if (num < 16)
- {
- text = "0" + text;
- }
- return text;
- }
- private static string GetSpecialCharacter(string str)
- {
- RichtextAlphaAdapter.CreateEntities();
- if (!RichtextAlphaAdapter.Entities.ContainsKey(str))
- {
- Debug.LogWarning(str + "に対応する特殊文字が見つからない");
- return string.Empty;
- }
- return RichtextAlphaAdapter.Entities[str];
- }
- private static string ReplaceSpecialCharacter(Match m)
- {
- return RichtextAlphaAdapter.GetSpecialCharacter(m.Value);
- }
- private static Regex regexColor
- {
- get
- {
- if (RichtextAlphaAdapter.m_RegexColor == null)
- {
- RichtextAlphaAdapter.CreateRegex();
- }
- return RichtextAlphaAdapter.m_RegexColor;
- }
- }
- private static Regex regexIndention
- {
- get
- {
- if (RichtextAlphaAdapter.m_RegexIndention == null)
- {
- RichtextAlphaAdapter.CreateRegex();
- }
- return RichtextAlphaAdapter.m_RegexIndention;
- }
- }
- private static Regex regexBrTag
- {
- get
- {
- if (RichtextAlphaAdapter.m_RegexBrTag == null)
- {
- RichtextAlphaAdapter.CreateRegex();
- }
- return RichtextAlphaAdapter.m_RegexBrTag;
- }
- }
- private static Regex regexSpecialCharacter
- {
- get
- {
- if (RichtextAlphaAdapter.m_RegexSpecialCharacter == null)
- {
- RichtextAlphaAdapter.CreateRegex();
- }
- return RichtextAlphaAdapter.m_RegexSpecialCharacter;
- }
- }
- private static void CreateRegex()
- {
- RichtextAlphaAdapter.m_RegexColor = new Regex(RichtextAlphaAdapter.patternColor, RegexOptions.IgnoreCase | RegexOptions.Singleline);
- RichtextAlphaAdapter.m_RegexIndention = new Regex(RichtextAlphaAdapter.patternIndention, RegexOptions.Singleline);
- RichtextAlphaAdapter.m_RegexBrTag = new Regex(RichtextAlphaAdapter.patternBrTag, RegexOptions.Singleline);
- RichtextAlphaAdapter.m_RegexSpecialCharacter = new Regex(RichtextAlphaAdapter.patternSpecialCharacter, RegexOptions.Singleline);
- }
- private static void CreateEntities()
- {
- if (RichtextAlphaAdapter.Entities != null)
- {
- return;
- }
- RichtextAlphaAdapter.Entities = new SortedDictionary<string, string>();
- RichtextAlphaAdapter.Entities.Add("©", "©");
- RichtextAlphaAdapter.Entities.Add("<", "<");
- RichtextAlphaAdapter.Entities.Add(">", ">");
- }
- [SerializeField]
- private Text m_TargetText;
- [SerializeField]
- [TextArea(3, 10)]
- private string m_InputText = string.Empty;
- private static SortedDictionary<string, string> Entities;
- private static readonly string patternColor = "(<)(FONT\\s?)?(color='?\"?#[a-f0-9]{6})([a-f0-9]{2})?('?\"?>)(.*?)(</FONT>|</color>)";
- private static readonly string patternIndention = "\n|\r";
- private static readonly string patternBrTag = "<br>";
- private static readonly string patternSpecialCharacter = "(&[a-zA-Z]+?;)";
- private static Regex m_RegexColor;
- private static Regex m_RegexIndention;
- private static Regex m_RegexBrTag;
- private static Regex m_RegexSpecialCharacter;
- }
- }
|