61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
// Спрайты-заглушки (квадрат и круг размером 1x1 клетку), генерируются в коде.
|
|
// Когда появится арт -- SquadView просто получит настоящие спрайты.
|
|
public static class PrimitiveSprites
|
|
{
|
|
private static Sprite square;
|
|
private static Sprite circle;
|
|
|
|
public static Sprite Square
|
|
{
|
|
get
|
|
{
|
|
if (square == null) square = Create(false);
|
|
return square;
|
|
}
|
|
}
|
|
|
|
public static Sprite Circle
|
|
{
|
|
get
|
|
{
|
|
if (circle == null) circle = Create(true);
|
|
return circle;
|
|
}
|
|
}
|
|
|
|
static Sprite Create(bool round)
|
|
{
|
|
const int size = 64;
|
|
var tex = new Texture2D(size, size, TextureFormat.RGBA32, false)
|
|
{
|
|
filterMode = FilterMode.Bilinear,
|
|
wrapMode = TextureWrapMode.Clamp
|
|
};
|
|
|
|
var pixels = new Color32[size * size];
|
|
float center = (size - 1) / 2f;
|
|
for (int y = 0; y < size; y++)
|
|
{
|
|
for (int x = 0; x < size; x++)
|
|
{
|
|
byte alpha = 255;
|
|
if (round)
|
|
{
|
|
float dx = x - center;
|
|
float dy = y - center;
|
|
float distance = Mathf.Sqrt(dx * dx + dy * dy);
|
|
// мягкий край в 1 пиксель, чтобы круг не был "лесенкой"
|
|
alpha = (byte)(Mathf.Clamp01(size / 2f - distance) * 255);
|
|
}
|
|
pixels[y * size + x] = new Color32(255, 255, 255, alpha);
|
|
}
|
|
}
|
|
tex.SetPixels32(pixels);
|
|
tex.Apply();
|
|
|
|
return Sprite.Create(tex, new Rect(0, 0, size, size), new Vector2(0.5f, 0.5f), size);
|
|
}
|
|
}
|