PanWithMouse.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Examples/Pan With Mouse")]
  4. public class PanWithMouse : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.mTrans = base.transform;
  9. this.mStart = this.mTrans.localRotation;
  10. }
  11. private void Update()
  12. {
  13. float deltaTime = RealTime.deltaTime;
  14. Vector3 mousePosition = Input.mousePosition;
  15. float num = (float)Screen.width * 0.5f;
  16. float num2 = (float)Screen.height * 0.5f;
  17. if (this.range < 0.1f)
  18. {
  19. this.range = 0.1f;
  20. }
  21. float x = Mathf.Clamp((mousePosition.x - num) / num / this.range, -1f, 1f);
  22. float y = Mathf.Clamp((mousePosition.y - num2) / num2 / this.range, -1f, 1f);
  23. this.mRot = Vector2.Lerp(this.mRot, new Vector2(x, y), deltaTime * 5f);
  24. this.mTrans.localRotation = this.mStart * Quaternion.Euler(-this.mRot.y * this.degrees.y, this.mRot.x * this.degrees.x, 0f);
  25. }
  26. public Vector2 degrees = new Vector2(5f, 3f);
  27. public float range = 1f;
  28. private Transform mTrans;
  29. private Quaternion mStart;
  30. private Vector2 mRot = Vector2.zero;
  31. }