/// <summary> /// Automatically find the draggable panel if possible. /// </summary> void Start() { if (draggablePanel == null) { draggablePanel = NGUITools.FindInParents <NGUIDraggablePanel>(gameObject); } }
/// <summary> /// Position the grid's contents when the script starts. /// </summary> void Start() { mStarted = true; if (keepWithinPanel) { mPanel = NGUITools.FindInParents <NGUIPanel>(gameObject); mDrag = NGUITools.FindInParents <NGUIDraggablePanel>(gameObject); } Reposition(); }
/// <summary> /// Backwards compatibility. /// </summary> void Awake() { // Legacy functionality support for backwards compatibility if (panel != null) { if (draggablePanel == null) { draggablePanel = panel.GetComponent <NGUIDraggablePanel>(); if (draggablePanel == null) { draggablePanel = panel.gameObject.AddComponent <NGUIDraggablePanel>(); } } panel = null; } }
/// <summary> /// Backwards compatibility. /// </summary> void Awake() { // Legacy functionality support for backwards compatibility if (panel != null) { if (draggablePanel == null) { draggablePanel = panel.GetComponent<NGUIDraggablePanel>(); if (draggablePanel == null) { draggablePanel = panel.gameObject.AddComponent<NGUIDraggablePanel>(); } } panel = null; } }
/// <summary> /// Recenter the draggable list on the center-most child. /// </summary> public void Recenter() { if (mDrag == null) { mDrag = NGUITools.FindInParents <NGUIDraggablePanel>(gameObject); if (mDrag == null) { Debug.LogWarning(GetType() + " requires " + typeof(NGUIDraggablePanel) + " on a parent object in order to work", this); enabled = false; return; } else { mDrag.onDragFinished = OnDragFinished; if (mDrag.horizontalScrollBar != null) { mDrag.horizontalScrollBar.onDragFinished = OnDragFinished; } if (mDrag.verticalScrollBar != null) { mDrag.verticalScrollBar.onDragFinished = OnDragFinished; } } } 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 Vector3 offsetCenter = center - mDrag.currentMomentum * (mDrag.momentumAmount * 0.1f); mDrag.currentMomentum = Vector3.zero; float min = float.MaxValue; Transform closest = null; Transform trans = transform; // Determine the closest child for (int i = 0, imax = trans.childCount; i < imax; ++i) { Transform t = trans.GetChild(i); float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter); if (sqrDist < min) { min = sqrDist; closest = t; } } if (closest != null) { mCenteredObject = closest.gameObject; // Figure out the difference between the chosen child and the panel's center in local coordinates Vector3 cp = dt.InverseTransformPoint(closest.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, springStrength).onFinished = onFinished; } else { mCenteredObject = null; } }
/// <summary> /// Cache the transform. /// </summary> void Start() { mPanel = GetComponent<NGUIPanel>(); mDrag = GetComponent<NGUIDraggablePanel>(); mTrans = transform; }
/// <summary> /// Recenter the draggable list on the center-most child. /// </summary> public void Recenter() { if (mDrag == null) { mDrag = NGUITools.FindInParents<NGUIDraggablePanel>(gameObject); if (mDrag == null) { Debug.LogWarning(GetType() + " requires " + typeof(NGUIDraggablePanel) + " on a parent object in order to work", this); enabled = false; return; } else { mDrag.onDragFinished = OnDragFinished; if (mDrag.horizontalScrollBar != null) mDrag.horizontalScrollBar.onDragFinished = OnDragFinished; if (mDrag.verticalScrollBar != null) mDrag.verticalScrollBar.onDragFinished = OnDragFinished; } } 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 Vector3 offsetCenter = center - mDrag.currentMomentum * (mDrag.momentumAmount * 0.1f); mDrag.currentMomentum = Vector3.zero; float min = float.MaxValue; Transform closest = null; Transform trans = transform; // Determine the closest child for (int i = 0, imax = trans.childCount; i < imax; ++i) { Transform t = trans.GetChild(i); float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter); if (sqrDist < min) { min = sqrDist; closest = t; } } if (closest != null) { mCenteredObject = closest.gameObject; // Figure out the difference between the chosen child and the panel's center in local coordinates Vector3 cp = dt.InverseTransformPoint(closest.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, springStrength).onFinished = onFinished; } else mCenteredObject = null; }
/// <summary> /// Position the grid's contents when the script starts. /// </summary> void Start() { mStarted = true; if (keepWithinPanel) { mPanel = NGUITools.FindInParents<NGUIPanel>(gameObject); mDrag = NGUITools.FindInParents<NGUIDraggablePanel>(gameObject); } Reposition(); }
/// <summary> /// Recalculate the position of all elements within the grid, sorting them alphabetically if necessary. /// </summary> public void Reposition() { if (!mStarted) { repositionNow = true; return; } Transform myTrans = transform; int x = 0; int y = 0; if (sorted) { List <Transform> list = new List <Transform>(); for (int i = 0; i < myTrans.childCount; ++i) { Transform t = myTrans.GetChild(i); if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) { list.Add(t); } } list.Sort(SortByName); for (int i = 0, imax = list.Count; i < imax; ++i) { Transform t = list[i]; if (!NGUITools.GetActive(t.gameObject) && hideInactive) { continue; } float depth = t.localPosition.z; t.localPosition = (arrangement == Arrangement.Horizontal) ? new Vector3(cellWidth * x, -cellHeight * y, depth) : new Vector3(cellWidth * y, -cellHeight * x, depth); if (++x >= maxPerLine && maxPerLine > 0) { x = 0; ++y; } } } else { for (int i = 0; i < myTrans.childCount; ++i) { Transform t = myTrans.GetChild(i); if (!NGUITools.GetActive(t.gameObject) && hideInactive) { continue; } float depth = t.localPosition.z; t.localPosition = (arrangement == Arrangement.Horizontal) ? new Vector3(cellWidth * x, -cellHeight * y, depth) : new Vector3(cellWidth * y, -cellHeight * x, depth); if (++x >= maxPerLine && maxPerLine > 0) { x = 0; ++y; } } } NGUIDraggablePanel drag = NGUITools.FindInParents <NGUIDraggablePanel>(gameObject); if (drag != null) { drag.UpdateScrollbars(true); } }
/// <summary> /// Cache the transform. /// </summary> void Start() { mPanel = GetComponent <NGUIPanel>(); mDrag = GetComponent <NGUIDraggablePanel>(); mTrans = transform; }
/// <summary> /// Automatically find the draggable panel if possible. /// </summary> void Start() { if (draggablePanel == null) { draggablePanel = NGUITools.FindInParents<NGUIDraggablePanel>(gameObject); } }