PO/Assets/Inventory/SpawnButton.cs

86 lines
2.4 KiB
C#
Raw Normal View History

2022-01-16 23:20:47 +03:00
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;
2022-01-17 23:09:07 +03:00
public int maxHeroButOnPage;
2022-01-16 23:20:47 +03:00
[SerializeField] private int page;
2022-01-19 23:48:18 +03:00
2022-01-16 23:20:47 +03:00
private void Start()
{
page = 0;
maxHeroButOnPage = maxHeroesInRow * maxHeroesInColumn;
InstantiateEquipmentButton(0, maxHeroButOnPage);
}
public void ChangePage(int _page)
{
2022-01-17 23:09:07 +03:00
if ((page + _page) * maxHeroButOnPage - Inventory.main.localplayerEquipment.Count < 0)
2022-01-16 23:20:47 +03:00
{
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)
{
2022-01-19 23:48:18 +03:00
for (int i = 0; i < allButtonLinks.Count; i++)
2022-01-17 23:09:07 +03:00
{
2022-01-19 23:48:18 +03:00
Destroy(allButtonLinks[i]);
2022-01-17 23:09:07 +03:00
}
2022-01-19 23:48:18 +03:00
allButtonLinks.Clear();
2022-01-17 23:09:07 +03:00
2022-01-16 23:20:47 +03:00
float _x = 0;
float _y = 0;
int _counter = 0;
for (int i = _begin; i < _end; i++)
{
2022-01-17 23:09:07 +03:00
if (i > Inventory.main.localplayerEquipment.Count - 1)
2022-01-16 23:20:47 +03:00
{
break;
}
Button link = Instantiate(buttonPrefab, parent.transform);
2022-01-19 23:48:18 +03:00
2022-01-16 23:20:47 +03:00
link.transform.GetComponent<EquipmentButton>().num = i;
link.transform.localPosition = new Vector3(_x, _y, 0);
2022-01-19 23:48:18 +03:00
link.image.sprite = Inventory.main.localplayerEquipment[i].GetComponent<Equipment>().equipmentConfig.equipmentCharacteristics.sprite;
2022-01-16 23:20:47 +03:00
2022-01-19 23:48:18 +03:00
_x += gameObject.GetComponent<RectTransform>().rect.width + distX;
2022-01-16 23:20:47 +03:00
_counter += 1;
if (_counter % maxHeroesInRow == 0)
{
_x = 0;
2022-01-19 23:48:18 +03:00
_y -= gameObject.GetComponent<RectTransform>().rect.height + distY;
2022-01-16 23:20:47 +03:00
}
allButtonLinks.Add(link);
}
}
}