206 lines
8.4 KiB
C#
206 lines
8.4 KiB
C#
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Общий стиль резервного интерфейса OnGUI (асфальтово-индустриальная палитра #16181D).
|
||
|
|
/// Все окна рисуются в виртуальном разрешении высотой 1080 px и масштабируются под любой экран,
|
||
|
|
/// поэтому интерфейс гарантированно отображается даже при пустых ссылках в Инспекторе.
|
||
|
|
/// </summary>
|
||
|
|
public static class GameUI
|
||
|
|
{
|
||
|
|
public const float RefHeight = 1080f;
|
||
|
|
|
||
|
|
public static readonly Color Bg = Hex("16181D");
|
||
|
|
public static readonly Color PanelColor = Hex("1E2128");
|
||
|
|
public static readonly Color PanelLight = Hex("2A2E37");
|
||
|
|
public static readonly Color Accent = Hex("F2A93B");
|
||
|
|
public static readonly Color Green = Hex("4CD07D");
|
||
|
|
public static readonly Color Red = Hex("E5534B");
|
||
|
|
public static readonly Color Blue = Hex("4FA3F7");
|
||
|
|
public static readonly Color TextColor = Hex("E8E9EB");
|
||
|
|
public static readonly Color Muted = Hex("9AA0A6");
|
||
|
|
|
||
|
|
private static bool initialized;
|
||
|
|
private static readonly Dictionary<string, Texture2D> texCache = new Dictionary<string, Texture2D>();
|
||
|
|
|
||
|
|
public static GUIStyle Panel, PanelSolid, Title, Header, Label, LabelCenter, Small, SmallCenter, Button, ButtonAccent, ButtonDanger, ButtonTab, ButtonTabActive, Badge;
|
||
|
|
|
||
|
|
public static float Scale => Screen.height / RefHeight;
|
||
|
|
public static float VirtualWidth => Screen.width / Mathf.Max(Scale, 0.01f);
|
||
|
|
public static float VirtualHeight => RefHeight;
|
||
|
|
|
||
|
|
public static Color Hex(string hex)
|
||
|
|
{
|
||
|
|
ColorUtility.TryParseHtmlString("#" + hex, out Color c);
|
||
|
|
return c;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Вызывать в начале каждого OnGUI: включает масштабирование и стили.</summary>
|
||
|
|
public static void Begin(int depth = 0)
|
||
|
|
{
|
||
|
|
EnsureStyles();
|
||
|
|
GUI.depth = depth;
|
||
|
|
float s = Scale;
|
||
|
|
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(s, s, 1f));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Vector2 MouseVirtual()
|
||
|
|
{
|
||
|
|
Vector2 m = Event.current != null ? Event.current.mousePosition : Vector2.zero;
|
||
|
|
return m;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void EnsureStyles()
|
||
|
|
{
|
||
|
|
if (initialized && Panel != null && Panel.normal.background != null) return;
|
||
|
|
initialized = true;
|
||
|
|
|
||
|
|
Panel = new GUIStyle(GUI.skin.box)
|
||
|
|
{
|
||
|
|
normal = { background = Rounded(PanelColor.WithAlpha(0.96f), Hex("3A3F4A"), 12, 2) },
|
||
|
|
border = new RectOffset(14, 14, 14, 14),
|
||
|
|
padding = new RectOffset(20, 20, 18, 18)
|
||
|
|
};
|
||
|
|
|
||
|
|
PanelSolid = new GUIStyle(Panel)
|
||
|
|
{
|
||
|
|
normal = { background = Rounded(Bg, Hex("2F333C"), 12, 2) }
|
||
|
|
};
|
||
|
|
|
||
|
|
Badge = new GUIStyle(GUI.skin.box)
|
||
|
|
{
|
||
|
|
normal = { background = Rounded(Hex("101216").WithAlpha(0.82f), Hex("2F333C"), 10, 1), textColor = TextColor },
|
||
|
|
border = new RectOffset(12, 12, 12, 12),
|
||
|
|
padding = new RectOffset(12, 12, 6, 6),
|
||
|
|
fontSize = 18,
|
||
|
|
alignment = TextAnchor.MiddleLeft,
|
||
|
|
richText = true
|
||
|
|
};
|
||
|
|
|
||
|
|
Title = new GUIStyle(GUI.skin.label)
|
||
|
|
{
|
||
|
|
fontSize = 42, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleCenter,
|
||
|
|
normal = { textColor = TextColor }, richText = true, wordWrap = true
|
||
|
|
};
|
||
|
|
|
||
|
|
Header = new GUIStyle(GUI.skin.label)
|
||
|
|
{
|
||
|
|
fontSize = 26, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleLeft,
|
||
|
|
normal = { textColor = Accent }, richText = true, wordWrap = true
|
||
|
|
};
|
||
|
|
|
||
|
|
Label = new GUIStyle(GUI.skin.label)
|
||
|
|
{
|
||
|
|
fontSize = 19, alignment = TextAnchor.UpperLeft, wordWrap = true, richText = true,
|
||
|
|
normal = { textColor = TextColor }
|
||
|
|
};
|
||
|
|
|
||
|
|
LabelCenter = new GUIStyle(Label) { alignment = TextAnchor.MiddleCenter };
|
||
|
|
|
||
|
|
Small = new GUIStyle(Label) { fontSize = 16, normal = { textColor = Muted } };
|
||
|
|
SmallCenter = new GUIStyle(Small) { alignment = TextAnchor.MiddleCenter };
|
||
|
|
|
||
|
|
Button = MakeButton(Hex("2E333D"), Hex("3B414D"), Hex("4A5160"), TextColor);
|
||
|
|
ButtonAccent = MakeButton(Hex("C9862A"), Accent, Hex("FFC266"), Hex("16181D"));
|
||
|
|
ButtonDanger = MakeButton(Hex("8E2F2A"), Red, Hex("FF7A70"), Color.white);
|
||
|
|
ButtonTab = MakeButton(Hex("23262E"), Hex("2E333D"), Hex("3B414D"), Muted);
|
||
|
|
ButtonTabActive = MakeButton(Hex("3B414D"), Hex("444B59"), Hex("4A5160"), Accent);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static GUIStyle MakeButton(Color normal, Color hover, Color active, Color text)
|
||
|
|
{
|
||
|
|
return new GUIStyle(GUI.skin.button)
|
||
|
|
{
|
||
|
|
fontSize = 19,
|
||
|
|
fontStyle = FontStyle.Bold,
|
||
|
|
alignment = TextAnchor.MiddleCenter,
|
||
|
|
wordWrap = true,
|
||
|
|
richText = true,
|
||
|
|
border = new RectOffset(10, 10, 10, 10),
|
||
|
|
padding = new RectOffset(12, 12, 6, 6),
|
||
|
|
normal = { background = Rounded(normal, null, 8, 0), textColor = text },
|
||
|
|
hover = { background = Rounded(hover, null, 8, 0), textColor = text },
|
||
|
|
active = { background = Rounded(active, null, 8, 0), textColor = text },
|
||
|
|
focused = { background = Rounded(normal, null, 8, 0), textColor = text }
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Color WithAlpha(this Color c, float a) { c.a = a; return c; }
|
||
|
|
|
||
|
|
public static GUIStyle WithCenter(this GUIStyle s) => new GUIStyle(s) { alignment = TextAnchor.MiddleCenter };
|
||
|
|
|
||
|
|
public static Texture2D Solid(Color c)
|
||
|
|
{
|
||
|
|
string key = "s" + ColorUtility.ToHtmlStringRGBA(c);
|
||
|
|
if (texCache.TryGetValue(key, out Texture2D t) && t != null) return t;
|
||
|
|
t = new Texture2D(1, 1, TextureFormat.RGBA32, false) { hideFlags = HideFlags.HideAndDontSave };
|
||
|
|
t.SetPixel(0, 0, c);
|
||
|
|
t.Apply();
|
||
|
|
texCache[key] = t;
|
||
|
|
return t;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Текстура скругленного прямоугольника для 9-slice фона окон и кнопок.</summary>
|
||
|
|
public static Texture2D Rounded(Color fill, Color? border, int radius, int borderWidth)
|
||
|
|
{
|
||
|
|
string key = $"r{ColorUtility.ToHtmlStringRGBA(fill)}_{(border.HasValue ? ColorUtility.ToHtmlStringRGBA(border.Value) : "n")}_{radius}_{borderWidth}";
|
||
|
|
if (texCache.TryGetValue(key, out Texture2D cached) && cached != null) return cached;
|
||
|
|
|
||
|
|
int size = radius * 2 + 4;
|
||
|
|
Texture2D tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
||
|
|
{
|
||
|
|
hideFlags = HideFlags.HideAndDontSave,
|
||
|
|
wrapMode = TextureWrapMode.Clamp,
|
||
|
|
filterMode = FilterMode.Bilinear
|
||
|
|
};
|
||
|
|
|
||
|
|
for (int y = 0; y < size; y++)
|
||
|
|
{
|
||
|
|
for (int x = 0; x < size; x++)
|
||
|
|
{
|
||
|
|
float cx = Mathf.Clamp(x + 0.5f, radius, size - radius);
|
||
|
|
float cy = Mathf.Clamp(y + 0.5f, radius, size - radius);
|
||
|
|
float d = Vector2.Distance(new Vector2(x + 0.5f, y + 0.5f), new Vector2(cx, cy));
|
||
|
|
float alpha = Mathf.Clamp01(radius - d + 0.5f);
|
||
|
|
Color c = fill;
|
||
|
|
if (border.HasValue && borderWidth > 0 && d > radius - borderWidth - 0.5f)
|
||
|
|
c = border.Value;
|
||
|
|
c.a *= alpha;
|
||
|
|
tex.SetPixel(x, y, c);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
tex.Apply();
|
||
|
|
texCache[key] = tex;
|
||
|
|
return tex;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void DrawRect(Rect r, Color c)
|
||
|
|
{
|
||
|
|
GUI.DrawTexture(r, Solid(c));
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void ProgressBar(Rect r, float value01, Color fill, string caption = null)
|
||
|
|
{
|
||
|
|
DrawRect(r, Hex("0E1013"));
|
||
|
|
Rect f = new Rect(r.x + 2, r.y + 2, Mathf.Max(0f, (r.width - 4) * Mathf.Clamp01(value01)), r.height - 4);
|
||
|
|
DrawRect(f, fill);
|
||
|
|
if (!string.IsNullOrEmpty(caption))
|
||
|
|
GUI.Label(r, caption, SmallCenter);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Кнопка со звуком нажатия.</summary>
|
||
|
|
public static bool Btn(Rect r, string text, GUIStyle style = null)
|
||
|
|
{
|
||
|
|
bool pressed = GUI.Button(r, text, style ?? Button);
|
||
|
|
if (pressed) ProceduralAudioManager.Instance?.PlayUi();
|
||
|
|
return pressed;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string Money(float usd)
|
||
|
|
{
|
||
|
|
GameEconomyManager eco = GameEconomyManager.Instance;
|
||
|
|
return eco != null ? eco.FormatMoney(usd) : $"${usd:N0}";
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string Check(bool ok) => ok ? "<color=#4CD07D>●</color>" : "<color=#E5534B>●</color>";
|
||
|
|
}
|