QMonoBehaviour.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /****************************************************************************
  2. * Copyright (c) 2017 ~2021.1 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. namespace QFramework
  26. {
  27. using UnityEngine;
  28. using System;
  29. using System.Collections.Generic;
  30. public abstract class QMonoBehaviour : MonoBehaviour
  31. {
  32. protected bool mReceiveMsgOnlyObjActive = true;
  33. public void Process (int eventId, params object[] param)
  34. {
  35. if (mReceiveMsgOnlyObjActive && gameObject.activeInHierarchy || !mReceiveMsgOnlyObjActive)
  36. {
  37. var msg = param[0] as IMsg;
  38. ProcessMsg(eventId, msg as QMsg);
  39. msg.Processed = true;
  40. if (msg.ReuseAble)
  41. {
  42. msg.Recycle2Cache();
  43. }
  44. }
  45. }
  46. protected virtual void ProcessMsg (int eventId,QMsg msg) {}
  47. public abstract IManager Manager { get; }
  48. public virtual void Show()
  49. {
  50. gameObject.SetActive (true);
  51. OnShow ();
  52. }
  53. protected virtual void OnShow() {}
  54. public virtual void Hide()
  55. {
  56. OnHide ();
  57. gameObject.SetActive (false);
  58. }
  59. protected virtual void OnHide() {}
  60. protected void RegisterEvents<T>(params T[] eventIDs) where T : IConvertible
  61. {
  62. foreach (var eventId in eventIDs)
  63. {
  64. RegisterEvent(eventId);
  65. }
  66. }
  67. protected void RegisterEvent<T>(T eventId) where T : IConvertible
  68. {
  69. mCachedEventIds.Add(eventId.ToUInt16(null));
  70. Manager.RegisterEvent(eventId, Process);
  71. }
  72. protected void UnRegisterEvent<T>(T eventId) where T : IConvertible
  73. {
  74. mCachedEventIds.Remove(eventId.ToUInt16(null));
  75. Manager.UnRegisterEvent(eventId.ToInt32(null), Process);
  76. }
  77. protected void UnRegisterAllEvent()
  78. {
  79. if (null != mPrivateEventIds)
  80. {
  81. mPrivateEventIds.ForEach(id => Manager.UnRegisterEvent(id,Process));
  82. }
  83. }
  84. public virtual void SendMsg(IMsg msg)
  85. {
  86. Manager.SendMsg(msg);
  87. }
  88. public virtual void SendEvent<T>(T eventId) where T : IConvertible
  89. {
  90. Manager.SendEvent(eventId);
  91. }
  92. private List<ushort> mPrivateEventIds = null;
  93. private List<ushort> mCachedEventIds
  94. {
  95. get { return mPrivateEventIds ?? (mPrivateEventIds = new List<ushort>()); }
  96. }
  97. protected virtual void OnDestroy()
  98. {
  99. if (Application.isPlaying)
  100. {
  101. OnBeforeDestroy();
  102. UnRegisterAllEvent();
  103. }
  104. }
  105. protected virtual void OnBeforeDestroy(){}
  106. }
  107. }