Repository navigation
Expand file tree
/
Copy pathToastModule.cs
More file actions
94 lines (81 loc) · 2.96 KB
/
Copy pathToastModule.cs
File metadata and controls
94 lines (81 loc) · 2.96 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
using System.Collections.Generic;
using UnityEngine;
namespace HTFToolkit
{
/// <summary>
/// 开关提示:屏幕上方居中的淡出通知("准星: 开启")。
/// </summary>
internal static class ToastModule
{
private struct Toast
{
public string Text;
public Color Color;
public double Expire;
}
private static readonly List<Toast> _toasts = new List<Toast>();
private static GUIStyle _style;
private static GUIStyle _shadowStyle;
private const double Lifetime = 1.6;
private const double FadeTime = 0.4;
public static void Show(string moduleName, bool on)
{
Show(moduleName + ": " + (on ? "<color=#5dff5d>开启</color>" : "<color=#ff5d5d>关闭</color>"),
on ? new Color(1f, 1f, 1f) : new Color(1f, 0.85f, 0.85f));
}
public static void Show(string text, Color color)
{
_toasts.Add(new Toast
{
Text = text,
Color = color,
Expire = Time.realtimeSinceStartupAsDouble + Lifetime
});
if (_toasts.Count > 5)
_toasts.RemoveAt(0);
}
public static void Draw()
{
if (_toasts.Count == 0 || Event.current == null)
return;
if (_style == null)
{
_style = new GUIStyle(GUI.skin.label)
{
fontStyle = FontStyle.Bold,
alignment = TextAnchor.MiddleCenter,
richText = true
};
_shadowStyle = new GUIStyle(_style);
}
_style.fontSize = UIScale.TitleFont;
_shadowStyle.fontSize = UIScale.TitleFont;
double now = Time.realtimeSinceStartupAsDouble;
float y = 46f;
for (int i = _toasts.Count - 1; i >= 0; i--)
{
if (now > _toasts[i].Expire)
{
_toasts.RemoveAt(i);
continue;
}
}
for (int i = 0; i < _toasts.Count; i++)
{
Toast t = _toasts[i];
double remain = t.Expire - now;
float alpha = remain < FadeTime ? (float)(remain / FadeTime) : 1f;
Color c = t.Color;
c.a = alpha;
// 框高/行距按字号自适应(固定 26px 在大字号下会截断文字)
float lineH = _style.fontSize * 1.4f;
Rect r = new Rect(0f, y, Screen.width, lineH);
_shadowStyle.normal.textColor = new Color(0f, 0f, 0f, alpha);
_style.normal.textColor = c;
GUI.Label(new Rect(r.x + 1f, r.y + 1f, r.width, r.height), t.Text, _shadowStyle);
GUI.Label(r, t.Text, _style);
y += lineH + _style.fontSize * 0.25f;
}
}
}
}