• 【Unity入门】鼠标输入和键盘输入


    Unity的Input类提供了许多监听用户输入的方法,比如我们常见的鼠标,键盘,手柄等。我们可以用Input类的接口来获取用户的输入信息

    一、监听鼠标输入
    GetMouseButtonUp 、GetMouseButtonDown、GetMouseButton

    input.GetMouseButtonDown和 input.GetMouseButtonUp 能够分别监听鼠标的按下和松开事件,GetMouseButton长按响应,值得注意的是,这三个方法需要传入参数,0表示左键,1表示右键,2表示中间键
    比如我们可以在代码中这样写,来监听游戏中的鼠标点击:

        void Update()
        {
            /*鼠标输入*/
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("你按下了鼠标左键");
            }
            if (Input.GetMouseButton(0))
            {
                Debug.Log("你压着鼠标左键不放");
            }
            if (Input.GetMouseButtonUp(0))
            {
                Debug.Log("你松开了鼠标左键");
            }
    
            if (Input.GetMouseButtonDown(1))
            {
                Debug.Log("你按下了鼠标右键");
            }
            if (Input.GetMouseButton(1))
            {
                Debug.Log("你压着鼠标右键不放");
            }
            if (Input.GetMouseButtonUp(1))
            {
                Debug.Log("你松开了鼠标右键");
            }
    
            if (Input.GetMouseButtonDown(2))
            {
                Debug.Log("你按下了鼠标中键");
            }
            if (Input.GetMouseButton(2))
            {
                Debug.Log("你压着鼠标中键不放");
            }
            if (Input.GetMouseButtonUp(2))
            {
                Debug.Log("你松开了鼠标中键");
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    mousePosition屏幕坐标

    如果想获取当前屏幕的坐标,可以用Input.mousePosition来访问,它是一个vector3类型的变量, 比如这样,我们就可以在鼠标点击时获取当前点击的屏幕位置

        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("正在执行鼠标左键点击");
                Vector3 MousePos = Input.mousePosition;
                Debug.Log("当前坐标:" + MousePos);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    二、监听键盘输入
    GetKeyUp,GetKeyDown,GetKey

    和鼠标一样,键盘也可以获取它的按下,长按和抬起状态。分别需要调用Input.GetKeyDown,Input.GetKey 和 Input.GetKeyUp 来实现
    同时它需要传入参数:KeyCode,下面是常见的KeyCode值:

    • KeyCode.A:A 键。

    • KeyCode.W:W 键。

    • KeyCode.S:S 键。

    • KeyCode.D:D 键。

    • KeyCode.Space:空格键。

    • KeyCode.Return:回车键。

    • KeyCode.Escape:Esc 键。

    • KeyCode.LeftShift:左 Shift 键。

    • KeyCode.RightShift:右 Shift 键。

    • KeyCode.LeftAlt:左 Alt 键。

    • KeyCode.RightAlt:右 Alt 键。

    • KeyCode.Tab:Tab 键。

    代码示例:

        void Update()
        {
    
            /*键盘输入*/
            if (Input.GetKey(KeyCode.Space))
            {
                Debug.Log("你压着空格不放");
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Debug.Log("你按下了空格");
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                Debug.Log("你松开了空格");
            }
            
            if (Input.GetKey(KeyCode.A))
            {
                Debug.Log("你压着A不放");
            }
            if (Input.GetKeyDown(KeyCode.A))
            {
                Debug.Log("你按下了A");
            }
            if (Input.GetKeyUp(KeyCode.A))
            {
                Debug.Log("你松开了A");
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    我们学会了transform.translate方法,也学会了响应键盘的按键事件,那我们就可以写出一个用WSAD键,控制物体前后左右移动的方法了

        void Update()
        {
            float DisPreSec = 6f;
            if (Input.GetKey(KeyCode.W))
            {
                this.transform.Translate(0, 0, DisPreSec * Time.deltaTime);
            }
     
            if (Input.GetKey(KeyCode.S))
            {
                this.transform.Translate(0, 0, -DisPreSec * Time.deltaTime);
            }
     
            if (Input.GetKey(KeyCode.A))
            {
                this.transform.Translate(DisPreSec * Time.deltaTime, 0, 0);
            }
     
            if (Input.GetKey(KeyCode.D))
            {
                this.transform.Translate(-DisPreSec * Time.deltaTime, 0, 0);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    geecg-uniapp 源码下载运行 修改端口号 修改tabBar 修改展示数据
    抖音矩阵系统,抖音矩阵源码定制。
    RKMEDIA--VO的使用
    第52节:cesium 3DTiles模型特效+选中高亮(含源码+视频)
    ArrayList特点分析及源码阅读
    Cholesterol-PEG-Maleimide|胆固醇-聚乙二醇-马来酰亚胺修饰蛋白用
    阿里 OSS鉴权访问文件
    5.1 加载矢量图层(ogr,gpx)
    乘风破浪,遇见最佳跨平台跨终端框架.Net Core/.Net生态 - 官方扩展集锦(Microsoft.Extensions on Nuget)
    转铁蛋白修饰长春新碱-粉防己碱脂质体|转铁蛋白修饰共载紫杉醇和金雀异黄素脂质体(试剂)
  • 原文地址:https://blog.csdn.net/weixin_45961836/article/details/134562883