using DG.Tweening;
using UnityEngine;
namespace UGUITweener
{
[ExecuteInEditMode]
public class TweenPosition : TweenerSetting
{
private Component component;
public Vector3 from = Vector3.zero;
public Vector3 to = Vector3.zero;
public bool World;
public override void TweenPlay(bool value)
{
if (component)
{
Vector3 startValue = value ? from : to;
Vector3 endvalue = value ? to : from;
if (component.GetType() == typeof(RectTransform))
{
((RectTransform)component).anchoredPosition3D = startValue;
((RectTransform)component).DOKill();
tween = ((RectTransform)component).DOAnchorPos3D(endvalue, duration).OnComplete(unityEvent.Invoke);
}
else
{
if (World)
{
((Transform)component).position = startValue;
}
else
{
((Transform)component).localPosition = startValue;
}
((Transform)component).DOKill();
if (World)
{
tween = ((Transform)component).DOMove(endvalue, duration).OnComplete(unityEvent.Invoke);
}
else
{
tween = ((Transform)component).DOLocalMove(endvalue, duration).OnComplete(unityEvent.Invoke);
}
}
SetTweenData();
}
}
public override void SetStartToCurrentValue()
{
base.SetStartToCurrentValue();
component = (Component)GetComponent<RectTransform>() ? (Component)GetComponent<RectTransform>() : (Component)GetComponent<Transform>();
if (component.GetType() == typeof(Transform))
{
World = true;
}
}
#if UNITY_EDITOR
[ContextMenu("设置From")]
private void SetPosition()
{
if (component)
{
if (component.GetType() == typeof(RectTransform))
{
from = ((RectTransform)component).anchoredPosition3D;
}
else
{
if (World)
{
from = ((Transform)component).position;
}
else
{
from = ((Transform)component).localPosition;
}
}
}
}
[ContextMenu("设置 Transform")]
private void SetTransform()
{
if (component)
{
if (component.GetType() == typeof(RectTransform))
{
((RectTransform)component).anchoredPosition3D = from;
}
else
{
if (World)
{
((Transform)component).position = from;
}
else
{
((Transform)component).localPosition = from;
}
}
}
}
#endif
}
}