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
@@ -1,13 +1,13 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using Unheimlich.Core;
namespace Unheimlich.UI
{
[RequireComponent(typeof(Camera))]
public class DialogueBlurEffectURP : MonoBehaviour
public class DialogueBlurEffectURP : Singleton<DialogueBlurEffectURP>
{
public static DialogueBlurEffectURP Instance { get; private set; }
[Header("Volume Settings")]
[SerializeField] private Volume _globalVolume;
@@ -43,15 +43,8 @@ namespace Unheimlich.UI
private Camera _baseCam;
private Camera _dialogueCam;
void Awake()
protected override void OnAwake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
_baseCam = GetComponent<Camera>();
if (_fadeCurve == null || _fadeCurve.keys.Length == 0)
@@ -1,11 +1,12 @@
using UnityEngine;
using TMPro;
using Unheimlich.Core;
using Unheimlich.Utility;
namespace Unheimlich.UI
{
public class DialogueScreenUI : MonoBehaviour
public class DialogueScreenUI : Singleton<DialogueScreenUI>
{
public static DialogueScreenUI Instance { get; private set; }
[Header("UI References")]
[SerializeField] private GameObject _dialoguePanel;
@@ -17,16 +18,8 @@ namespace Unheimlich.UI
private CanvasGroup _canvasGroup;
private void Awake()
protected override void OnAwake()
{
if (Instance != null && Instance != this)
{
Debug.LogWarning("[DialogueScreenUI] Multiple instances detected! Destroying duplicate.");
Destroy(gameObject);
return;
}
Instance = this;
if (_dialoguePanel != null)
{
@@ -37,12 +30,6 @@ namespace Unheimlich.UI
}
}
private void OnDestroy()
{
if (Instance == this)
Instance = null;
}
public void Show(string speaker, string dialogue)
{
if (_speakerText != null)
@@ -73,18 +60,19 @@ namespace Unheimlich.UI
{
if (_canvasGroup != null && _dialoguePanel != null)
{
LeanTween.cancel(_dialoguePanel);
LeanTween.alphaCanvas(_canvasGroup, 0f, _fadeOutDuration)
.setOnComplete(() =>
{
TweenHelper.FadeCanvasGroup(
_canvasGroup,
0f,
_fadeOutDuration,
() => {
Hide();
onComplete?.Invoke();
onComplete.SafeInvoke();
});
}
else
{
Hide();
onComplete?.Invoke();
onComplete.SafeInvoke();
}
}
}
+26 -28
View File
@@ -4,6 +4,7 @@ using TMPro;
using Unheimlich.Data.Dialogues;
using Unheimlich.Gameplay.Player;
using Unheimlich.Interactions.FloatingWords;
using Unheimlich.Utility;
namespace Unheimlich.UI
{
@@ -43,6 +44,10 @@ namespace Unheimlich.UI
private DialogueDisplayMode _currentDisplayMode = DialogueDisplayMode.World;
private int _fadeCanvasTweenId = -1;
private int _rotationTweenId = -1;
private List<int> _floatingTweenIds = new List<int>();
private static readonly Vector3 ViewportCenter = new Vector3(0.5f, 0.5f, 0f);
private void Awake()
@@ -91,7 +96,7 @@ namespace Unheimlich.UI
if (_dialogueCanvasGroup != null)
{
LeanTween.cancel(_dialoguePanel);
TweenHelper.CancelTween(ref _fadeCanvasTweenId);
_dialogueCanvasGroup.alpha = 1f;
}
}
@@ -125,9 +130,12 @@ namespace Unheimlich.UI
{
if (_dialogueCanvasGroup != null && _dialoguePanel != null)
{
LeanTween.cancel(_dialoguePanel);
LeanTween.alphaCanvas(_dialogueCanvasGroup, 0f, _fadeOutDuration)
.setOnComplete(() =>
TweenHelper.CancelTween(ref _fadeCanvasTweenId);
_fadeCanvasTweenId = TweenHelper.FadeCanvasGroup(
_dialogueCanvasGroup,
0f,
_fadeOutDuration,
() =>
{
HideDialogueBox();
onComplete?.Invoke();
@@ -247,35 +255,18 @@ namespace Unheimlich.UI
float amplitude = _currentAnimationSettings.FloatingAmplitude;
float duration = _currentAnimationSettings.FloatingDuration;
float offset = index * 0.2f;
float delay = index * 0.2f;
Vector3 startPos = choice.transform.localPosition;
LeanTween.value(choice, 0f, Mathf.PI * 2f, duration)
.setDelay(offset)
.setOnUpdate((float val) => {
if (choice != null)
{
Vector3 newPos = startPos;
newPos.y += Mathf.Sin(val) * amplitude;
choice.transform.localPosition = newPos;
}
})
.setLoopClamp()
.setEase(LeanTweenType.linear);
int tweenId = TweenHelper.FloatAnimation(choice, startPos, amplitude, duration, delay, Axis.Y);
_floatingTweenIds.Add(tweenId);
}
public void RotationAnimation(float degreesPerSecond = 60f)
{
if (degreesPerSecond <= 0f)
return;
float duration = 360f / degreesPerSecond;
LeanTween.cancel(ChoiceContainer);
LeanTween.rotateAroundLocal(ChoiceContainer, _currentRotationAxis, 360f, duration)
.setLoopClamp()
.setEase(LeanTweenType.linear);
TweenHelper.CancelTween(ref _rotationTweenId);
_rotationTweenId = TweenHelper.RotateAround(ChoiceContainer, _currentRotationAxis, degreesPerSecond);
}
/// <summary>
@@ -318,10 +309,17 @@ namespace Unheimlich.UI
_currentAnchor = null;
_currentAnimationSettings = null;
_activeChoices.Clear();
LeanTween.cancel(ChoiceContainer);
TweenHelper.CancelTween(ref _rotationTweenId);
foreach (int tweenId in _floatingTweenIds)
{
int id = tweenId;
TweenHelper.CancelTween(ref id);
}
_floatingTweenIds.Clear();
foreach (Transform child in ChoiceContainer.transform)
{
LeanTween.cancel(child.gameObject);
Destroy(child.gameObject);
}
}