Files
2026-05-21 16:04:49 +02:00

75 lines
2.2 KiB
C#

using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
namespace DanieleMarotta.RiversongCodeShowcase
{
public class BuildingStorageVisualization : MonoBehaviour
{
[PropertySpace(SpaceAfter = 20)]
[SerializeField]
private List<Transform> _slots;
[VerticalGroup("Arrange Slots/Controls")]
[LabelText("Min")]
[SerializeField]
private Vector2 _boundsMin;
[VerticalGroup("Arrange Slots/Controls")]
[LabelText("Max")]
[SerializeField]
private Vector2 _boundsMax;
[VerticalGroup("Arrange Slots/Controls")]
[SerializeField]
private int _columns = 4;
public int SlotCount => _slots.Count;
public Transform GetSlot(int slot)
{
return _slots[slot];
}
public void SetProductVisualization(int slotIndex, GameObject productVisualization)
{
productVisualization.transform.SetParent(GetSlot(slotIndex));
productVisualization.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
}
public bool TryGetProductVisualization(int slotIndex, out GameObject productVisualization)
{
var slot = GetSlot(slotIndex);
productVisualization = slot.childCount > 0 ? slot.GetChild(0).gameObject : null;
return productVisualization;
}
[HorizontalGroup("Arrange Slots", 200)]
[Button(ButtonSizes.Large)]
[GUIColor("cyan")]
private void ArrangeSlots()
{
var rows = Mathf.CeilToInt((float)_slots.Count / _columns);
var w = _boundsMax.x - _boundsMin.x;
var h = _boundsMax.y - _boundsMin.y;
var spacing = new Vector2(w, h) / new Vector2(_columns, rows);
for (var i = 0; i < _slots.Count; i++)
{
var slot = _slots[i];
var row = i / _columns;
var column = i % _columns;
var p = slot.position;
p.x = _boundsMin.x + (0.5f + column) * spacing.x;
p.z = _boundsMin.y + (0.5f + row) * spacing.y;
slot.position = p;
}
}
}
}