鼠标
//当指定的鼠标按钮被按下时返回true
bool result=Input.GetMouseButton(0);
//在用户按下指定鼠标按键的第一帧返回true
bool result= Input. GetMouseButtonDown(0);
//在用户释放指定鼠标按键的第一帧返回true
bool result= Input. GetMouseButtonUp(0);
//按钮值设定:
0对应左键 , 1对应右键 , 2对应中键。
键盘
//当通过名称指定的按键被用户按住时返回true
bool result=Input.GetKey(KeyCode.A);
//当用户按下指定名称按键时的那一帧返回true
bool result=Input. GetKeyDown(KeyCode.A);
//在用户释放给定名称按键的那一帧返回true
bool result=Input. GetKeyUp(KeyCode.A);
手机
GetTouch - Unity 脚本 API
TouchPhase - Unity 脚本 API
Touch - Unity 脚本 API
移动平台也可以识别:OnMouse*
案例:放大镜
瞬间切换
private Camera camera;
private bool isFar;
private void Start()
{
camera = GetComponent<Camera>();
isFar = true;
}
private void Update()
{
if(Input.GEtMouseButtonDown(1))
{
isFar = !isFar;
if(isFar)
camera.fieldOfView = 60;//摄像头拉远到60
else
camera.fieldOfView = 20;//摄像头拉近到20
}
}
匀速的切换
private Camera camera;
private bool isFar;
private void Start()
{
camera = GetComponent<Camera>();
isFar = true;
}
private void Update()
{
if(Input.GEtMouseButtonDown(1))//按时执行1帧
{
isFar = !isFar;
}
//每帧执行
if(isFar)
{
if(camera.fieldOfView < 60)//摄像头拉远到60
camera.fieldOfView += 2;
}
else
{
if(camera.fieldOfView > 20)//摄像头拉近到20
camera.fieldOfView -= 2;
}
}
由快到慢的切换
private Camera camera;
private bool isFar;
private void Start()
{
camera = GetComponent<Camera>();
isFar = true;
}
private void Update()
{
if(Input.GEtMouseButtonDown(1))//按时执行1帧
{
isFar = !isFar;
}
//每帧执行
if(isFar)
{
//摄像头拉远到60
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 60, 0.1);
if(Mathf.Abs(camera.fieldOfView - 60) < 0.1f)
camera.fieldOfView = 60;
}
else
{
//摄像头拉近到20
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 20, 0.1);
if(Mathf.Abs(camera.fieldOfView - 20) < 0.1f)
camera.fieldOfView = 20;
}
}
多段切换
private Camera camera;
private bool isFar;
public float[] zoomLevel;
private int index;
private void Start()
{
camera = GetComponent<Camera>();
}
private void Update()
{
if(Input.GEtMouseButtonDown(1))//按时执行1帧
{
//index = index < zoomLevel.Length - 1? index + 1 : 0;
index = (index + 1) % zoomLevel.Length;
}
//每帧执行
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoomLevel[index], 0.1);
if(Mathf.Abs(camera.fieldOfView - zoomLevel[index]) < 0.1f)
camera.fieldOfView = 60;
}
自定义按键
虚拟按键:
位置:Edit-Project Setting-input Manager
获取虚拟轴:虚拟轴名就是input Manager中列表元素的名字
- bool result=Input. GetButton(“虚拟轴名”); //true or false
- bool result=Input. GetButtonDown(“虚拟轴名”);
- bool result=Input. GetButtonUp(“虚拟轴名”);
- float value=Input.GetAxis (“虚拟轴名”); // -1 … 0 … 1
- float value=Input.GetAxisRaw (“虚拟轴名”);//-1 0 1
案例:摄像头旋转
public int rotateSpeed = 5;
private void FixedUpdate()//如果在Update中需要 * Time.deltaTime
{
//鼠标左右移动
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
if(x != 0 || y != 0)
{
RotateView(x,y);
}
}
private void RotateView(float x,float y)
{
x *= rotateSpeed;
y *= rotateSpeed;
//视野上下移动,沿X轴旋转
this.transform.Rotate(-y,0,0);
//视野左右移动,沿Y轴旋转,需要沿世界坐标系Y轴
this.transform.Rotate(0,x,0,Space.world);
}
案例:雷电移动
private void Update()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
if(hor != 0 || ver != 0)
{
Movement(hor,ver);
}
}
public float moveSpeed = 10;
private void Movement(float hor,float ver)
{
hor *= moveSpeed * Time.deltaTime;
ver *= moveSpeed * Time.deltaTime;
this.transform.Translate(hor,0,ver);
}