119 lines
4.2 KiB
C#
119 lines
4.2 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using System;
|
|||
|
|
|
|||
|
|
namespace GeneralTools
|
|||
|
|
{
|
|||
|
|
public class KeyDownManager : SingletonBaseAttribute<KeyDownManager>
|
|||
|
|
{
|
|||
|
|
public List<KeyDownActionClass> actionList = new List<KeyDownActionClass>();
|
|||
|
|
int keyDownActionLevel = 0;
|
|||
|
|
public override void IAwake()
|
|||
|
|
{
|
|||
|
|
actionList = new List<KeyDownActionClass>();
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置按键 等级默认0 常规常驻5以上
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param 按键="_code"></param>
|
|||
|
|
/// <param atcion="_action"></param>
|
|||
|
|
/// <param 连按="continuousPress"></param>
|
|||
|
|
/// <param 按键等级="_level"></param>
|
|||
|
|
public void AddKeyDownAction(KeyCode _code, Action _action, bool _continuousPress = false, int _level = 0)
|
|||
|
|
{
|
|||
|
|
AddAction(_code, _action, null, _continuousPress, _level);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置按键
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param 按键="_code"></param>
|
|||
|
|
/// <param atcion="_action"></param>
|
|||
|
|
/// <param 简介="_summary"></param>
|
|||
|
|
/// <param 连按="continuousPress"></param>
|
|||
|
|
/// <param 按键等级="_level"></param>
|
|||
|
|
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;
|
|||
|
|
/// <summary>
|
|||
|
|
/// 触发等级
|
|||
|
|
/// </summary>
|
|||
|
|
public int level;
|
|||
|
|
public string summary;
|
|||
|
|
public bool continuousPress;
|
|||
|
|
}
|