在游戏开发中,我们经常用到组合键的使用来控制某个技能,例如:ctrl+6显示图标等。OK,开始着手:
这要用到Unity中的Event事件,它是一个UnityGUI事件,对应于用户输入事件(按键,鼠标行为),或是UnityGUI布局,或渲染事件。对于每个事件OnGUI在脚本中被调用;因此OnGUI每帧潜在被调用多次。Event.current对应于在OnGUI里调用当前事件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestKeyBorad : MonoBehaviour {
private void OnGUI()
{
if(Event.current.rawType==EventType.keyDown)
{
EventCallBack(Event.current);
}
}
private void EventCallBack(Event e)
{
bool eventDown = (e.modifiers & EventModifiers.Control) != 0;
if (!eventDown) return;
e.Use(); //使用这个事件
switch(e.keyCode)
{
case KeyCode.UpArrow:
Debug.Log("按下组合键:ctrl+↑");
break;
case KeyCode.DownArrow:
Debug.Log("按下组合键:ctrl+↓");
break;
case KeyCode.LeftArrow:
Debug.Log("按下组合键:ctrl+←");
break;
case KeyCode.RightArrow:
Debug.Log("按下组合键:ctrl+→");
break;
}
}
}
小知识点结束.