一、使用
缩放效果和手势识别通过 Lean Touch插件实现(插件在商店中免费下载)
-
缩放功能:
- 在目标模型上添加Lean Touch脚本
-
添加Lean Scale脚本,设置双指缩放在Required Finger Count给值2,限制缩放的大小值勾选Scale Clamp并填写 Scale Min 和 Scale Max
旋转:
1. 单个轴的旋转
using UnityEngine;
using System.Collections;
using Lean.Touch;
public class LeanTouchRotateX : MonoBehaviour {
Vector3 previousPosition;
Vector3 offset;
void Update() {
// 通过LeanTouch插件,来判断目前触碰屏幕的手指数量
if (LeanTouch.Fingers.Count == 1) {
// LeanTouch可以将鼠标点击和屏幕触碰进行转换
if (Input.GetMouseButtonDown (0)) {
previousPosition = Input.mousePosition;
}
if (Input.GetMouseButton (0)) {
offset = Input.mousePosition - previousPosition;
previousPosition = Input.mousePosition;
if (offset.x > 0) {
transform.Rotate (Vector3.down, offset.magnitude/5, Space.World);
}
if (offset.x < 0) {
transform.Rotate (Vector3.up, offset.magnitude/5, Space.World);
}
}
}
}
}
offset.magnitude/5 : 旋转角度大小, Space.World旋转的轴是世界轴,可根据使用需要修改
2. 两个轴的旋转
using UnityEngine;
using System.Collections;
using Lean.Touch;
public class LeanTouchRotate : MonoBehaviour {
Vector3 previousPosition;
Vector3 offset;
void Update() {
// 通过LeanTouch插件,来判断目前触碰屏幕的手指数量
if (LeanTouch.Fingers.Count == 1) {
// LeanTouch可以将鼠标点击和屏幕触碰进行转换
if (Input.GetMouseButtonDown (0)) {
previousPosition = Input.mousePosition;
}
if (Input.GetMouseButton (0)) {
offset = Input.mousePosition - previousPosition;
previousPosition = Input.mousePosition;
float xdis = Mathf.Abs (offset.x);
float ydis = Mathf.Abs (offset.y);
if (xdis > ydis) {
if (offset.x > 0) {
transform.Rotate (Vector3.down, offset.magnitude/5, Space.World);
}
if (offset.x < 0) {
transform.Rotate (Vector3.up, offset.magnitude/5, Space.World);
}
} else {
if (offset.y > 0) {
transform.Rotate (Vector3.right, offset.magnitude/5, Space.World);
}
if (offset.y < 0) {
transform.Rotate (Vector3.left, offset.magnitude/5, Space.World);
}
}
}
}
}
}