using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; public class ChatContex : MonoBehaviour { public Text _ContexText; public bool _IsShowBlackColor = true; public bool _CanShowAllChars = true; #region link base protected string _ModifiedStr; protected bool _HasInitLink = false; void Update() { if (_HasInitLink) return; if (_ContexText == null || string.IsNullOrEmpty(_ContexText.text)) return; if (_CanShowAllChars && _ContexText.cachedTextGenerator.characterCountVisible == _ContexText.text.Length) { SetLinksAfterLayout(); _HasInitLink = true; } else if (!_CanShowAllChars && _ContexText.cachedTextGenerator.characterCountVisible > 0) { SetLinksAfterLayout(); _HasInitLink = true; } } public void OnDisable() { _HasInitLink = false; ClearAllLinks(); } public void ShowLinkStr(string linkStr) { _ModifiedStr = linkStr; _HasInitLink = false; ClearAllLinks(); GetAllChatLinks(); _ModifiedStr = GetLinkedStr(); _ContexText.text = _ModifiedStr; _ContexText.CalculateLayoutInputHorizontal(); _ContexText.CalculateLayoutInputVertical(); } protected List _LinkLists = new List(); protected void ClearAllLinks() { foreach (var link in _LinkLists) { link.ClearLink(); } _LinkLists.Clear(); } protected void GetAllChatLinks() { int checkPos = _ModifiedStr.IndexOf(ChatLinkBase.StrSendStart); int checkEndPos = -1; string checkStr = ""; while (checkPos >= 0) { checkEndPos = _ModifiedStr.IndexOf(ChatLinkBase.StrSendEnd, checkPos); if (checkEndPos < 0) break; checkStr = _ModifiedStr.Substring(checkPos, checkEndPos - checkPos); if (!string.IsNullOrEmpty(checkStr)) { var chatLink = ChatLinkBase.CreateChatLinkBySendStr(_ContexText, null, checkStr, _IsShowBlackColor); chatLink.StartPosInText = checkPos; chatLink.EndPosInText = checkEndPos; _LinkLists.Add(chatLink); } checkPos = _ModifiedStr.IndexOf(ChatLinkBase.StrSendStart, checkEndPos); } } protected string GetLinkedStr() { int subStrStart = 0; int subStrEnd = 0; int newInputEnd = 0; string newInputText = ""; for (int i = 0; i < _LinkLists.Count; ++i) { if (i > 0) { subStrStart = _LinkLists[i - 1].EndPosInText + 1; } subStrEnd = _LinkLists[i].StartPosInText; if (subStrEnd > subStrStart) { newInputText += _ModifiedStr.Substring(subStrStart, subStrEnd - subStrStart); } _LinkLists[i].StartPosInShowText = newInputText.Length; newInputText += _LinkLists[i].StrShow; newInputEnd = _LinkLists[i].EndPosInText + 1; _LinkLists[i].EndPosInShowText = newInputText.Length; } newInputText += _ModifiedStr.Substring(newInputEnd, _ModifiedStr.Length - newInputEnd); return newInputText; } protected void SetLinksAfterLayout() { foreach (var link in _LinkLists) { link.SetLinkAfterLayout(_ContexText); } } #endregion }