public class UICenterOnChildManual : MonoBehaviour
{
UIDraggablePanel mDrag;
GameObject mCenteredObject;
public SpringPanel.OnFinished onFinished;
void Awake ()
{
mDrag
= gameObject
.GetComponent ( typeof ( UIDraggablePanel
) ) as UIDraggablePanel
; //Test
onFinished += Test; // for debugging purposes
}
/// <summary>
/// Recenter the draggable panel on targetTrans.
/// </summary>
public void CenterOnChild( Transform targetTrans )
{
Debug.Log ("CenterOnChild called at "+Time.time);
if (mDrag.panel == null) return;
// Calculate the panel's center in world coordinates
Vector4 clip = mDrag.panel.clipRange;
Transform dt = mDrag.panel.cachedTransform;
Vector3 center = dt.localPosition;
center.x += clip.x;
center.y += clip.y;
center = dt.parent.TransformPoint(center);
// Offset this value by the momentum
mDrag.currentMomentum = Vector3.zero;
// Figure out the difference between the chosen child and the panel's center in local coordinates
Vector3 cp = dt.InverseTransformPoint(targetTrans.position);
Vector3 cc = dt.InverseTransformPoint(center);
Vector3 offset = cp - cc;
// Offset shouldn't occur if blocked by a zeroed-out scale
if (mDrag.scale.x == 0f) offset.x = 0f;
if (mDrag.scale.y == 0f) offset.y = 0f;
if (mDrag.scale.z == 0f) offset.z = 0f;
// Spring the panel to this calculated position
SpringPanel.Begin(mDrag.gameObject, dt.localPosition - offset, 8f).onFinished = onFinished;
}
void Test ()
{
Debug.Log ("SpringPanel finished at "+Time.time);
}
}