//------------------------------------------------- // NGUI: Next-Gen UI kit // Copyright © 2011-2019 Tasharen Entertainment Inc //------------------------------------------------- using UnityEngine; /// /// Attaching this script to an element of a scroll view will make it possible to center on it by clicking on it. /// [AddComponentMenu("NGUI/Interaction/Center Scroll View on Click")] public class UICenterOnClick : MonoBehaviour { public void OnClick () { UICenterOnChild center = NGUITools.FindInParents(gameObject); UIPanel panel = NGUITools.FindInParents(gameObject); if (center != null) { center.bCenterOn = true; if (center.enabled) center.CenterOn(transform); } else if (panel != null && panel.clipping != UIDrawCall.Clipping.None) { UIScrollView sv = panel.GetComponent(); Vector3 offset = -panel.cachedTransform.InverseTransformPoint(transform.position); if (!sv.canMoveHorizontally) offset.x = panel.cachedTransform.localPosition.x; if (!sv.canMoveVertically) offset.y = panel.cachedTransform.localPosition.y; SpringPanel.Begin(panel.cachedGameObject, offset, 6f); } } public void OnClick_byCenterOnChild() { UICenterOnChild center = NGUITools.FindInParents(gameObject); if (center == null) center = transform.parent.gameObject.AddComponent(); center.bCenterOn = true; center.enabled = true; //Single.Coroutine.NextUpdate(() => //{ // center.CenterOn(transform); //}); center.onFinished = () => { center.enabled = false; Destroy(this); }; } }