123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using UnityEngine;
- [AddComponentMenu("NGUI/Interaction/Drag-Resize Widget")]
- public class UIDragResize : MonoBehaviour
- {
- private void OnDragStart()
- {
- if (this.target != null)
- {
- Vector3[] worldCorners = this.target.worldCorners;
- this.mPlane = new Plane(worldCorners[0], worldCorners[1], worldCorners[3]);
- Ray currentRay = UICamera.currentRay;
- float distance;
- if (this.mPlane.Raycast(currentRay, out distance))
- {
- this.mRayPos = currentRay.GetPoint(distance);
- this.mLocalPos = this.target.cachedTransform.localPosition;
- this.mWidth = this.target.width;
- this.mHeight = this.target.height;
- this.mDragging = true;
- }
- }
- }
- private void OnDrag(Vector2 delta)
- {
- if (this.mDragging && this.target != null)
- {
- Ray currentRay = UICamera.currentRay;
- float distance;
- if (this.mPlane.Raycast(currentRay, out distance))
- {
- Transform cachedTransform = this.target.cachedTransform;
- cachedTransform.localPosition = this.mLocalPos;
- this.target.width = this.mWidth;
- this.target.height = this.mHeight;
- Vector3 b = currentRay.GetPoint(distance) - this.mRayPos;
- cachedTransform.position += b;
- Vector3 vector = Quaternion.Inverse(cachedTransform.localRotation) * (cachedTransform.localPosition - this.mLocalPos);
- cachedTransform.localPosition = this.mLocalPos;
- NGUIMath.ResizeWidget(this.target, this.pivot, vector.x, vector.y, this.minWidth, this.minHeight, this.maxWidth, this.maxHeight);
- }
- }
- }
- private void OnDragEnd()
- {
- this.mDragging = false;
- }
- public UIWidget target;
- public UIWidget.Pivot pivot = UIWidget.Pivot.BottomRight;
- public int minWidth = 100;
- public int minHeight = 100;
- public int maxWidth = 100000;
- public int maxHeight = 100000;
- private Plane mPlane;
- private Vector3 mRayPos;
- private Vector3 mLocalPos;
- private int mWidth;
- private int mHeight;
- private bool mDragging;
- }
|