Project_WL/Assets/ThirdParty/NGUI/Scripts/Interaction/UICenterOnClick.cs

53 lines
1.6 KiB
C#

//-------------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2019 Tasharen Entertainment Inc
//-------------------------------------------------
using UnityEngine;
/// <summary>
/// Attaching this script to an element of a scroll view will make it possible to center on it by clicking on it.
/// </summary>
[AddComponentMenu("NGUI/Interaction/Center Scroll View on Click")]
public class UICenterOnClick : MonoBehaviour
{
public void OnClick ()
{
UICenterOnChild center = NGUITools.FindInParents<UICenterOnChild>(gameObject);
UIPanel panel = NGUITools.FindInParents<UIPanel>(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<UIScrollView>();
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<UICenterOnChild>(gameObject);
if (center == null) center = transform.parent.gameObject.AddComponent<UICenterOnChild>();
center.bCenterOn = true;
center.enabled = true;
//Single.Coroutine.NextUpdate(() =>
//{
// center.CenterOn(transform);
//});
center.onFinished = () =>
{
center.enabled = false;
Destroy(this);
};
}
}