Add utils + refacto

This commit is contained in:
2026-01-30 17:39:26 +01:00
parent 9ff17b5305
commit 23d3cb5d2c
29 changed files with 675 additions and 461 deletions
@@ -0,0 +1,66 @@
using UnityEngine;
namespace Unheimlich.Utility
{
public static class RaycastHelper
{
private static readonly Vector3 ViewportCenter = new Vector3(0.5f, 0.5f, 0f);
public static bool RaycastFromCameraCenter(Camera camera, float maxDistance,
LayerMask layerMask, out RaycastHit hit)
{
if (camera == null)
{
hit = default;
return false;
}
Ray ray = camera.ViewportPointToRay(ViewportCenter);
return Physics.Raycast(ray, out hit, maxDistance, layerMask);
}
public static bool RaycastFromCameraCenter<T>(Camera camera, float maxDistance,
LayerMask layerMask, out T component) where T : Component
{
component = null;
if (RaycastFromCameraCenter(camera, maxDistance, layerMask, out RaycastHit hit))
{
component = hit.collider.GetComponent<T>();
return component != null;
}
return false;
}
public static bool ThrottledRaycast(ref float timer, float interval,
Camera camera, float maxDistance, LayerMask layerMask, out RaycastHit hit)
{
timer += Time.deltaTime;
if (timer >= interval)
{
timer = 0f;
return RaycastFromCameraCenter(camera, maxDistance, layerMask, out hit);
}
hit = default;
return false;
}
public static bool ThrottledRaycastComponent<T>(ref float timer, float interval,
Camera camera, float maxDistance, LayerMask layerMask, out T component) where T : Component
{
timer += Time.deltaTime;
if (timer >= interval)
{
timer = 0f;
return RaycastFromCameraCenter(camera, maxDistance, layerMask, out component);
}
component = null;
return false;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f37b9866154109148b3f7d2d3c121c36
@@ -0,0 +1,224 @@
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
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: f70df97fd94f86a4aa2cb00353624ba4
@@ -0,0 +1,47 @@
using UnityEngine;
namespace Unheimlich.Utility
{
public static class UnityExtensions
{
public static void SafeInvoke(this System.Action action)
{
action?.Invoke();
}
public static void SafeInvoke<T>(this System.Action<T> action, T arg)
{
action?.Invoke(arg);
}
public static Color WithAlpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
public static Vector3 WithY(this Vector3 vector, float y)
{
vector.y = y;
return vector;
}
public static Vector3 WithX(this Vector3 vector, float x)
{
vector.x = x;
return vector;
}
public static Vector3 WithZ(this Vector3 vector, float z)
{
vector.z = z;
return vector;
}
public static void CancelAllTweens(this GameObject gameObject)
{
if (gameObject != null)
LeanTween.cancel(gameObject);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: b5c8821940a7678458f26015ee167fdf