/// /// Created by SWAN DEV 2018 /// using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /// Dynamic UI - Draggable UI script for UGUI RaycastTarget components(e.g. Image, RawImage, Text). Attach on the UGUI component, set 'RaycastTarget = True'. /// (Draggable UI detect the drag area by checking the sizeDelta of the target UI rectTransform, make sure sizeDelta is correctly set. Especially for text.) public class DDraggableUI : MonoBehaviour { /// Indicates if any DDraggableUI object in the scene is being dragged. public static bool draggingGlobal{ get; private set; } /// The target draggable UGUI component(Image, RawImage, Text, etc). [Tooltip("The target draggable UGUI component(Image, RawImage, Text, etc).")] public GameObject m_TargetUIObject; /// Move this GameObject instead of the (dragged) TargetUIObject if this GameObject is assigned. [Tooltip("Move this GameObject instead of the (dragged) TargetUIObject if this GameObject is assigned.")] public GameObject m_MoveUIObject = null; /// Lock position change along X-axis. [Tooltip("Lock position change along X-axis.")] public bool m_LockX = false; /// Lock position change along Y-axis. [Tooltip("Lock position change along Y-axis.")] public bool m_LockY = false; /// Indicates if m_TargetUIObject is being dragged. public bool dragging{ get; private set; } private Action _onDragStart = null; public void SetOnDragStartCallback(Action onDragStartAction) { _onDragStart = onDragStartAction; } private Action _onDragComplete = null; public void SetOnDragCompleteCallback(Action onDragCompleteAction) { _onDragComplete = onDragCompleteAction; } bool isTouchDown = false; bool isTouchUp = true; float diffX = 0f, diffY = 0f; PointerEventData cursor = new PointerEventData(EventSystem.current); List objectsHit = new List(); void OnEnable() { if(m_TargetUIObject == null) m_TargetUIObject = gameObject; if(m_MoveUIObject == null) m_MoveUIObject = m_TargetUIObject; dragging = false; isTouchUp = true; } void OnDisable() { draggingGlobal = false; dragging = false; } void Update() { if(isTouchUp) { isTouchDown = SDev.Util.UnifyInputUtil.WasTouchOrMousePressed(); if (isTouchDown) { //A mouse/touch down event detected. isTouchUp = false; cursor.position = SDev.Util.UnifyInputUtil.GetCursorPosition(); EventSystem.current.RaycastAll(cursor, objectsHit); if(objectsHit.Count > 0) { if(objectsHit[0].gameObject.Equals(m_TargetUIObject)) { dragging = true; diffX = m_MoveUIObject.transform.position.x - cursor.position.x; diffY = m_MoveUIObject.transform.position.y - cursor.position.y; if(_onDragStart != null) _onDragStart(); } } } } else if(isTouchDown) { isTouchUp = SDev.Util.UnifyInputUtil.WasTouchOrMouseReleased(); if (isTouchUp) { //A mouse/touch up event detected. isTouchDown = false; dragging = false; draggingGlobal = false; if(_onDragComplete != null) _onDragComplete(); } } if(dragging) { draggingGlobal = true; cursor.position = SDev.Util.UnifyInputUtil.GetCursorPosition(); m_MoveUIObject.transform.position = new Vector3((m_LockX? m_MoveUIObject.transform.position.x : cursor.position.x + diffX), (m_LockY? m_MoveUIObject.transform.position.y : cursor.position.y + diffY), m_MoveUIObject.transform.position.z); } } public static void SetDraggable(GameObject targetUIObject, bool isDraggable) { if(isDraggable) { if(targetUIObject.GetComponent() == null) { targetUIObject.AddComponent(); } } else { if(targetUIObject.GetComponent() != null) { Destroy(targetUIObject.GetComponent()); } } } }