【Unity实现单机功能】自定义按键输入

本文介绍了如何在Unity中实现自定义多个按键的输入功能,为游戏或交互应用提供更灵活的操作控制。
摘要由CSDN通过智能技术生成

实现自定义多个数的按键输入

//CustomKey.cs 
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

/// <summary>
/// 自定义按键
/// </summary>
public class CustomKey : MonoBehaviour
{
    //声明Button对象以进行拖拽
    public Button resetBtn;
    public Button jumpBtn;
    public Button getDownBtn;
    public Button leftHeadBtn;
    public Button rightHeadBtn;
    public Button musicSwitchBtn;

    public CustomButton jump;
    public CustomButton getDown;
    public CustomButton leftHead;
    public CustomButton rightHead;
    public CustomButton musicSwitch;

    void Awake()
    {
        resetBtn.onClick.AddListener(CustomButton.ResetBindKeys);
        resetBtn.transform.FindChild("Text").GetComponent<Text>().text = "Reset";                
        jump = new CustomButton("Jump", jumpBtn, new KeyCode[]{ KeyCode.Space});
        getDown = new CustomButton("GetDown", getDownBtn, new KeyCode[] { KeyCode.LeftControl});
        leftHead = new CustomButton("LeftHead", leftHeadBtn, new KeyCode[] { KeyCode.Q});
        rightHead = new CustomButton("RightHead", rightHeadBtn, new KeyCode[] { KeyCode.E});
        musicSwitch = new CustomButton("musicSwitch", musicSwitchBtn, new KeyCode[] { KeyCode.LeftControl, KeyCode.M});     
    }

    void Update()
    {
        if (CustomButton.isPlaying)
        {
            jump.Function();
            getDown.Function();
            leftHead.Function();
            rightHead.Function();
            musicSwitch.Function();
        }
    }

    void OnGUI()
    {
        if (CustomButton.isWaitingForKey)
        {
            Event e = Event.current;
            if (e.isKey && e.keyCode != KeyCode.None)
            {                
                if (!CustomButton.tempList.Contains(e.keyCode)) //如果list中不包含该按键,则记录按键
  
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值