using System; using System.Collections; using Api; using Plugins.CxShine.Singleton; using UnityEngine; namespace Message { public class MessageComp : UnitySingleton { public bool OpenWhile = true; public bool EnterHall = false; public float IntervalTime = 1.0f; // public bool MsgConsume = false; public Action OnBattleRequest; public Action OnBattleAgree; public Action OnBattleStart; public Action OnBattlePlayerReady; public Action OnBattleClientMsg; public Action OnBattlePlayerQuit; private bool end = false; private void Start() { StartCoroutine(ExecuteEveryFewSeconds()); } private void OnDestroy() { OpenWhile = false; Debug.Log("Message Comp Destroy"); } public void ClearMessage() { ApiComp.Instance.ClearMsgs(); } IEnumerator ExecuteEveryFewSeconds() { while (true) { // if (OpenWhile) { // Debug.Log("sync msgs.."); if (EnterHall) { ApiComp.Instance.GetMsgList(data => { foreach (Api.Msg msg in data.msgs) { // Debug.Log(">>>>"+msg.type+">>"+msg.id); if (msg.type == MessageTypes.MsgRequestBattle.GetHashCode()) { OnBattleRequest?.Invoke(msg.msgFromPlayer, msg.content); } else if (msg.type == MessageTypes.MsgBattleAgree.GetHashCode()) { OnBattleAgree?.Invoke(msg.msgFromPlayer, msg.content); } else if (msg.type == MessageTypes.MsgBattleStart.GetHashCode()) { OnBattleStart?.Invoke(msg.content); } else if (msg.type == MessageTypes.OnBattleClientMsg.GetHashCode()) { Debug.Log("zane client msg:" + msg); OnBattleClientMsg?.Invoke(msg.msgFromPlayer, msg.content); } else if (msg.type == MessageTypes.MsgPlayerReady.GetHashCode()) { OnBattlePlayerReady?.Invoke(msg.msgFromPlayer, msg.content); } else if (msg.type == MessageTypes.PlayerQuitInBattle.GetHashCode()) { OnBattlePlayerQuit?.Invoke(msg.msgFromPlayer, msg.content); } ApiComp.Instance.EndMessage(msg.id); } }, (code, msg) => { }); } } // 等待几秒钟 if (end) { yield return null; } else { yield return new WaitForSeconds(IntervalTime); } } } } }