InputNetworkEventProxy.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5. public class InputNetworkEventProxy : IInputProxyBase
  6. {
  7. const int c_msgPoolSize = 5; //网络消息池大小
  8. const int c_connectMsgPool = 2; //网络连接事件池大小
  9. public static void DispatchStatusEvent(NetworkState status)
  10. {
  11. //只有允许输入时才派发事件
  12. if (IsActive)
  13. {
  14. InitPool();
  15. InputNetworkConnectStatusEvent e = GetConnectMsgEvent(status);
  16. InputManager.Dispatch("InputNetworkConnectStatusEvent",e);
  17. }
  18. }
  19. public static void DispatchMessageEvent(string massageType, Dictionary<string, object> data)
  20. {
  21. //只有允许输入时才派发事件
  22. if (IsActive)
  23. {
  24. InitPool();
  25. InputNetworkMessageEvent e = GetMsgEvent();
  26. e.m_MessgaeType = massageType;
  27. e.Data = data;
  28. InputManager.Dispatch("InputNetworkMessageEvent",e);
  29. }
  30. }
  31. #region 事件池
  32. static InputNetworkMessageEvent[] m_msgPool;
  33. static InputNetworkConnectStatusEvent[] m_connectMsgPool;
  34. static int m_connectIndex = 0;
  35. static int m_msgIndex = 0;
  36. static bool isInit = false;
  37. static void InitPool()
  38. {
  39. if (!isInit)
  40. {
  41. isInit = true;
  42. m_connectMsgPool = new InputNetworkConnectStatusEvent[c_connectMsgPool];
  43. for (int i = 0; i < c_connectMsgPool; i++)
  44. {
  45. m_connectMsgPool[i] = new InputNetworkConnectStatusEvent();
  46. }
  47. m_msgPool = new InputNetworkMessageEvent[c_msgPoolSize];
  48. for (int i = 0; i < c_msgPoolSize; i++)
  49. {
  50. m_msgPool[i] = new InputNetworkMessageEvent();
  51. }
  52. }
  53. }
  54. static InputNetworkMessageEvent GetMsgEvent()
  55. {
  56. InputNetworkMessageEvent msg = m_msgPool[m_msgIndex];
  57. msg.Reset();
  58. m_msgIndex++;
  59. if (m_msgIndex >= m_msgPool.Length)
  60. {
  61. m_msgIndex = 0;
  62. }
  63. return msg;
  64. }
  65. static InputNetworkConnectStatusEvent GetConnectMsgEvent(NetworkState status)
  66. {
  67. InputNetworkConnectStatusEvent msg = m_connectMsgPool[m_connectIndex];
  68. msg.Reset();
  69. msg.m_status = status;
  70. m_connectIndex++;
  71. if (m_connectIndex >= m_connectMsgPool.Length)
  72. {
  73. m_connectIndex = 0;
  74. }
  75. return msg;
  76. }
  77. #endregion
  78. }