Hi,
I have got an image that I want to drag. The canvas is properly set up. The image as well(raycast target true).
Now I want to attatch the image to an gameobject (top layer) to have it render infront of everything else with OnBeginDrag. Sadly this function wont get called. OnDrag and OnEndDrag are being called however.
Any ideas?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler {
GameObject startparent;
RectTransform parentrect;
Vector3 startpos;
GameObject toplayer;
void Start(){
startparent = gameObject.transform.parent.gameObject;
parentrect = startparent.GetComponent<RectTransform> ();
toplayer = startparent.transform.parent.Find ("TopLayer").gameObject;
startpos = parentrect.localPosition;
toplayer.transform.localPosition = parentrect.localPosition;
}
public void OnBeginDrag(PointerEventData eventData){
gameObject.transform.SetParent (toplayer.transform, false);
Debug.Log ("Begin");
}
public void OnDrag(PointerEventData eventData){
Vector3 newpos = Input.mousePosition - new Vector3 (Screen.width / 2f, Screen.height / 2f, 0f) - startpos;
gameObject.transform.localPosition = newpos;
}
public void OnEndDrag(PointerEventData eventData){
gameObject.transform.SetParent (startparent.transform, false);
gameObject.transform.localPosition = Vector3.zero;
Debug.Log ("End");
}
}