Last active
February 26, 2022 10:03
-
-
Save neon-izm/9f6d1a35b036ab4241bec45f065fe69b to your computer and use it in GitHub Desktop.
Draggable uGUI components,I tested only canvas scaler+ screen space(overlay)
This file contains 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
[RequireComponent(typeof(RectTransform))] | |
public class DraggableImage : MonoBehaviour, IDragHandler | |
{ | |
private RectTransform _rectTransform = null; | |
private Canvas _parentCanvas; | |
private void Awake() | |
{ | |
_rectTransform = GetComponent<RectTransform>(); | |
} | |
public void OnDrag(PointerEventData e) | |
{ | |
if (_parentCanvas == null) | |
{ | |
_parentCanvas = GetComponentInParent<Canvas>(); | |
} | |
var dragDelta = new Vector2(e.delta.x, e.delta.y); | |
//need scale factor for coefficient movement parametor | |
var scale = _parentCanvas.scaleFactor; | |
_rectTransform.anchoredPosition += dragDelta/scale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment