Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

IDragHandler

interface in UnityEngine.EventSystems


Implements interfaces: IEventSystemHandler

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

Interface to implement if you wish to receive OnDrag callbacks.

#pragma strict
@RequireComponent(Image)
public class DragMe extends MonoBehaviour {
	public var dragOnSurfaces: boolean = true;
	private var m_DraggingIcon: GameObject;
	private var m_DraggingPlane: RectTransform;
	public function OnBeginDrag(eventData: PointerEventData) {
		var canvas: var = FindInParents.<Canvas>(gameObject);
		if (canvas == null)return ;
		// What we want to do is create an icon for this.
		m_DraggingIcon = new GameObject("icon");
		m_DraggingIcon.transform.SetParent(canvas.transform, false);
		m_DraggingIcon.transform.SetAsLastSibling();
		var image: var = m_DraggingIcon.AddComponent.<Image>();
		// We want it to be ignored by the event system.
		m_DraggingIcon.AddComponent.<IgnoreRaycast>();
		image.sprite = GetComponent.<Image>().sprite;
		image.SetNativeSize();
		if (dragOnSurfaces)
			m_DraggingPlane = transform as RectTransform;
		else
			m_DraggingPlane = canvas.transform as RectTransform;
		SetDraggedPosition(eventData);
	}
	public function OnDrag(data: PointerEventData) {
		if (m_DraggingIcon != null)
			SetDraggedPosition(data);
	}
	private function SetDraggedPosition(data: PointerEventData) {
		if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
			m_DraggingPlane = data.pointerEnter.transform as RectTransform;
		var rt: var = m_DraggingIcon.GetComponent.<RectTransform>();
		var globalMousePos: Vector3;
		if (RectTransformUtility.PointInRectangleGlobalSpace(m_DraggingPlane, globalMousePos, data.position, data.pressEventCamera)) {
			rt.position = globalMousePos;
			rt.rotation = m_DraggingPlane.rotation;
		}
	}
	public function OnEndDrag(eventData: PointerEventData) {
		if (m_DraggingIcon != null)
			Destroy(m_DraggingIcon);
	}
	public static function FindInParents(go: GameObject) {
		if (go == null)return null;
		var comp: var = go.GetComponent.<T>();
		if (comp != null)return comp;
		var t: Transform = go.transform.parent;
		while ( t != null && comp == null ) {
			comp = t.gameObject.GetComponent.<T>();
			t = t.parent;
		}
		return comp;
	}
}
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Image))] public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public bool dragOnSurfaces = true; private GameObject m_DraggingIcon; private RectTransform m_DraggingPlane;

public void OnBeginDrag(PointerEventData eventData) { var canvas = FindInParents<Canvas>(gameObject); if (canvas == null) return;

// We have clicked something that can be dragged. // What we want to do is create an icon for this. m_DraggingIcon = new GameObject("icon");

m_DraggingIcon.transform.SetParent (canvas.transform, false); m_DraggingIcon.transform.SetAsLastSibling(); var image = m_DraggingIcon.AddComponent<Image>(); // The icon will be under the cursor. // We want it to be ignored by the event system. m_DraggingIcon.AddComponent<IgnoreRaycast>();

image.sprite = GetComponent<Image>().sprite; image.SetNativeSize(); if (dragOnSurfaces) m_DraggingPlane = transform as RectTransform; else m_DraggingPlane = canvas.transform as RectTransform; SetDraggedPosition(eventData); }

public void OnDrag(PointerEventData data) { if (m_DraggingIcon != null) SetDraggedPosition(data); }

private void SetDraggedPosition(PointerEventData data) { if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null) m_DraggingPlane = data.pointerEnter.transform as RectTransform; var rt = m_DraggingIcon.GetComponent<RectTransform>(); Vector3 globalMousePos; if (RectTransformUtility.PointInRectangleGlobalSpace(m_DraggingPlane, out globalMousePos, data.position, data.pressEventCamera)) { rt.position = globalMousePos; rt.rotation = m_DraggingPlane.rotation; } }

public void OnEndDrag(PointerEventData eventData) { if (m_DraggingIcon != null) Destroy(m_DraggingIcon); }

static public T FindInParents<T>(GameObject go) where T : Component { if (go == null) return null; var comp = go.GetComponent<T>();

if (comp != null) return comp; Transform t = go.transform.parent; while (t != null && comp == null) { comp = t.gameObject.GetComponent<T>(); t = t.parent; } return comp; } }

Public Functions

OnDragWhen draging is occuring this will be called every time the cursor is moved.