UIDragResize.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Interaction/Drag-Resize Widget")]
  4. public class UIDragResize : MonoBehaviour
  5. {
  6. private void OnDragStart()
  7. {
  8. if (this.target != null)
  9. {
  10. Vector3[] worldCorners = this.target.worldCorners;
  11. this.mPlane = new Plane(worldCorners[0], worldCorners[1], worldCorners[3]);
  12. Ray currentRay = UICamera.currentRay;
  13. float distance;
  14. if (this.mPlane.Raycast(currentRay, out distance))
  15. {
  16. this.mRayPos = currentRay.GetPoint(distance);
  17. this.mLocalPos = this.target.cachedTransform.localPosition;
  18. this.mWidth = this.target.width;
  19. this.mHeight = this.target.height;
  20. this.mDragging = true;
  21. }
  22. }
  23. }
  24. private void OnDrag(Vector2 delta)
  25. {
  26. if (this.mDragging && this.target != null)
  27. {
  28. Ray currentRay = UICamera.currentRay;
  29. float distance;
  30. if (this.mPlane.Raycast(currentRay, out distance))
  31. {
  32. Transform cachedTransform = this.target.cachedTransform;
  33. cachedTransform.localPosition = this.mLocalPos;
  34. this.target.width = this.mWidth;
  35. this.target.height = this.mHeight;
  36. Vector3 b = currentRay.GetPoint(distance) - this.mRayPos;
  37. cachedTransform.position += b;
  38. Vector3 vector = Quaternion.Inverse(cachedTransform.localRotation) * (cachedTransform.localPosition - this.mLocalPos);
  39. cachedTransform.localPosition = this.mLocalPos;
  40. NGUIMath.ResizeWidget(this.target, this.pivot, vector.x, vector.y, this.minWidth, this.minHeight, this.maxWidth, this.maxHeight);
  41. }
  42. }
  43. }
  44. private void OnDragEnd()
  45. {
  46. this.mDragging = false;
  47. }
  48. public UIWidget target;
  49. public UIWidget.Pivot pivot = UIWidget.Pivot.BottomRight;
  50. public int minWidth = 100;
  51. public int minHeight = 100;
  52. public int maxWidth = 100000;
  53. public int maxHeight = 100000;
  54. private Plane mPlane;
  55. private Vector3 mRayPos;
  56. private Vector3 mLocalPos;
  57. private int mWidth;
  58. private int mHeight;
  59. private bool mDragging;
  60. }