ProjectDDD/Packages/SLUnity/SLUI/SLUIMoveTo.cs
2025-06-25 11:33:17 +09:00

135 lines
4.2 KiB
C#

using UnityEngine;
namespace Superlazy.UI
{
public class SLUIMoveTo : SLUIOnOffTransition
{
public float startDelay = 0.0f;
public bool useTimeBase = true;
public float onDuration = 0.3f;
public float offDuration = 0.0f;
public bool useSpeedBase = false;
public float onMoveSpeed = 1.0f;
public float offMoveSpeed = 1.0f;
public float scaleTo = 1.0f;
public bool moveArc = false;
public string targetPositionBind;
public string endEffect;
//public Vector3 offset;
private RectTransform rectTransform;
private float currentTime;
private readonly float currentDelay;
protected override void Validate()
{
rectTransform = GetComponent<RectTransform>();
}
protected override void Init()
{
if (currentState == false)
{
currentTime = 0;
}
else
{
currentTime = 1;
}
}
protected override bool OnUpdate(bool forceUpdate = false)
{
var result = false;
var defaultPosition = rectTransform.parent.TransformPoint(Vector3.zero);
var pos = SLGame.Session.Get(targetPositionBind);
var targetPosition = new Vector3(pos["X"], pos["Y"], pos["Z"]);
var distance = (defaultPosition - targetPosition).magnitude;
if (useSpeedBase)
{
onDuration = onMoveSpeed == 0 ? 0 : distance / onMoveSpeed;
offDuration = offMoveSpeed == 0 ? 0 : distance / offMoveSpeed;
}
var ease = currentState ? 0.0f : 1.0f;
if (currentState)
{
if (onDuration == 0)
{
currentTime = onDuration;
}
else
{
currentTime += Time.unscaledDeltaTime;
if ((currentTime - startDelay) > onDuration) currentTime = onDuration + startDelay;
if (currentTime > startDelay)
{
ease = (currentTime - startDelay) / onDuration;
}
else
{
ease = 0;
}
}
if (currentTime - startDelay >= onDuration)
{
if (string.IsNullOrEmpty(endEffect) == false)
{
SLGame.Event(endEffect, SLEntity.Empty);
}
result = true;
}
}
else
{
if (offDuration == 0)
{
currentTime = 0;
}
else
{
currentTime -= Time.unscaledDeltaTime;
if (currentTime < 0) currentTime = 0;
ease = currentTime / offDuration;
}
if (currentTime <= 0.0f)
{
currentTime = 0.0f;
result = true;
}
}
var newScale = Mathf.Lerp(1.0f, scaleTo, ease);
rectTransform.localScale = new Vector3(newScale, newScale, newScale);
if (moveArc)
{
var newX = Mathf.Lerp(defaultPosition.x, targetPosition.x, Mathf.Pow(ease, 0.7f));
var newY = Mathf.Lerp(defaultPosition.y, targetPosition.y, 1 - Mathf.Pow(1 - ease, 0.7f));
var newZ = Mathf.Lerp(defaultPosition.z, targetPosition.z, Mathf.Pow(ease, 0.7f));
var newPosition = new Vector3(newX, newY, newZ);
rectTransform.position = newPosition;
}
else
{
var newPosition = Vector3.Lerp(defaultPosition, targetPosition, ease);
rectTransform.position = newPosition;
}
return result;
}
protected override void ForceDisable()
{
currentTime = 0.0f;
rectTransform.localPosition = Vector3.zero;
}
}
}