123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using UnityEngine;
- using UnityEngine.EventSystems;
- namespace Rokid.UXR.Components {
- public class DragUI : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
- {
- [SerializeField]
- public RectTransform canvas;
- [SerializeField]
- public Transform frontParent;
- private RectTransform imgRect;
- private CanvasGroup canvasGroup;
- private Transform oldParent;
- private Transform nowParent;
- public int sibiling;
- private Vector2 offset;
- void Start()
- {
-
- imgRect = GetComponent<RectTransform>();
- canvasGroup = GetComponent<CanvasGroup>();
- sibiling = transform.GetSiblingIndex();
- oldParent = transform.parent;
- }
- public void OnDrag(PointerEventData eventData)
- {
-
- Vector2 mouseDrag = eventData.position;
- Vector2 uguiPos;
- bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos);
- if (isRect)
- {
-
- imgRect.anchoredPosition = offset + uguiPos;
- }
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
-
- canvasGroup.blocksRaycasts = false;
-
- nowParent = transform.parent;
- transform.SetParent(frontParent);
- Vector2 mouseDown = eventData.position;
- Vector2 mouseUguiPos;
- bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
- if (isRect)
- {
- offset = imgRect.anchoredPosition - mouseUguiPos;
- }
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- RKLog.Info("On End Drag");
- if (eventData.pointerCurrentRaycast.gameObject != null)
- {
- if (eventData.pointerCurrentRaycast.gameObject.name == "Grid")
- {
- Transform targetParent = eventData.pointerCurrentRaycast.gameObject.transform;
- if (targetParent.childCount > 0)
- {
- RKLog.Info("Change Position");
-
- Transform target = targetParent.GetChild(0);
- target.SetParent(oldParent);
- target.localPosition = Vector3.zero;
- }
- transform.SetParent(targetParent);
- transform.localPosition = Vector3.zero;
- }
- else
- {
-
- transform.SetParent(oldParent);
- transform.localPosition = Vector3.zero;
- }
- }
- else
- {
-
- transform.SetParent(oldParent);
- transform.localPosition = Vector3.zero;
- }
- canvasGroup.blocksRaycasts = true;
- }
- }
- }
|