28 Feb 2020
686字
3分
次
Unity3D
BY-SA 4.0(除特别声明或转载文章外)
<!DOCTYPE html>
unity 鼠标手势的左右滑动
```
using UnityEngine;
using System.Collections;
public class HeroModelRotate : MonoBehaviour
{
///
/// 第一次按下的位置
///
private Vector2 first = Vector2.zero;
///
/// 鼠标的拖拽位置(第二次的位置)
///
private Vector2 second = Vector2.zero;
///
/// 旋转的角度
///
private float angle = 3f;
void OnGUI()
{
if (Event.current.type == EventType.MouseDown)
{
//记录鼠标按下的位置
first = Event.current.mousePosition;
}
if (Event.current.type == EventType.MouseDrag)
{
//记录鼠标拖动的位置
second = Event.current.mousePosition;
if (second.x < first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件
this.transform.Rotate(Vector3.up, angle);
}
if (second.x > first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件
this.transform.Rotate(Vector3.down, angle);
}
first = second;
}
}
}
```
</html>