- Home /
Question on Tutorial: Panes, Panels, Windows
I took this tutorial on Panes, Panels, and Windows that discussed, among other things, how to "Drag" Panels which is a specific thing I need to do. Here is the tutorial: https://unity3d.com/fr/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows
Anyway, I took the tutorial, used the provided code, but I still can't get my panel to move. Hmmmm. I tried attaching the script to every object, the panel, the canvas, an empty game object, etc. but I haven't had any success. Maybe there is something simple that I'm missing. Here is the c# code provided in the tutorial.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
private Vector2 pointerOffset;
private RectTransform canvasRectTransform;
private RectTransform panelRectTransform;
void Awake () {
Canvas canvas = GetComponentInParent <Canvas>();
if (canvas != null) {
canvasRectTransform = canvas.transform as RectTransform;
panelRectTransform = transform.parent as RectTransform;
}
}
public void OnPointerDown (PointerEventData data) {
panelRectTransform.SetAsLastSibling ();
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out pointerOffset);
}
public void OnDrag (PointerEventData data) {
if (panelRectTransform == null)
return;
Vector2 pointerPostion = ClampToWindow (data);
Vector2 localPointerPosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (
canvasRectTransform, pointerPostion, data.pressEventCamera, out localPointerPosition
)) {
panelRectTransform.localPosition = localPointerPosition - pointerOffset;
}
}
Vector2 ClampToWindow (PointerEventData data) {
Vector2 rawPointerPosition = data.position;
Vector3[] canvasCorners = new Vector3[4];
canvasRectTransform.GetWorldCorners (canvasCorners);
float clampedX = Mathf.Clamp (rawPointerPosition.x, canvasCorners[0].x, canvasCorners[2].x);
float clampedY = Mathf.Clamp (rawPointerPosition.y, canvasCorners[0].y, canvasCorners[2].y);
Vector2 newPointerPosition = new Vector2 (clampedX, clampedY);
return newPointerPosition;
}
}
I tried adding colliders, rigidbodies, etc. No luck. I even tried importing some of the standard assets scripts ("DragRigidbody") and work with those but I've yet to get my panel to budge. I just need to be able to reposition a panel (or panels) on my canvas.
Yes. That was added when I added the original UI Panel. Both a Canvas and EventSystem were added.
Thanks, by the way for taking the time to help me.
I have some stuff I need to do this morning but after that I'll watch the tutorial video again (and some of the others). $$anonymous$$aybe there is something simple I'm missing.