Unity3d--C#封装输入键(移动,鼠标,跳,开火,换弹,蹲下)

    技术2022-07-10  128

    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)//是否蹲伏 { //蹲伏执行函数 }
    Processed: 0.014, SQL: 9