Unity -简单键鼠事件和虚拟轴

简单键鼠事件 — “Test_03”

KeyTest

键鼠事件每帧都要监听,要放在Update()中处理

public class KeyTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // 【鼠标点击事件】 0左键、1右键、2中键(滚轮)
        //      按下鼠标左键
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("点击鼠标左键");
        }
        //      持续按下鼠标左键
        if (Input.GetMouseButton(0))
        {
            Debug.Log("持续按下了鼠标左键");
        }

        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("抬起了鼠标左键");
        }
        
        // 【键盘点击事件】
        //      按下按键
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("按下了A键");
        }
        //      持续按下按键
        if (Input.GetKey(KeyCode.A))
        {
            Debug.Log("持续按下了A键");
        }
        //      抬起按键
        if (Input.GetKeyUp(KeyCode.A))
        {
            Debug.Log("松开了A键");
        }
    }
}

虚拟轴 — “AxisTest”

编辑 -> 项目设置 -> 输入管理器
虚拟轴

public class AxisTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // 获取水平轴,通过虚拟轴名称获取
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Debug.Log(horizontal + "     " +vertical);
        // 虚拟按键,通过虚拟按键名称定位,这里是设置了跳跃按键为空格
        if (Input.GetButtonDown("Jump"))
        {
            Debug.Log("按下空格");
        }

        if (Input.GetButton("Jump"))
        {
            Debug.Log("持续按下空格");
        }

        if (Input.GetButtonUp("Jump"))
        {
            Debug.Log("松开空格");
        }
    }
}
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值