using UnityEngine;
[AddComponentMenu("NGUI/Examples/Spin With Mouse")]
public class SpinWithMouse : MonoBehaviour
{
public enum Axis {
XAxis = 0,
YAxis = 1,
ZAxis = 2,
}
public Transform target;
public float speed = 1f;
public bool direction = true;
public Axis whichAxis = Axis.YAxis;
Transform mTrans;
void Start ()
{
mTrans = transform;
}
void OnDrag (Vector2 delta)
{
UICamera.currentTouch.clickNotification = UICamera.ClickNotification.None;
float deltaval = delta.x;
Quaternion newRotation = Quaternion.Euler(0f, -0.5f * deltaval * speed, 0f);
if (direction) {
deltaval = delta.x;
} else {
deltaval = delta.y;
}
switch (whichAxis) {
case Axis.XAxis:
newRotation = Quaternion.Euler( -0.5f * deltaval * speed, 0f, 0f);
break;
case Axis.YAxis:
newRotation = Quaternion.Euler(0f, -0.5f * deltaval * speed, 0f);
break;
case Axis.ZAxis:
newRotation = Quaternion.Euler(0f, 0f, -0.5f * deltaval * speed);
break;
}
if (target != null) {
target.localRotation = newRotation * target.localRotation;
} else {
mTrans.localRotation = newRotation * mTrans.localRotation;
}
}
}