//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Tuesday, July 24, 2018 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Snxxz.UI { public class ChatFloatWin : Window { [SerializeField] RectTransform m_ContainerFloat; [SerializeField] RichText m_Chat; [SerializeField] CanvasGroup canvasGroup; DateTime overdueTime = DateTime.Now; Coroutine cacheCoroutine; #region Built-in protected override void BindController() { } protected override void AddListeners() { } protected override void OnPreOpen() { m_ContainerFloat.gameObject.SetActive(false); ChatCtrl.Inst.chatFloatUpdate += ChatFloatUpdate; } protected override void OnAfterOpen() { } protected override void OnPreClose() { ChatCtrl.Inst.chatFloatUpdate -= ChatFloatUpdate; if (cacheCoroutine != null) { StopCoroutine(cacheCoroutine); cacheCoroutine = null; } } protected override void OnAfterClose() { } #endregion protected override void LateUpdate() { base.LateUpdate(); if (DateTime.Now > overdueTime && m_ContainerFloat.gameObject.activeSelf) { m_ContainerFloat.gameObject.SetActive(false); } } private void ChatFloatUpdate(ChatData data) { if (cacheCoroutine != null) { StopCoroutine(cacheCoroutine); cacheCoroutine = null; } if (data == null) { return; } if (string.IsNullOrEmpty(data.content)) { return; } switch (data.type) { case ChatInfoType.Friend: case ChatInfoType.FairyQuestion: case ChatInfoType.FairyTip: case ChatInfoType.TeamTip: return; } m_ContainerFloat.gameObject.SetActive(true); canvasGroup.alpha = 0; m_Chat.text = string.Empty; overdueTime = DateTime.Now.AddTicks(3 * TimeSpan.TicksPerSecond); if (data.infoList != null) { m_Chat.SetExtenalData(data.infoList); } cacheCoroutine = StartCoroutine(Co_SetText(StringUtility.Contact(SetChatExtraInfo(data), data.content))); } IEnumerator Co_SetText(string value) { yield return null; m_Chat.text = value; canvasGroup.alpha = 1; if (m_Chat.preferredWidth >= 1000) { m_Chat.alignment = TextAnchor.MiddleLeft; } else { m_Chat.alignment = TextAnchor.MiddleCenter; } } string SetChatExtraInfo(ChatData data) { switch (data.type) { case ChatInfoType.World: { return string.Format(" {1}: ", "ChatIcon_World", (data as ChatUeseData).name); } case ChatInfoType.Area: return string.Format(" {1}: ", "ChatIcon_Area", (data as ChatUeseData).name); case ChatInfoType.Team: { string playerName = (data as ChatUeseData).name; return string.Format(" {1}", "ChatIcon_Team", playerName + (playerName != string.Empty ? ": " : string.Empty)); } case ChatInfoType.Friend: break; case ChatInfoType.Fairy: { string playerName = (data as ChatUeseData).name; return string.Format(" {1}", "ChatIcon_Fairy", playerName + (playerName != string.Empty ? ": " : string.Empty)); } case ChatInfoType.Trumpet: return string.Format(" {1}: ", "ChatIcon_Trumpet", (data as ChatTrumpetData).name); case ChatInfoType.Invite: return string.Format(" ", "ChatIcon_Invite"); case ChatInfoType.System: return string.Format(" ", "ChatIcon_System"); } return string.Empty; } } }