键盘事件管理器可用于监控玩家输入的按键,做出对应的动作
游戏中有两个关键的容器
keys列表存储了按键
keyDownEvents和keyUpEvents存储了按键以及按键对应的事件
通过keys中存储的按键来快速在字典中找到对应的事件
下面会详细说道流程,不要着急,可以看完了再回过头来看
private List keys;
private Dictionary keyDownEvents, keyUpEvents;
键盘事件管理器中使用了单例模式,下面来展示一下 代码片
。
单例模式。
private static KeyboardEventManager m_Instance;
public static KeyboardEventManager instance
{
get
{
if(m_Instance == null)
{
//FindObjectOfType:在场景中查找一个类对象(需要提前在场景中添加该脚本才能找到)
m_Instance = GameObject.FindObjectOfType(typeof(KeyboardEventManager)) as KeyboardEventManager;
if(m_Instance == null)
{
//创建一个名为KeyboardEventManager Temporary的游戏物体,包含KeyboardEventManager脚本,并且获得该脚本
m_Instance = new GameObject("KeyboardEventManager Temporary", typeof(KeyboardEventManager)).GetComponent();
}
m_Instance.Init();
}
return m_Instance;
}
}
private void Init()
{
keyDownEvents = new Dictionary();
keyUpEvents = new Dictionary();
keys = new List();
}
说完了管理器的初始化,我们接下来来看一看注册和注销的代码片
。(这里只举了按下的代码,弹起的代码同理)
简单来说注册和注销就是对容器的值进行增删
//首先我们在类外面先定义一个委托:
public delegate void KeyEvent(KeyCode k);
---
//键盘按下时的注册
public void RegistrationKeyDown(KeyCode k,KeyEvent kEvent)
{
if (keyDownEvents.ContainsKey(k))
keyDownEvents[k] += kEvent;
else
{
if (!keys.Contains(k)) keys.Add(k);
keyDownEvents.Add(k, kEvent);
}
}
//键盘按下时的注销,如果需要的话
public void UnregisterKeyDown(KeyCode k,KeyEvent kEvent,bool removeKey)
{
if (keyDownEvents.ContainsKey(k))
{
keyDownEvents[k] -= kEvent;
if (keyDownEvents[k] == null)
keyDownEvents.Remove(k);
}
//如果removeKey为真就删除容器中有关该按键的所有内容
if (removeKey) RemoveKey(k);
}
public void RemoveKey(KeyCode k)
{
if (keyDownEvents.ContainsKey(k)) keyDownEvents.Remove(k);
if (keyUpEvents.ContainsKey(k)) keyUpEvents.Remove(k);
if (keys.Contains(k)) keys.Remove(k);
}
接下来我们就对该委托注册方法
void Start()
{
KeyboardEventManager.instance.RegistrationKeyDown(KeyCode.J, new KeyEvent(ComboDown));
KeyboardEventManager.instance.RegistrationKeyDown(KeyCode.K, new KeyEvent(ComboDown));
KeyboardEventManager.instance.RegistrationKeyDown(KeyCode.L, new KeyEvent(ComboDown));
KeyboardEventManager.instance.RegistrationKeyUp(KeyCode.J, new KeyEvent(ComboUp));
KeyboardEventManager.instance.RegistrationKeyUp(KeyCode.K, new KeyEvent(ComboUp));
KeyboardEventManager.instance.RegistrationKeyUp(KeyCode.L, new KeyEvent(ComboUp));
}
private void ComboDown(KeyCode k)
{
if (k == KeyCode.J) print("轻击");
if (k == KeyCode.K) print("跳跃");
if (k == KeyCode.L) print("闪避");
}
private void ComboUp(KeyCode k)
{
if (k == KeyCode.J) print("取消轻击");
if (k == KeyCode.K) print("取消跳跃");
if (k == KeyCode.L) print("取消闪避");
}
最后只需要在update中检测就ok拉
private void Update()
{
foreach(KeyCode key in keys)
{
if (Input.GetKeyDown(key))
OnKeyDown(key);
if (Input.GetKeyUp(key))
OnKeyUp(key);
}
}
private void OnKeyDown(KeyCode k)
{
KeyEvent e = null;
if (keyDownEvents.TryGetValue(k, out e))
if (e != null)
e(k);
}
private void OnKeyUp(KeyCode k)
{
KeyEvent e = null;
if (keyUpEvents.TryGetValue(k, out e))
if (e != null)
e(k);
}
最后贴出这个demo的文件,有需要的就下载看看吧
链接: https://pan.baidu.com/s/1W9yYfwmsaqr5lFPQU4xByA 提取码: gjji 复制这段内容后打开百度网盘手机App,操作更方便哦