Files
Ipseity-Demo/Assets/Unheimlich_POC/Scripts/Utility/TweenHelper.cs
T
2026-01-30 17:39:26 +01:00

225 lines
8.1 KiB
C#

using UnityEngine;
using TMPro;
namespace Unheimlich.Utility
{
public static class TweenHelper
{
public static int FadeCanvasGroup(CanvasGroup canvasGroup, float targetAlpha,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInQuad)
{
if (canvasGroup == null) return -1;
LeanTween.cancel(canvasGroup.gameObject);
return LeanTween.alphaCanvas(canvasGroup, targetAlpha, duration)
.setEase(easeType)
.setOnComplete(onComplete)
.id;
}
public static int FadeText(TextMeshProUGUI text, float targetAlpha,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInQuad)
{
return FadeText(text?.gameObject, text, null, targetAlpha, duration, 1f, onComplete, easeType);
}
public static int FadeText(GameObject targetObject, TextMeshProUGUI mainText,
TextMeshProUGUI ghostText, float targetAlpha, float duration,
float ghostAlphaRatio, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInQuad)
{
if (mainText == null || targetObject == null) return -1;
float startAlpha = mainText.color.a;
LeanTween.cancel(targetObject);
return LeanTween.value(targetObject, startAlpha, targetAlpha, duration)
.setEase(easeType)
.setOnUpdate((float val) => {
if (mainText != null)
{
Color c = mainText.color;
c.a = val;
mainText.color = c;
}
if (ghostText != null)
{
Color gc = ghostText.color;
gc.a = val * ghostAlphaRatio;
ghostText.color = gc;
}
})
.setOnComplete(onComplete)
.id;
}
public static int ScaleLocal(GameObject target, Vector3 targetScale,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInOutQuad)
{
if (target == null) return -1;
LeanTween.cancel(target);
return LeanTween.scale(target, targetScale, duration)
.setEase(easeType)
.setOnComplete(onComplete)
.id;
}
public static int MoveLocal(GameObject target, Vector3 targetPosition,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInOutQuad)
{
if (target == null) return -1;
LeanTween.cancel(target);
return LeanTween.moveLocal(target, targetPosition, duration)
.setEase(easeType)
.setOnComplete(onComplete)
.id;
}
public static int RotateLocal(GameObject target, Vector3 targetRotation,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInOutQuad)
{
if (target == null) return -1;
LeanTween.cancel(target);
return LeanTween.rotate(target, targetRotation, duration)
.setEase(easeType)
.setOnComplete(onComplete)
.id;
}
public static int MoveLocalWithCurve(GameObject target, Vector3 targetPosition,
float duration, AnimationCurve curve, System.Action onComplete = null)
{
if (target == null) return -1;
LeanTween.cancel(target);
return LeanTween.moveLocal(target, targetPosition, duration)
.setEase(curve)
.setOnComplete(onComplete)
.id;
}
public static int FadeVolume(AudioSource source, float targetVolume,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInOutQuad)
{
if (source == null) return -1;
GameObject sourceObject = source.gameObject;
LeanTween.cancel(sourceObject);
return LeanTween.value(sourceObject, source.volume, targetVolume, duration)
.setEase(easeType)
.setOnUpdate((float val) => {
if (source != null)
source.volume = val;
})
.setOnComplete(onComplete)
.id;
}
public static int FadeVolume(GameObject target, float targetVolume,
float duration, System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.linear)
{
if (target == null) return -1;
float startVolume = AudioListener.volume;
LeanTween.cancel(target);
return LeanTween.value(target, startVolume, targetVolume, duration)
.setEase(easeType)
.setOnUpdate((float val) => {
AudioListener.volume = val;
})
.setOnComplete(onComplete)
.id;
}
public static int FloatAnimation(GameObject target, Vector3 basePosition,
float amplitude, float duration, float delay = 0f, Axis axis = Axis.Y)
{
if (target == null) return -1;
LeanTween.cancel(target);
return LeanTween.value(target, 0f, Mathf.PI * 2f, duration)
.setDelay(delay)
.setOnUpdate((float val) => {
if (target != null)
{
Vector3 newPos = basePosition;
float offset = Mathf.Sin(val) * amplitude;
switch (axis)
{
case Axis.X: newPos.x += offset; break;
case Axis.Y: newPos.y += offset; break;
case Axis.Z: newPos.z += offset; break;
}
target.transform.localPosition = newPos;
}
})
.setLoopClamp()
.setEase(LeanTweenType.linear)
.id;
}
public static int RotateAround(GameObject target, Vector3 axis, float degreesPerSecond)
{
if (target == null || degreesPerSecond <= 0f) return -1;
float duration = 360f / degreesPerSecond;
LeanTween.cancel(target);
return LeanTween.rotateAroundLocal(target, axis, 360f, duration)
.setLoopClamp()
.setEase(LeanTweenType.linear)
.id;
}
public static int AnimateMaterialFloat(GameObject target, Material material,
int propertyID, float fromValue, float toValue, float duration,
System.Action onComplete = null,
LeanTweenType easeType = LeanTweenType.easeInOutQuad)
{
if (target == null || material == null) return -1;
LeanTween.cancel(target);
return LeanTween.value(target, fromValue, toValue, duration)
.setEase(easeType)
.setOnUpdate((float val) => {
if (material != null)
material.SetFloat(propertyID, val);
})
.setOnComplete(onComplete)
.id;
}
public static void CancelTween(ref int tweenId)
{
if (tweenId != -1)
{
LeanTween.cancel(tweenId);
tweenId = -1;
}
}
}
public enum Axis
{
X, Y, Z
}
}