Unity3d–C#封装输入键
游戏里我们为什么要封装输入呢?
答案就是到处写输入函数,会导致输入控制乱不好统一管理,有时候还有一定的干扰
1:创建脚本Input.cs
1.1:创建输入控制类InputAxis
public class fps_InputAxis
{
public KeyCode positive;
public KeyCode negative;
}
1.2:创建子弹和链表用来存储控制键,输入值,
public Dictionary<string, KeyCode> buttons = new Dictionary<string, KeyCode>();
public Dictionary<string, fps_InputAxis> axis = new Dictionary<string, fps_InputAxis>();
public List<string> unityAxis = new List<string>();
1.3在初始化Start()里调用初始化方法
private void SetupDefaults(string type = "")
{
if (type == "" || type == "buttons")
{
if (buttons.Count == 0)
{
AddButton("Fire", KeyCode.Mouse0);//开火键---添加按键进入
AddButton("Reload", KeyCode.R);//换弹夹键
AddButton("Jump", KeyCode.Space);//跳跃键
AddButton("Crouch", KeyCode.C);//蹲伏键
AddButton("Sprint", KeyCode.LeftShift);//冲刺键
AddButton("Skill", KeyCode.K);//技能键
}
}
if (type == "" || type == "Axis")
{
if (axis.Count ==