SecretPwdModule.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SC.InputSystem;
  5. using System;
  6. public class SecretPwdModule : PointerDelegate
  7. {
  8. [SerializeField]
  9. private int[] pwd;//密码
  10. [SerializeField]
  11. private List<GameObject> Btns;
  12. private List<int> mypwd;
  13. private float lastClickTime;
  14. private bool unlock;//是否解锁密码
  15. public Action changeAction;
  16. void Start()
  17. {
  18. mypwd = new List<int>();
  19. //Test();
  20. }
  21. void OnDestroy()
  22. {
  23. changeAction = null;
  24. }
  25. //是否解锁
  26. public bool UnLock { get { return unlock; } }
  27. //正在解锁
  28. public bool IsChecking { get { return mypwd.Count > 0; } }
  29. protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  30. {
  31. if (API_InputSystem.Target != null)
  32. {
  33. if(API_InputSystem.Target.name == "X")
  34. {
  35. lastClickTime = Time.realtimeSinceStartup;
  36. mypwd.Clear();
  37. unlock = false;
  38. if(changeAction != null)
  39. {
  40. changeAction.Invoke();
  41. }
  42. return;
  43. }
  44. if(unlock)
  45. {
  46. return;//如果已经解锁 就不反复进行
  47. }
  48. if(Btns.IndexOf(API_InputSystem.Target) != -1)
  49. {
  50. lastClickTime = Time.realtimeSinceStartup;
  51. mypwd.Add(int.Parse(API_InputSystem.Target.name));
  52. if(CheckCorrect())
  53. {
  54. CDebug.Log("密码正确");
  55. unlock = true;
  56. if (changeAction != null)
  57. {
  58. changeAction.Invoke();
  59. }
  60. }
  61. }
  62. }
  63. }
  64. private void Test()
  65. {
  66. mypwd = new List<int> { 0,1,0, 0, 1,1, 0, 2, 0, 1, 1,0};
  67. bool res = CheckCorrect();
  68. Debug.Log("测试结果 " + res);
  69. }
  70. private bool CheckCorrect()
  71. {
  72. bool check;
  73. if (mypwd.Count >0)
  74. {
  75. for (int i = 0; i<= mypwd.Count - pwd.Length; i ++)
  76. {
  77. check = true;
  78. for (int j = 0; j< pwd.Length; j ++)
  79. {
  80. if(mypwd[ i +j] != pwd[j])
  81. {
  82. check = false;
  83. break;
  84. }
  85. }
  86. if(check)
  87. {
  88. return true;
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. private void Update()
  95. {
  96. if (Time.realtimeSinceStartup > lastClickTime + 10)
  97. {
  98. mypwd.Clear();//时间超过五秒自动清理
  99. }
  100. }
  101. }