http://blog.csdn.net/awnuxcvbn/article/details/11974921
将此脚本挂在一个物体上cube
using UnityEngine;
using System.Collections;
public class CubeRotate : MonoBehaviour {
/// <summary>
/// 起始位置
/// </summary>
Vector3 startPostion;
/// <summary>
/// 鼠标按下的之前位置
/// </summary>
Vector3 previousPosition;
/// <summary>
/// 鼠标按下之后的滑动距离
/// </summary>
Vector3 offset;
/// <summary>
/// 鼠标抬起后距离初始位置的位置
/// </summary>
Vector3 finalOffset;
bool isSlide;
float angle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) // Input.GetMouseButtonDown(0) 当0键被按下一次
{
startPostion = Input.mousePosition;
previousPosition = Input.mousePosition;
}
if (Input.GetMouseButton(0)) // Input.GetMouseButton(0) 当0键被按住持续侦测(包含down和up各一次)
{
offset = Input.mousePosition - previousPosition;
previousPosition = Input.mousePosition;
transform.Rotate(Vector3.Cross(offset, Vector3.forward).normalized, offset.magnitude, Space.World);
}
if (Input.GetMouseButtonUp(0)) //Input.GetMouseButtonUp(0) 当0键放开一次
{
finalOffset = Input.mousePosition - startPostion;
isSlide = true;
angle = finalOffset.magnitude;
}
if (isSlide)
{
transform.Rotate(Vector3.Cross(finalOffset, Vector3.forward).normalized, angle * 2 * Time.deltaTime, Space.World);
if (angle > 0)
{
angle -= 5;
}
else
{
angle = 0;
}
}
}
}
//unity input {转载}
//Input 输入
//按键
//Input.GetKey(“up”) = Input.GetKey(KeyCode.UpArrow) 按住键盘上键
//Input.GetKeyDown (“up”) 按下键盘上键
//Input.GetKeyUp (“up”) 放开键盘上键
// (KeyCode.UpArrow)为键码
//Input.GetButton(“ ”) = Input.GetKey(“ ”) 两种几乎相同(目前没发现差异)
//Input.GetButton一样会有分Input.GetButtonDown & Input.GetButtonUp
//(“ ”)符号内为按键英文~ 参考Unity→Edit→Project Settings→Input(可新增)
//PS : Unity内并没有内建android(返回与选单)功能键设定,必须使用者自行设定,在Inpu.GetButton(“ ”)中”escpae”&”menu”有对应返回与选单键
//例: if(Input.GetKeyDown (“escpae”)){ Application.Quit(); } 按下返回就退出
//滑鼠用
//Input.GetMouseButton(0) 当0键被按住持续侦测(包含down和up各一次)
//Input.GetMouseButtonDown(0) 当0键被按下一次
//Input.GetMouseButtonUp(0) 当0键放开一次
//键 : 0=左 1=中 2=右
//PS : Unity有对应滑鼠左键输入功能部分可以用在Android手指触碰的操作上
//获取轴
//Input.GetAxis(“ ”) ~ (“ ”)参考Unity→Edit→Project Settings→Input(可新增)
//根据座标轴传回虚拟座标值,取得输入装置输入时值范围-1 ~ 1
//例: Input.GetAxis(“Mouse x”) ~ 可取得滑鼠横向移动增量
//Android 常用 Input
//触碰
//Input.touchCount ~ 触碰数量
//Input.GetTouch(0) ~ 当第一支手指触碰时
//0=第一支1=第二支2=第三支以此类推(触碰到点的侦测数量上限未测不知)
//TouchPhase ~ 触碰状态有分五种Began Moved Stationary Ended Canceled
//Began按下Moved移动Stationary按住没移动Ended离开
//Canceled用于触碰超过5点以上或贴至脸上时取消追踪
//Input.GetTouch(0).position 取得第一支手指触碰座标
//例 :
//Touch 单点移动判断式
//if(Input.touchCount == 1 && Input.GetTouch(0).phase==TouchPhase.Moved){执行}
//(触碰数量为1个) 和(第一支手指触碰时的状态为移动) 时{执行}
//Touch 双点移动判断式
//if(Input.touchCount >1){
// if(Input.GetTouch(0).phase==TouchPhase.Moved || Input.GetTouch(1).phase==TouchPhase.Moved){ 执行}}
//(触碰数量超过1个) 和
// (第一支手指触碰时的状态为移动)或着(第二支手指触碰时的状态为移动) 时{执行}
//Input.GetTouch(0) 和 Input.GetMouseButton(0)
//单点时大致上互通(不确定是否完全相同)
//多点上就会出现差异Touch会取手指前后顺序Mouse会取中心点