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
== 0)
{
AddAxis("Horizontal", KeyCode
.W
, KeyCode
.S
);
AddAxis("Vertical", KeyCode
.A
, KeyCode
.D
);
}
}
if (type
== "" || type
== "UnityAxis")
{
if (unityAxis
.Count
== 0)
{
AddUntiyAxis("Mouse X");
AddUntiyAxis("Mouse Y");
AddUntiyAxis("Horizontal");
AddUntiyAxis("Vertical");
}
}
}
1.4添加按键函数
private void AddButton(string n
, KeyCode k
)
{
if (buttons
.ContainsKey(n
))
buttons
[n
] = k
;
else
buttons
.Add(n
, k
);
}
1.5添加自定义轴
private void AddAxis(string n
, KeyCode pk
, KeyCode nk
)
{
if (axis
.ContainsKey(n
))
{
axis
[n
] = new fps_InputAxis() { positive
= pk
, negative
= nk
};
}
else
{
axis
.Add(n
, new fps_InputAxis() { positive
= pk
, negative
= nk
});
}
}
1.6添加轴
private void AddUntiyAxis(string n
)
{
if (!unityAxis
.Contains(n
))
{
unityAxis
.Add(n
);
}
}
1.7:外界获取按键的值—按下
public bool GetButton(string button
)
{
if (buttons
.ContainsKey(button
))
{
return Input
.GetKey(buttons
[button
]);
}
return false;
}
1.8 返回按键是否按下状态
public bool GetButtonDown(string button
)
{
if (buttons
.ContainsKey(button
))
{
return Input
.GetKeyDown(buttons
[button
]);
}
return false;
}
1.9 获取轴(-1~1)float 类型
public float GetAxis(string axis
)
{
if(this.unityAxis
.Contains(axis
))
{
return Input
.GetAxis(axis
);
}
return 0;
}
1.10 获取轴,返回一个值,只有-1,0,1这三个值
public float GetAxisRaw(string axis
)
{
if(this.axis
.ContainsKey(axis
))
{
float val
= 0;
if(Input
.GetKey(this.axis
[axis
].positive
))
{
return 1;
}
if(Input
.GetKey(this.axis
[axis
].negative
))
{
return -1;
}
return val
;
}else if(unityAxis
.Contains(axis
))
{
return Input
.GetAxisRaw(axis
);
}
else
{
return 0;
}
}
2输入参数控制PlayerParameter.cs
2.1 添加特性
[RequireComponent(typeof(CharacterController
))]
2.2 参数值
[HideInInspector]
public Vector2 inputSmoothLook
;
[HideInInspector]
public Vector2 inputMoveVector
;
[HideInInspector]
public bool inputCrouch
;
[HideInInspector]
public bool inputJump
;
[HideInInspector]
public bool inputSprint
;
[HideInInspector]
public bool inputFile
;
[HideInInspector]
public bool inputReload
;
3在外界英雄的输入进行获取输入值控制
3.1 持有输入和输入参数 的引用
private fps_PlayerParameter parameter
;
private fps_Input input
;
3.2 对持有输入进行加载组件
parameter
= this.GetComponent<fps_PlayerParameter>();
input
= GameObject
.FindGameObjectWithTag(Tags
.gameController
).GetComponent<fps_Input>();
3.2 ###### 3.3在Update()进行调用赋值函数
private void InitialInput()
{
parameter
.inputMoveVector
= new Vector2(input
.GetAxis("Horizontal"),input
.GetAxis("Vertical"));
parameter
.inputSmoothLook
= new Vector2(input
.GetAxisRaw("Mouse X"), input
.GetAxisRaw("Mouse Y"));
parameter
.inputCrouch
= input
.GetButton("Crouch");
parameter
.inputJump
= input
.GetButton("Jump");
parameter
.inputSprint
= input
.GetButton("Sprint");
parameter
.inputFile
= input
.GetButton("Fire");
parameter
.inputReload
= input
.GetButtonDown("Reload");
}
4:英雄调用测试
4.1:状态和输入的持有
private fps_PlayerParameter parameter
;
private Vector3 moveDirection
= Vector3
.zero
;
parameter
= this.GetComponent<fps_PlayerParameter>();
4.2在update()里调用移动函数
private void UpdateMove()
{
moveDirection
= new Vector3(parameter
.inputMoveVector
.x
, 0, parameter
.inputMoveVector
.y
);
moveDirection
= transform
.TransformDirection(moveDirection
);
moveDirection
*= speed
;
if(parameter
.inputJump
)
{
moveDirection
.y
= jumpSpeed
;
AudioSource
.PlayClipAtPoint(jumpAudio
, transform
.position
);
CurrentSpeed();
}
moveDirection
.y
-= gravity
* Time
.deltaTime
;
}
4.3 判断是否蹲伏
if(parameter
.inputCrouch
)
{
}