蓝鸥Unity开发教程之课时7 Input类


一、Input

二、Input获取键盘事件

三、Input获取鼠标事件

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {


    void Start () {
    
    }

    //获取用户事件需要使用Input

    void Update () {
        //每帧都需要监听用户事件
        //在当前这一帧中,如果用户按下W就会返回true,否则返回false

            if (Input.GetKeyDown (KeyCode.W)) {
            print ("往前走");
        }

        //Input.GetKeyDown()方法用来检测按键按下的事件
            if (Input.GetKeyDown (KeyCode.S)) {
            //print只能在MonoBehaviour的子类中使用,其他情况下只能使用Debug.Log()打印
            Debug.Log ("往后退");
        }
        //Input.GetKeyUp()方法用来检测按键弹起的事件
        if(Input.GetKeyUp(KeyCode.Alpha1)){
            print ("弹起了1");
        }

        //Input.GetKey()方法用来检测持续按键的事件
        if(Input.GetKey(KeyCode.F)){
            print ("按下了F");
        }

        //Input.GetMouseButtonDown()方法用来检测鼠标按键按下的事件
        //参数0表示鼠标左键
        //参数1表示鼠标右键
        //参数2表示鼠标中键
        if(Input.GetMouseButtonDown(0)){
            print ("按下鼠标左键");
        }

        //Input.GetMouseButtonUp()方法用来检测鼠标按键弹起的事件
        if(Input.GetMouseButtonUp(0)){
            print ("弹起了鼠标左键");
        }

        //Input.GetMouseButton()方法用来检测鼠标持续按下的事件
        if(Input.GetMouseButton(0)){
            print ("持续按下鼠标左键");
        }


    
    }
}

 

一、Input

二、Input获取键盘事件

三、Input获取鼠标事件

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {


    void Start () {
    
    }

    //获取用户事件需要使用Input

    void Update () {
        //每帧都需要监听用户事件
        //在当前这一帧中,如果用户按下W就会返回true,否则返回false

            if (Input.GetKeyDown (KeyCode.W)) {
            print ("往前走");
        }

        //Input.GetKeyDown()方法用来检测按键按下的事件
            if (Input.GetKeyDown (KeyCode.S)) {
            //print只能在MonoBehaviour的子类中使用,其他情况下只能使用Debug.Log()打印
            Debug.Log ("往后退");
        }
        //Input.GetKeyUp()方法用来检测按键弹起的事件
        if(Input.GetKeyUp(KeyCode.Alpha1)){
            print ("弹起了1");
        }

        //Input.GetKey()方法用来检测持续按键的事件
        if(Input.GetKey(KeyCode.F)){
            print ("按下了F");
        }

        //Input.GetMouseButtonDown()方法用来检测鼠标按键按下的事件
        //参数0表示鼠标左键
        //参数1表示鼠标右键
        //参数2表示鼠标中键
        if(Input.GetMouseButtonDown(0)){
            print ("按下鼠标左键");
        }

        //Input.GetMouseButtonUp()方法用来检测鼠标按键弹起的事件
        if(Input.GetMouseButtonUp(0)){
            print ("弹起了鼠标左键");
        }

        //Input.GetMouseButton()方法用来检测鼠标持续按下的事件
        if(Input.GetMouseButton(0)){
            print ("持续按下鼠标左键");
        }


    
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值