【Unity入门】15.鼠标输入和键盘输入

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

    大家好,我是Lampard~~

    欢迎来到Unity入门系列博客,所学知识来自B站阿发老师~感谢

(一)监听鼠标输入

(1) Input类

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

    下面我们会调用几个常用的接口监听玩家鼠标和键盘的输入

 (2) GetMouseButtonUp 和 GetMouseButtonDown

    input.GetMouseButtonUp 和 input.GetMouseButtonDown 能够分别监听鼠标的按下和松开事件,值得注意的是,这两个方法需要传入参数,0表示左键,1表示右键,2表示中间键

    比如我们可以在代码中这样写,来监听游戏中的鼠标点击:

    void Update()
    {
        if (Input.GetMouseButtonUp(0)) {
            Debug.Log("正在执行鼠标左键抬起");
        }

        if (Input.GetMouseButtonUp(1))
        {
            Debug.Log("正在执行鼠标右键抬起");
        }

        if (Input.GetMouseButtonUp(2))
        {
            Debug.Log("正在执行鼠标中键抬起");
        }

        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("正在执行鼠标左键点击");
        }

        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("正在执行鼠标右键点击");
        }

        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("正在执行鼠标中键点击");
        }
    }

    注意,点击的时候需要选中Game窗口(玩家看到的窗口),试试看效果:

(3) GetMouseButton长按响应

    如果想监听长按逻辑可以选用GetMouseButton方法,同样需要传入0/1/2作为参数

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Debug.Log("正在执行鼠标左键长按");
        }
    }

    只要一直处在长按状态,就会一直输出内容

 (4) mousePosition屏幕坐标

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

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("正在执行鼠标左键点击");
            Vector3 MousePos = Input.mousePosition;
            Debug.Log("当前坐标:" + MousePos);
        }
    }
}

    看看效果:

(二)监听键盘输入

(1) 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 键。

    比如这样的代码可以监听W键的按下和抬起

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("正在执行W键的按下");
        }

        if (Input.GetKeyUp(KeyCode.W))
        {
            Debug.Log("正在执行W键的抬起");
        }
    }

(2)控制物体前后左右移动

    我们学会了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);
        }
    }

    最后看看效果

 

 

好啦今天就到这里,感谢阅读!!!
点赞,关注!!!

猜你喜欢

转载自blog.csdn.net/cooclc/article/details/130278106