AntiCheatBase.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //检测外挂 非法
  5. public class AntiCheatBase
  6. {
  7. //检测间隔
  8. protected float m_fAntiDeltaTime = 0.0f;
  9. //上一次检测时间
  10. protected float m_fAntiLastTime = AntiCheatCfg.c_fErrorTime;
  11. //是否启用
  12. private bool m_bIsUseed = true;
  13. public void Init(float fAntiDeltaTime)
  14. {
  15. m_fAntiDeltaTime = fAntiDeltaTime;
  16. m_fAntiLastTime = AntiCheatCfg.c_fErrorTime;
  17. #if UNITY_EDITOR
  18. m_bIsUseed = false;//editor 平台关闭
  19. #endif
  20. OnInit();
  21. }
  22. public void SetEnable(bool bIsUse)
  23. {
  24. m_bIsUseed = bIsUse;
  25. }
  26. public void Update()
  27. {
  28. if (m_bIsUseed)
  29. {
  30. float fCurTime = Time.realtimeSinceStartup;
  31. if(m_fAntiLastTime == AntiCheatCfg.c_fErrorTime)
  32. {
  33. m_fAntiLastTime = Time.realtimeSinceStartup;
  34. }
  35. float fDeltaTime = fCurTime - m_fAntiLastTime;
  36. if (fDeltaTime >= m_fAntiDeltaTime)
  37. {
  38. m_fAntiLastTime = fCurTime;
  39. CheckAntiCheat();
  40. }
  41. }
  42. }
  43. public void Dispose()
  44. {
  45. OnDispos();
  46. }
  47. private void CheckAntiCheat()
  48. {
  49. bool bIsCheat = IsCheckAntiCheat();
  50. if (bIsCheat)
  51. OnAntiCheat();
  52. }
  53. //初始化
  54. protected virtual void OnInit()
  55. {
  56. }
  57. //是否使用外挂判断
  58. protected virtual bool IsCheckAntiCheat()
  59. {
  60. return false;
  61. }
  62. //检测到外挂后 处理
  63. protected virtual void OnAntiCheat()
  64. {
  65. }
  66. //销毁
  67. protected virtual void OnDispos()
  68. {
  69. }
  70. }