PO/Assets/Inventory/SpawnButton.cs

97 lines
2.9 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 OnClick()
{
InstantiateEquipmentButton(0, 9);
}
2022-01-16 23:20:47 +03:00
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-16 23:20:47 +03:00
{
allButtonLinks.Clear();
for (int i = 0; i < parent.transform.childCount; i++)
2022-01-17 23:09:07 +03:00
{
Destroy(parent.transform.GetChild(i).gameObject);
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);
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;
}
}
2022-01-16 23:20:47 +03:00
_x += link.GetComponent<RectTransform>().rect.width + distX;
2022-01-16 23:20:47 +03:00
_counter += 1;
if (_counter % maxHeroesInRow == 0)
{
_x = 0;
_y -= link.GetComponent<RectTransform>().rect.height + distY;
2022-01-16 23:20:47 +03:00
}
allButtonLinks.Add(link);
}
}
}