Unity 输入系统

来源:互联网 发布:windows android 编辑:程序博客网 时间:2024/05/27 06:56
键盘输入:
对于键盘按键操作,只有三种操作:
键被按下: Input.GetKeyDown();
键被松开: Input.GetKeyUp() ;
键被按下一直没松开: Input.GetKey();
鼠标输入:
Input.GetMouseButtonDown(); 按下触发
Input.GetMouseButtonUp(); 松开触发
Input.GetMouseButton(); 按下不松开一种触发
括号中输入 0对应左键,1对应右键,2对应中键
轴输入 Input.GetAxis/GetAxisRaw
根据坐标轴名称返回虚拟坐标系中的值
使用控制器和键盘输入时此值范围在-1到1之间
键盘 Horizontal (水平轴)
Vertical (垂直轴)
例如:
public class example : MonoBehaviour {public float speed = 10.0F;public float rotationSpeed = 100.0F;void Update() {  //获取横向和纵向坐标轴,默认情况下他们关联到方向键上,值的范围是在-1到1之间float translation = Input.GetAxis("Vertical") * speed**Time.deltaTime;float rotation = Input.GetAxis("Horizontal") * rotationSpeed*Time.deltaTime;    //沿着z轴平移对象transform.Translate(0, 0, translation);    //以我们的y轴为中心旋转transform.Rotate(0, rotation, 0);}}



鼠标:
Mouse X (水平轴) Mouse (垂直轴) Mouse ScrollWheel (鼠标中键滚动)

public class question1 : MonoBehaviour {    //在鼠标左键被按下时,利用鼠标的中轴,实现模型的缩放。void Update () {        if (Input.GetMouseButton(0))   //按下鼠标左键        {               //获取鼠标中轴滚动量            float a = Input.GetAxis("Mouse ScrollWheel");            //找到摄像机组件Camera上的fieldOfView属性            this.gameObject.GetComponent<Camera>().fieldOfView += a;        }}}



触摸
当将Unity游戏运行到IOS或Android设备上时,桌面系统的鼠标左键可以自动变为手机屏幕上的触屏操作,但如多点触屏等操作却是无法利用鼠标操作进行的。Unity的Input类中不仅包含桌面系统的各种输入功能,也包含了针对移动设备触屏操作的各种功能,下面介绍一下Input类在触碰操作上的使用。

首先介绍一下Input.touches结构,这是一个触摸数组,每个记录代表着手指在屏幕上的触碰状态。每个手指触控都是通过Input.touches来描述的:

属性 说明
fingerId 触摸的唯一索引
position 触摸屏幕的位置
deltatime 从最后状态到目前状态所经过的时间
tapCount 点击数。Andorid设备不对点击计数,这个方法总是返回1
deltaPosition 自最后一帧所改变的屏幕位置
phase 相位,也即屏幕操作状态

其中phase(状态)有以下这几种:
状态 说明
Began 手指刚刚触摸屏幕
Moved 手指在屏幕上移动
Stationary 手指触摸屏幕,但自最后一阵没有移动
Ended 手指离开屏幕
Canceled 系统取消触控跟踪,原因如把设备放在脸上或同时超过5个触摸点



原创粉丝点击