using System.Collections; using System.Collections.Generic; using UnityEngine; using SC.InputSystem; using System; public class SecretPwdModule : PointerDelegate { [SerializeField] private int[] pwd;//密码 [SerializeField] private List Btns; private List mypwd; private float lastClickTime; private bool unlock;//是否解锁密码 public Action changeAction; void Start() { mypwd = new List(); //Test(); } void OnDestroy() { changeAction = null; } //是否解锁 public bool UnLock { get { return unlock; } } //正在解锁 public bool IsChecking { get { return mypwd.Count > 0; } } protected override void partAnyKeyDownDelegate(InputKeyCode keyCode, InputDevicePartBase part) { if (API_InputSystem.Target != null) { if(API_InputSystem.Target.name == "X") { lastClickTime = Time.realtimeSinceStartup; mypwd.Clear(); unlock = false; if(changeAction != null) { changeAction.Invoke(); } return; } if(unlock) { return;//如果已经解锁 就不反复进行 } if(Btns.IndexOf(API_InputSystem.Target) != -1) { lastClickTime = Time.realtimeSinceStartup; mypwd.Add(int.Parse(API_InputSystem.Target.name)); if(CheckCorrect()) { CDebug.Log("密码正确"); unlock = true; if (changeAction != null) { changeAction.Invoke(); } } } } } private void Test() { mypwd = new List { 0,1,0, 0, 1,1, 0, 2, 0, 1, 1,0}; bool res = CheckCorrect(); Debug.Log("测试结果 " + res); } private bool CheckCorrect() { bool check; if (mypwd.Count >0) { for (int i = 0; i<= mypwd.Count - pwd.Length; i ++) { check = true; for (int j = 0; j< pwd.Length; j ++) { if(mypwd[ i +j] != pwd[j]) { check = false; break; } } if(check) { return true; } } } return false; } private void Update() { if (Time.realtimeSinceStartup > lastClickTime + 10) { mypwd.Clear();//时间超过五秒自动清理 } } }