227 lines
7.4 KiB
C#
227 lines
7.4 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text.RegularExpressions;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
/*******************************************************************************
|
|||
|
|
*Create By CG
|
|||
|
|
*Function
|
|||
|
|
*******************************************************************************/
|
|||
|
|
namespace CG.UTility
|
|||
|
|
{
|
|||
|
|
public class TextSymbolWrap : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
// Start is called before the first frame update
|
|||
|
|
public Text textCom;
|
|||
|
|
private string origStr;
|
|||
|
|
private string replaceStr;
|
|||
|
|
private string finalReplaceStr;
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD>еĿո<D5B8><F1A3A8BB>пո<D0BF>Unicode<64><65><EFBFBD><EFBFBD>Ϊ/u0020<32><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>/u00A0<41><30>
|
|||
|
|
public static readonly string Non_breaking_space = "\u00A0";
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>Ϊ<EFBFBD>˲<EFBFBD><CBB2>ƻ<EFBFBD><C6BB><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻƥ<D6BB><C6A5>ָ<EFBFBD><D6B8><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
|
|||
|
|
private readonly string strPunctuation = @"[<5B><><EFBFBD><EFBFBD>?<3F><><EFBFBD><EFBFBD>!<21><>]";
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD>ڴ洢text<78><74><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private System.Text.StringBuilder TempText = null;
|
|||
|
|
|
|||
|
|
/// <20><><EFBFBD>ڴ洢text<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private IList<UILineInfo> TextLine;
|
|||
|
|
|
|||
|
|
private int screenWidth = 0;
|
|||
|
|
private int screenHeight = 0;
|
|||
|
|
//<2F><><EFBFBD>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>Dz<EFBFBD><C7B2><EFBFBD>ԭʼ<D4AD>ı<EFBFBD>
|
|||
|
|
private string endString = "<22><>";
|
|||
|
|
|
|||
|
|
private bool isReplacing = false;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
private void OnEnable()
|
|||
|
|
{
|
|||
|
|
isReplacing = false;
|
|||
|
|
CheckTextComponent();
|
|||
|
|
CheckScreenSizeChange();
|
|||
|
|
ReplaceTextFun();
|
|||
|
|
}
|
|||
|
|
// Update is called once per frame
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
if (CheckScreenSizeChange() == true)
|
|||
|
|
{
|
|||
|
|
if (textCom != null && string.IsNullOrEmpty(origStr) == false)
|
|||
|
|
{
|
|||
|
|
if (textCom != null)
|
|||
|
|
{
|
|||
|
|
textCom.text = origStr;
|
|||
|
|
}
|
|||
|
|
replaceStr = "";
|
|||
|
|
finalReplaceStr = "";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
CheckReplaceText();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool CheckScreenSizeChange()
|
|||
|
|
{
|
|||
|
|
if (Screen.width != screenWidth || Screen.height != screenHeight)
|
|||
|
|
{
|
|||
|
|
screenWidth = Screen.width;
|
|||
|
|
screenHeight = Screen.height;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void CheckTextComponent()
|
|||
|
|
{
|
|||
|
|
if (textCom != null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
textCom = this.gameObject.GetComponent<Text>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void CheckReplaceText()
|
|||
|
|
{
|
|||
|
|
if (textCom == null)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
if (CheckTextIsChange() == false)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
ReplaceTextFun();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ReplaceTextFun()
|
|||
|
|
{
|
|||
|
|
if (isReplacing == true)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
replaceStr = "";
|
|||
|
|
finalReplaceStr = "";
|
|||
|
|
StartCoroutine("ClearUpPunctuationMode", textCom);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private bool CheckTextIsChange()
|
|||
|
|
{
|
|||
|
|
if (textCom == null)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
string txt = textCom.text;
|
|||
|
|
if (string.Equals(txt, finalReplaceStr) == true)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
IEnumerator ClearUpPunctuationMode(Text _component)
|
|||
|
|
{
|
|||
|
|
isReplacing = true;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̾ͽ<CCBE><CDBD>м<EFBFBD><D0BC>㣬Ҫ<E3A3AC><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD>һ֡<D2BB>ż<EFBFBD><C5BC>㣬<EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3><EFBFBD>60<36><30><EFBFBD><EFBFBD>
|
|||
|
|
yield return new WaitForSeconds(0.06f);
|
|||
|
|
|
|||
|
|
if (string.IsNullOrEmpty(_component.text))
|
|||
|
|
{
|
|||
|
|
isReplacing = false;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ӵĻ<D3B5><C4BB>з<EFBFBD><D0B7><EFBFBD>
|
|||
|
|
//_component.text = _component.text.Replace(" ", string.Empty);
|
|||
|
|
string tempTxt = _component.text;
|
|||
|
|
bool isOrigStr = false;
|
|||
|
|
if (tempTxt[tempTxt.Length - 1].ToString() != endString)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βû<CEB2>пհ<D0BF><D5B0>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ԭʼ<D4AD><CABC><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷֱ<DAB7><D6B1>ʸı<CAB8>ʱ<EFBFBD>ٴμ<D9B4><CEBC><EFBFBD>
|
|||
|
|
origStr = tempTxt;
|
|||
|
|
isOrigStr = true;
|
|||
|
|
}
|
|||
|
|
TextLine = _component.cachedTextGenerator.lines;
|
|||
|
|
//<2F><>Ҫ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
int ChangeIndex = -1;
|
|||
|
|
TempText = new System.Text.StringBuilder(_component.text);
|
|||
|
|
for (int i = 1; i < TextLine.Count; i++)
|
|||
|
|
{
|
|||
|
|
//<2F><>λ<EFBFBD>Ƿ<EFBFBD><C7B7>б<EFBFBD><D0B1><EFBFBD>
|
|||
|
|
UILineInfo lineInfo = TextLine[i];
|
|||
|
|
int startCharIdx = lineInfo.startCharIdx;
|
|||
|
|
if (TempText.Length <= startCharIdx)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
bool IsPunctuation = Regex.IsMatch(TempText[startCharIdx].ToString(), strPunctuation);
|
|||
|
|
//<2F><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>пոijɲ<C4B3><C9B2><EFBFBD><EFBFBD>пո<D0BF><D5B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD>пո<D0BF><D5B8><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>±<EFBFBD>
|
|||
|
|
if (TempText[TextLine[i].startCharIdx].ToString() == Non_breaking_space)
|
|||
|
|
{
|
|||
|
|
IsPunctuation = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//û<>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
|
|||
|
|
if (!IsPunctuation)
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//<2F>б<EFBFBD><D0B1><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>浱ǰ<E6B5B1>±<EFBFBD>
|
|||
|
|
ChangeIndex = TextLine[i].startCharIdx;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>жϵ<D0B6><CFB5>Ѿ<EFBFBD><D1BE><EFBFBD>ǰһ<C7B0><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>DZ<EFBFBD><C7B1><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ַ<EFBFBD><D6B7>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
|
while (IsPunctuation)
|
|||
|
|
{
|
|||
|
|
ChangeIndex = ChangeIndex - 1;
|
|||
|
|
if (ChangeIndex < 0) break;
|
|||
|
|
|
|||
|
|
IsPunctuation = Regex.IsMatch(TempText[ChangeIndex].ToString(), strPunctuation);
|
|||
|
|
//<2F><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>пոijɲ<C4B3><C9B2><EFBFBD><EFBFBD>пո<D0BF><D5B8><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>Dz<EFBFBD><C7B2><EFBFBD><EFBFBD>пո<D0BF><D5B8><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>±<EFBFBD>
|
|||
|
|
if (TempText[ChangeIndex].ToString() == Non_breaking_space)
|
|||
|
|
{
|
|||
|
|
IsPunctuation = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
if (ChangeIndex < 0) continue;
|
|||
|
|
|
|||
|
|
if (TempText[ChangeIndex - 1] != '\n')
|
|||
|
|
TempText.Insert(ChangeIndex, "\n");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
replaceStr = TempText.ToString();
|
|||
|
|
if (string.Equals(tempTxt, replaceStr) == false)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>text<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD>֤<EFBFBD><D6A4><EFBFBD>иĶ<D0B8><C4B6><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|||
|
|
//<2F><>Ϊ<EFBFBD>п<EFBFBD><D0BF><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>뻻<EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵط<C4B5><D8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
if (isOrigStr)
|
|||
|
|
{
|
|||
|
|
replaceStr += endString;
|
|||
|
|
}
|
|||
|
|
_component.text = replaceStr;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD>͵<EFBFBD>ǰtext<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>һ<EFBFBD>£<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>ǰtext<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ѿ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>иı<D0B8>
|
|||
|
|
finalReplaceStr = replaceStr;
|
|||
|
|
}
|
|||
|
|
isReplacing = false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|