Files
Fruitomation/Assets/Scripts/UI/GridViewController.cs
2026-03-31 11:52:26 +01:00

53 lines
1.4 KiB
C#

using Fruitomation.Global;
using UnityEngine.UI;
using UnityEngine;
namespace Fruitomation.UI
{
[RequireComponent(typeof(Image))]
public class GridViewController : MonoBehaviour
{
[SerializeField] private Texture2D SourceTexture;
[SerializeField] private int Cols;
[SerializeField] private int Rows;
private Image Image;
private void Awake()
{
Texture2D tiled = TileTexture(SourceTexture, Cols, Rows);
Sprite sprite = Sprite.Create
(
tiled,
new Rect(0, 0, tiled.width, tiled.height),
new Vector2(0.5f, 0.5f)
);
Image = GetComponent<Image>();
Image.color = Color.white;
Image.sprite = sprite;
}
private static Texture2D TileTexture(Texture2D source, int cols, int rows)
{
int srcW = source.width;
int srcH = source.height;
Texture2D result = new(srcW * cols, srcH * rows);
Color[] srcPixels = source.GetPixels();
for (int row = 0; row < rows; row++)
for (int col = 0; col < cols; col++)
{
result.SetPixels(col * srcW, row * srcH, srcW, srcH, srcPixels);
}
result.Apply();
return result;
}
private void Update() =>
Image.enabled = GameStateController.Is(GameState.Building);
}
}