Hi,
NGUI free doesn't come with UICenterOnChild, so I've made my own version, UICenterOnChildManual, which centers on a specific Transform as opposed to finding the center-most transform itself.
It's very much a stripped down version of the original, adapted for compatibility and use with my pickers.
Is it OK to include it in my asset store package?
Here it is :
using UnityEngine;
/// <summary>
/// Manual version of NGUI's UICenterOnChild -stripped down and adapted for compatibility with NGUI free
/// </summary>
public class UICenterOnChildManual : MonoBehaviour
{
UIDraggablePanel mDrag;
GameObject mCenteredObject;
UIPanel mDragPanel;
/// <summary>
/// Recenter the draggable list on targetTrans.
/// </summary>
public void CenterOnChild( Transform targetTrans )
{
if (mDrag == null)
{
mDrag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
mDragPanel
= gameObject
.GetComponent ( typeof ( UIPanel
) ) as UIPanel
;
if (mDrag == null)
{
Debug
.LogWarning(GetType
() + " requires " + typeof(UIDraggablePanel
) + " on a parent object in order to work",
this); enabled = false;
return;
}
}
if (mDragPanel == null) return;
// Calculate the panel's center in world coordinates
Vector4 clip = mDragPanel.clipRange;
Transform dt = mDragPanel.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);
}
}
Cheers,
Gregzo