Question by
esveebee · Feb 08, 2016 at 10:21 AM ·
canvasdragscreenspace
Unity UI does not visible when i drag using canvas set to screen space - camera
when i set canvas to screen space overlay, the UI is visible during dragging, but when i set the canvas to screen space camera it is not visible while dragging, and i just need drag in screen space camera. Here is my drag code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public Transform parentToReturnTo=null;
public void OnBeginDrag(PointerEventData eventData){
Debug.Log ("OnBeginDrag");
parentToReturnTo = this.transform.parent;
this.transform.SetParent (this.transform.parent.parent);
GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData){
Debug.Log ("OnDrag");
this.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData){
Debug.Log ("OnEndDrag");
this.transform.SetParent(parentToReturnTo);
GetComponent<CanvasGroup> ().blocksRaycasts = true;
}
}
Comment
Answer by boom2536 · Mar 01, 2016 at 08:20 AM
//if canvas screen space = camera
public void OnDrag(PointerEventData eventData){
Vector3 screenPoint = Input.mousePosition;
screenPoint.z = 100.0f; //distance of the plane from the camera
transform.position = Camera.main.ScreenToWorldPoint(screenPoint);
}
Your answer
Follow this Question
Related Questions
Get the relative dimensions of two different canvases 0 Answers
Trying to drag GameObject using touch input 0 Answers
rect transform, dont chance with the new version 5.3.4? 0 Answers
aspect menu in Game Window distorts entire Canvas in Scene Window 0 Answers
ScreenSpace canvas not working 3 Answers