Unity基础(07)—— 鼠标、键盘输入

一、Unity 通过键盘进行输入字符

Input 类,包装了输入功能,一般在 Update 中来检测用户的输入。

1、获取鼠标的输入,

    // 0 对应左键,1 对应右键,2 对应中键。
    bool result00 = Input.GetMouseButton(0);
    bool result01 = Input.GetMouseButtonDown(1); 
    bool result02 = Input.GetMouseButtonUp(2);

2、接受键盘输入的字符

void Update () {

    if (Input.GetKey(KeyCode.M))
    {
        // 长按事件监听某一按键是否处于一直按下的状态,通过Input.GetKey( )来判断键盘中某一按键是否被一直按着。
        // 当按下该键的时候,会在按下的时间内,按帧来进行刷新。
        Debug.Log("You have click the M key");
    }

    if (Input.GetKeyDown(KeyCode.A))
    {
        // GetKeyDown( )方法将按键值作为参数,监听此按键是否被按下。按下返回true,否者返回false。
        Debug.Log("You click the A");
    }

    if (Input.GetKeyUp(KeyCode.A))
    {
        // 当该按键被松开的时候,返回true
        Debug.Log("Click the A ");
     }


}

二、实例:

1、通过鼠标右键实现相机的镜头缩放。

首先将控制的脚本挂在相机上

public class sc01 : MonoBehaviour 
{
    public bool isFar = true;
    public Camera ce;

    private void Start()
    {
        ce = GetComponent<Camera>();
    }

    // Update is called once per frame
    void Update ()
    {
        //*************** 方式一:一帧之内完成 ***************

        if (Input.GetMouseButtonDown(1))
        { 
            if (isFar)
            {
                ce.fieldOfView = 60;
            }
            else
            {
                ce.fieldOfView = 20;
            }
            isFar = !isFar;
        }
	}

    private void Update()
    {
        //************* 方式二:多帧逐渐变换匀速 ***************

        if (Input.GetMouseButtonDown(1))
        {
            //按下鼠标右键时执行一帧
            isFar = !isFar;
        }

        // 每帧执行一次
        if (isFar)
        {
            if (ce.fieldOfView < 60)
            {
                ce.fieldOfView += 1;
                if (Mathf.Abs(ce.fieldOfView - 60) < 0.2)
                {
                    ce.fieldOfView = 60;
                }
            }
            
        }
        else
        {
            if (ce.fieldOfView > 20)
            {
                ce.fieldOfView -= 1;
                if (Mathf.Abs(ce.fieldOfView - 20) < 0.2)
                {
                    ce.fieldOfView = 20;
                }
            }
        }
    }


    //************* 方式三:变速,先快后慢,并设置多个缩放等级 ***************

    public float[] zoomLevel;  //当类型一致,个数不一致时,一般定义数组来进行处理,数组大小不定
    private int index;
    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            //按下鼠标右键时执行一帧
            index = (index < (zoomLevel.Length - 1)) ? index + 1 : 0;
        }

        // 每帧执行一次
        ce.fieldOfView = Mathf.Lerp(ce.fieldOfView, zoomLevel[index], 0.1f);
        if (Mathf.Abs(ce.fieldOfView - zoomLevel[index]) < 0.2f)
        {
            ce.fieldOfView = zoomLevel[index];
        }
    }

}

2、鼠标左右上下移动时,物体也左右上下移动

public class sc02 : MonoBehaviour 
{
	
	// Update is called once per frame
	void FixedUpdate ()
    {
        // 鼠标左右移动
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");

        Demo2(x, y);

    }

    private void Demo2(float x, float y)
    {
        transform.Rotate(-y, 0, 0);
        transform.Rotate(0, x, 0,Space.World);
        Debug.Log("x = " + x);
        Debug.Log("y = " + y);
    }
}

 

 

 

 

 

 


参考资料:

[1]  输入与控制——键盘事件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值