Skip to content

Instantly share code, notes, and snippets.

@neon-izm
Last active February 26, 2022 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neon-izm/9f6d1a35b036ab4241bec45f065fe69b to your computer and use it in GitHub Desktop.
Save neon-izm/9f6d1a35b036ab4241bec45f065fe69b to your computer and use it in GitHub Desktop.
Draggable uGUI components,I tested only canvas scaler+ screen space(overlay)
[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