PO/Assets/Scripts/Inventory/SpawnButton.cs

80 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnButton : MonoBehaviour
{
[SerializeField] private Button buttonPrefab;
[SerializeField] private GameObject parent;
[SerializeField] private List<Button> allButtonLinks = new List<Button>();
[SerializeField] private float distX;
[SerializeField] private float distY;
[SerializeField] private int maxHeroesInRow = 3;
[SerializeField] private int maxHeroesInColumn = 3;
private int maxHeroButOnPage;
[SerializeField] private int page;
private void Start()
{
page = 0;
maxHeroButOnPage = maxHeroesInRow * maxHeroesInColumn;
InstantiateEquipmentButton(0, maxHeroButOnPage);
}
public void ChangePage(int _page)
{
if ((page + _page) * maxHeroButOnPage - Inventory.main.allEquipment.Count < 0)
{
if (page + _page > -1)
{
page += _page;
}
foreach (Transform child in transform)
{
GameObject.Destroy(child.gameObject);
}
InstantiateEquipmentButton(page * (maxHeroButOnPage), (page + 1) * (maxHeroButOnPage));
}
}
public void InstantiateEquipmentButton(int _begin, int _end)
{
float _x = 0;
float _y = 0;
int _counter = 0;
for (int i = _begin; i < _end; i++)
{
if (i > Inventory.main.allEquipment.Count - 1)
{
break;
}
Button link = Instantiate(buttonPrefab, parent.transform);
link.transform.GetComponent<EquipmentButton>().num = i;
link.transform.localPosition = new Vector3(_x, _y, 0);
link.image.sprite = Inventory.main.allEquipment[i].GetComponent<Equipment>().equipmentConfig.equipmentCharacteristics.sprite;
_x += gameObject.GetComponent<RectTransform>().rect.width + distX;
_counter += 1;
if (_counter % maxHeroesInRow == 0)
{
_x = 0;
_y -= gameObject.GetComponent<RectTransform>().rect.height + distY;
}
allButtonLinks.Add(link);
}
}
}