PO/Assets/Inventory/SpawnButton.cs

97 lines
2.9 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;
public int maxHeroButOnPage;
[SerializeField] private int page;
private void Start()
{
page = 0;
maxHeroButOnPage = maxHeroesInRow * maxHeroesInColumn;
InstantiateEquipmentButton(0, maxHeroButOnPage);
}
public void OnClick()
{
InstantiateEquipmentButton(0, 9);
}
public void ChangePage(int _page)
{
if ((page + _page) * maxHeroButOnPage - Inventory.main.localplayerEquipment.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 )
{
allButtonLinks.Clear();
for (int i = 0; i < parent.transform.childCount; i++)
{
Destroy(parent.transform.GetChild(i).gameObject);
}
float _x = 0;
float _y = 0;
int _counter = 0;
for (int i = _begin; i < _end; i++)
{
if (i > Inventory.main.localplayerEquipment.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.localplayerEquipment[link.transform.GetComponent<EquipmentButton>().num].GetComponent<Equipment>().equipmentConfig.equipmentCharacteristics.sprite;
for (int j = 0; j < Inventory.main.allEquipment.Count; j++)
{
if (Inventory.main.allEquipment[j].GetComponent<Equipment>().equipmentConfig.equipmentCharacteristics.sprite == link.image.sprite)
{
link.transform.GetComponent<EquipmentButton>().num = j;
break;
}
}
_x += link.GetComponent<RectTransform>().rect.width + distX;
_counter += 1;
if (_counter % maxHeroesInRow == 0)
{
_x = 0;
_y -= link.GetComponent<RectTransform>().rect.height + distY;
}
allButtonLinks.Add(link);
}
}
}