-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTooltipManager.cs
More file actions
102 lines (84 loc) · 2.5 KB
/
TooltipManager.cs
File metadata and controls
102 lines (84 loc) · 2.5 KB
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
92
93
94
95
96
97
98
99
100
101
102
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections;
public class TooltipManager : MonoBehaviour
{
public static TooltipManager Instance { get; private set; }
[Header("UI References")]
public GameObject tooltipPanel;
public TextMeshProUGUI tooltipText;
public RectTransform tooltipRect;
[Header("Settings")]
public float showDelay = 0.5f;
public float fadeSpeed = 0.2f;
public Vector2 offset = new Vector2(10f, 10f);
private CanvasGroup canvasGroup;
private Coroutine showCoroutine;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
canvasGroup = tooltipPanel.GetComponent<CanvasGroup>();
if (canvasGroup == null)
canvasGroup = tooltipPanel.AddComponent<CanvasGroup>();
HideTooltip();
}
public void ShowTooltip(string text, Vector2 position)
{
if (showCoroutine != null)
StopCoroutine(showCoroutine);
showCoroutine = StartCoroutine(ShowTooltipCoroutine(text, position));
}
private IEnumerator ShowTooltipCoroutine(string text, Vector2 position)
{
yield return new WaitForSeconds(showDelay);
tooltipText.text = text;
tooltipPanel.SetActive(true);
// Position the tooltip
Vector2 screenPoint = position + offset;
tooltipRect.position = screenPoint;
// Fade in
float alpha = 0;
while (alpha < 1)
{
alpha += Time.deltaTime / fadeSpeed;
canvasGroup.alpha = alpha;
yield return null;
}
}
public void HideTooltip()
{
if (showCoroutine != null)
{
StopCoroutine(showCoroutine);
showCoroutine = null;
}
StartCoroutine(HideTooltipCoroutine());
}
private IEnumerator HideTooltipCoroutine()
{
float alpha = canvasGroup.alpha;
while (alpha > 0)
{
alpha -= Time.deltaTime / fadeSpeed;
canvasGroup.alpha = alpha;
yield return null;
}
tooltipPanel.SetActive(false);
}
public void UpdatePosition(Vector2 position)
{
if (tooltipPanel.activeSelf)
{
tooltipRect.position = position + offset;
}
}
}