上一个使360旋转物体的脚本,支持鼠标和手势操作,代码如下:
02 | using System.Collections; |
04 | public class SpinWithFinger : MonoBehaviour { |
06 | public Transform target; |
07 | public float speed = 1f; |
22 | #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBPLAYER |
23 | if (Input.GetMouseButton(0)){ |
24 | x = Input.GetAxis( "Mouse X" ); |
25 | y = Input.GetAxis( "Mouse Y" ); |
26 | OnDrag( new Vector2(y,x)*10); |
28 | #elif UNITY_IPHONE || UNITY_ANDROID |
29 | if (Input.touchCount==1){ |
30 | t = Input.GetTouch(0); |
32 | case TouchPhase.Moved: |
33 | OnDrag(t.deltaPosition*10); |
35 | case TouchPhase.Stationary: |
36 | OnDrag(t.deltaPosition*10); |
43 | void OnDrag (Vector2 delta){ |
44 | target.localRotation = Quaternion.Euler(0.5f * delta.x * speed, -0.5f * delta.y * speed, 0f) * target.localRotation; |
这段代码使用了宏定义判断平台(有关宏定义的使用在上一篇文章中有介绍http://www.u3dblog.com/?p=309),加入了两种操作的兼容。
脚本是加在物体上,然后在属性监视面板中可以设置物体旋转的速度,就可以很好的控制了。