VanguardReMaster/client/Assets/Vanguard/Runtime/UI/LoginBackdrop.cs

74 lines
2.7 KiB
C#

using UnityEngine;
using UnityEngine.UI;
namespace Vanguard.UI
{
/// <summary>
/// Фон экрана входа: key art на весь экран (cover) и затемнение по краям.
/// Арт лежит в Resources, потому что Addressables в клиенте ещё не подключены.
/// </summary>
public static class LoginBackdrop
{
private const string ResourcePath = "Login/login_bg";
public static RectTransform Create(Transform parent)
{
var rootGo = new GameObject("LoginBackdrop", typeof(RectTransform));
var root = (RectTransform)rootGo.transform;
root.SetParent(parent, false);
UiKit.Stretch(root);
root.SetAsFirstSibling();
var texture = Resources.Load<Texture2D>(ResourcePath);
if (texture != null)
{
var artGo = new GameObject(
"Art",
typeof(RectTransform),
typeof(Image),
typeof(AspectRatioFitter));
var art = (RectTransform)artGo.transform;
art.SetParent(root, false);
UiKit.Stretch(art);
var image = artGo.GetComponent<Image>();
image.sprite = Sprite.Create(
texture,
new Rect(0f, 0f, texture.width, texture.height),
new Vector2(0.5f, 0.5f),
100f);
image.type = Image.Type.Simple;
image.preserveAspect = false;
image.raycastTarget = false;
var fitter = artGo.GetComponent<AspectRatioFitter>();
fitter.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent;
fitter.aspectRatio = texture.width / (float)texture.height;
}
else
{
// Арта нет — тёмная заливка, экран не должен падать.
var fallback = UiKit.CreatePanel(
root, "Fallback", new Color(0.02f, 0.05f, 0.13f, 1f));
fallback.GetComponent<Image>().raycastTarget = false;
}
var shade = NeonUi.AddImage(
root,
"Shade",
NeonUi.Vignette(
480,
270,
new Color(0f, 0f, 0f, 0.08f),
new Color(0.004f, 0.016f, 0.055f, 0.72f)),
new Vector2(480f, 270f),
Vector2.zero);
shade.preserveAspect = false;
UiKit.Stretch(shade.rectTransform);
return root;
}
}
}