-
Notifications
You must be signed in to change notification settings - Fork 0
/
HintSystem.cs
91 lines (73 loc) · 2.51 KB
/
HintSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
public class HintSystem : MonoBehaviour
{
[Header("UI Elements")]
public CanvasGroup hintUIPanel;
public TMP_Text hintUIText;
[Header("Place Scriptable Hint Assets")]
public List<HintAsset> hintAssets;
[Header("Control Lerping")]
[SerializeField] private float fadeDurationVar = 0.5f;
private Dictionary<string, HintAsset.HintText> hintDictionary = new();
private void Start()
{
//Initialize the hint UI panel as invisible
hintUIPanel.alpha = 0f;
//Subscribe all GStrings in the HintAsset to localisation
foreach (var hintAsset in hintAssets)
{
foreach(var hint in hintAsset.HintList)
{
hint.Text.SubscribeGloc();
hintDictionary[hint.HintKey] = hint; // Populate dictionary for quick lookup
}
}
}
public void ShowHint(string hintKey)
{
if(hintDictionary.TryGetValue(hintKey, out var hint))
{
StartCoroutine(HandleHintText(hint));
}
else
{
Debug.LogWarning($"Hint with key '{hintKey}' not found. ");
}
}
private IEnumerator HandleHintText(HintAsset.HintText hint)
{
//Fade in panel
float elapsedTime = 0f;
float fadeDuration = fadeDurationVar;
float startAlpha = hintUIPanel.alpha;
float targetAlpha = 1f;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
hintUIPanel.alpha = Mathf.Lerp(startAlpha, targetAlpha, elapsedTime / fadeDuration);
yield return null;
}
hintUIPanel.alpha = targetAlpha; //Ensure final value is set
//Set the hint text
hintUIText.text = hint.Text.Value;
//Wait for the specified hint duration
yield return new WaitForSeconds(hint.Time);
//Fade out the panel
elapsedTime = 0f;
startAlpha = hintUIPanel.alpha;
targetAlpha = 0f;
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
hintUIPanel.alpha = Mathf.Lerp(startAlpha, targetAlpha, elapsedTime / fadeDuration);
yield return null;
}
//Ensure the final value is set
hintUIPanel.alpha = targetAlpha;
}
}