I'm trying to centre an item at the end of a UITable list within a UIDraggablePanel. What I'm trying to do is that when someone clicks an item near the end and only part of it is shown, I want the whole item to be displayed instead of it being selected and only half shown.
I stumbled across an old forum
http://www.tasharen.com/forum/index.php?topic=180.0 and found the answer, however the math is slightly wrong. If I select the first or last item in the list it moves the entire draggable panel out of the panel clipping until I click it again. I tried to fix it with no luck. Any suggestions? (the code is below)
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(UIDraggablePanel))]
public class UIDraggablePanelSelectedObjectFollower : MonoBehaviour {
UIDraggablePanel draggablePanel;
Transform mTrans;
GameObject mGO;
GameObject lastFollowed;
void Awake() {
mGO = gameObject;
mTrans = transform;
draggablePanel = GetComponent<UIDraggablePanel>();
}
void Update() {
if (enabled && mGO.active && UICamera.selectedObject != null && UICamera.selectedObject != lastFollowed) {
if (!UICamera.selectedObject.transform.IsChildOf(draggablePanel.transform)) return;
var draggableBounds = draggablePanel.bounds;
var widgetBounds = NGUIMath.CalculateRelativeWidgetBounds(draggablePanel.transform, UICamera.selectedObject.transform);
UIPanel mPanel = GetComponent<UIPanel>();
Vector3 constraint = mPanel.CalculateConstrainOffset(widgetBounds.min, widgetBounds.max);
if (constraint.sqrMagnitude > 0.000001f) {
SpringPanel.Begin(mPanel.gameObject, mTrans.localPosition + constraint, 13f);
lastFollowed = UICamera.selectedObject;
}
}
}
}