using System.Collections; using System.Collections.Generic; using UnityEngine; using System; namespace GeneralTools { public class KeyDownManager : SingletonBaseAttribute { public List actionList = new List(); int keyDownActionLevel = 0; public override void IAwake() { actionList = new List(); keyDownActionLevel = ConfigHelper.keyDownActionLevel; } public override void IStart() { AddKeyDownAction(KeyCode.F2, () => { ShowKeyAction(); }, "显示所有绑定的按键指令", false, 5); } private void AddAction(KeyCode _code, Action _action, string _summary, bool _continuousPress, int _level) { if (_action == null) { Debug.Log("设置的" + _code + "action为空"); return; } KeyDownActionClass newAct = new KeyDownActionClass(); newAct.code = _code; newAct.action = _action; newAct.summary = string.IsNullOrEmpty(_summary) ? _action.Target.ToString() : _summary; newAct.continuousPress = _continuousPress; newAct.level = _level >= 0 ? _level : 0; actionList.Add(newAct); } private void AddKeyDownAction(KeyDownActionClass action) { if (action != null) { actionList.Add(action); } } /// /// 设置按键 等级默认0 常规常驻5以上 /// /// /// /// /// public void AddKeyDownAction(KeyCode _code, Action _action, bool _continuousPress = false, int _level = 0) { AddAction(_code, _action, null, _continuousPress, _level); } /// /// 设置按键 /// /// /// /// /// /// public void AddKeyDownAction(KeyCode _code, Action _action, string _summary, bool _continuousPress = false, int _level = 0) { AddAction(_code, _action, _summary, _continuousPress, _level); } private void ShowKeyAction() { for (int i = 0; i < actionList.Count; i++) { string showlog = actionList[i].code + " ---->" + actionList[i].summary + "-----LV:" + actionList[i].level; switch (ConfigHelper.logType) { case 0: Debug.Log(showlog); break; case 1: Debug.LogWarning(showlog); break; case 2: Debug.LogError(showlog); break; case 3: Debug.LogError(showlog); break; default: Debug.Log(showlog); break; } } } private void Update() { for (int i = 0; i < actionList.Count; i++) { if (!actionList[i].continuousPress && Input.GetKeyDown(actionList[i].code) && actionList[i].level >= keyDownActionLevel && actionList[i].level >= 0) { actionList[i].action(); } if (actionList[i].continuousPress && Input.GetKey(actionList[i].code) && actionList[i].level >= keyDownActionLevel && actionList[i].level >= 0) { actionList[i].action(); } } } } } public class KeyDownActionClass { public KeyCode code; public Action action; /// /// 触发等级 /// public int level; public string summary; public bool continuousPress; }