Created
August 30, 2024 07:38
-
-
Save Ddemon26/e1e94018470a97f8fccb4832814c6dde to your computer and use it in GitHub Desktop.
Simple Drag Window Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.EventSystems; | |
| namespace TC._Project.TestSystems.Debugging { | |
| /// <summary> | |
| /// A class that allows a UI window to be dragged within the bounds of its parent canvas. | |
| /// </summary> | |
| public class DragWindow : MonoBehaviour, IDragHandler, IPointerDownHandler { | |
| [Tooltip("The RectTransform of the window to be dragged.")] | |
| [SerializeField] RectTransform m_dragRectTransform; | |
| [Tooltip("The Canvas in which the window is contained.")] | |
| [SerializeField] Canvas m_canvas; | |
| Vector2 m_canvasSize; | |
| void Awake() { | |
| EnsureRectTransformIsSet(); | |
| FindCanvasInHierarchy(); | |
| if (m_canvas) { | |
| m_canvasSize = m_canvas.GetComponent<RectTransform>().sizeDelta; | |
| } | |
| } | |
| /// <summary> | |
| /// Ensures that the RectTransform is set, assigning it from the parent if not. | |
| /// </summary> | |
| void EnsureRectTransformIsSet() { | |
| if (!m_dragRectTransform) { | |
| m_dragRectTransform = transform.parent.GetComponent<RectTransform>(); | |
| } | |
| } | |
| /// <summary> | |
| /// Finds the Canvas in the hierarchy, assigning it from the parent if not set. | |
| /// </summary> | |
| void FindCanvasInHierarchy() { | |
| if (!m_canvas) { | |
| m_canvas = GetComponentInParent<Canvas>(); | |
| } | |
| } | |
| /// <summary> | |
| /// Called when the object is dragged. | |
| /// </summary> | |
| /// <param name="eventData">Current event data.</param> | |
| public void OnDrag(PointerEventData eventData) { | |
| var newPosition = m_dragRectTransform.anchoredPosition + eventData.delta / m_canvas.scaleFactor; | |
| newPosition = ConstrainWithinCanvas(newPosition); | |
| m_dragRectTransform.anchoredPosition = newPosition; | |
| } | |
| /// <summary> | |
| /// Constrains the position within the bounds of the canvas. | |
| /// </summary> | |
| /// <param name="position">The new position to be constrained.</param> | |
| /// <returns>The constrained position.</returns> | |
| Vector2 ConstrainWithinCanvas(Vector2 position) { | |
| var minPosition = -0.5f * m_canvasSize; | |
| var maxPosition = 0.5f * m_canvasSize; | |
| position.x = Mathf.Clamp(position.x, minPosition.x, maxPosition.x); | |
| position.y = Mathf.Clamp(position.y, minPosition.y, maxPosition.y); | |
| return position; | |
| } | |
| /// <summary> | |
| /// Called when a pointer down event is detected. | |
| /// </summary> | |
| /// <param name="eventData">Current event data.</param> | |
| public void OnPointerDown(PointerEventData eventData) | |
| => m_dragRectTransform.SetAsLastSibling(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment