- Home /
Question by
Sobe459 · Oct 25, 2021 at 07:50 AM ·
scrollviewdrag-and-dropparent-child
Draggable in Scroll View Returning to proper place on Drop
Hey all, What I am attempting to do is I have a scroll rect of draggable elements set up of "sticker critters". When I drag them out I set to the root transform so they arn't masked anymore to drag into a box where they can be dropped. I'm not worried about removing them from the roster. However because I'm using a grid layout component whenever I drop the draggable it goes to the bottom of the hierarchy in the inspector. If i want it to go back to the spot it can from what would be my best action moving forward? I was thinking adding a place holder when i pick up the draggle but how do i get the child index of the draggable? Thanks in advanced, will provide any other info.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DraggableRosterUICritter : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
public StickerCritterObject stickerCritter;
[SerializeField] private RosterManager rosterManager;
[SerializeField] private GameObject UIEmptySlotPrefab;
[SerializeField] private GameObject UIEmptySlotTemp;
[SerializeField] private Transform originalParent;
[SerializeField] private Vector3 startpos;
private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
private void Awake()
{
rosterManager = FindObjectOfType<RosterManager>();
originalParent = transform.parent;
canvas = transform.root.GetComponent<Canvas>();
rectTransform = GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("OnDrag");
rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
transform.SetParent(originalParent);
rectTransform.anchoredPosition = startpos;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("OnPointerDown");
startpos = rectTransform.anchoredPosition;
transform.SetParent(transform.root);
}
}
Comment