protected override void Awake() { base.Awake(); transform.GetChild(0).GetComponent <Button>().onClick.AddListener(OnClick); SpiritCountText = transform.GetChild(0).GetChild(0).GetComponent <TextMeshProUGUI>(); void OnClick() { Debug.Log(1); var goldCost = SpiritData.Get(Numeral.ResourceCost).Value; var spiritLimit = SpiritData.Get(Enums.Spirit.SpiritLimit).Value; var magicCrystalsCost = SpiritData.Get(Enums.Spirit.MagicCrystalReq).Value; Debug.Log(Owner.ResourceSystem); if (Owner.ResourceSystem.CheckHaveResources(spiritLimit, goldCost, magicCrystalsCost)) { Debug.Log(3); PlaceNewSpiritClicked?.Invoke(SpiritData); Count--; if (Count >= 1) { Debug.Log(4); SpiritCountText.text = Count.ToString(); Debug.Log(5); } else { AllThisSpiritsPlaced?.Invoke(this); Destroy(gameObject); } } } }
public void AddSpiritButton(SpiritData spiritData) { var spiritCount = 0; var isSameSpirit = false; spiritButtons.ForEach(button => { if (button.SpiritData.Index == spiritData.Index) { isSameSpirit = true; button.Count++; button.transform.GetChild(0).GetChild(0).GetComponent <TextMeshProUGUI>().text = button.Count.ToString(); spiritCount++; } }); if (!isSameSpirit) { spiritButtons.Add(GetSpiritButton()); } UpdateUI(); SpiritButtonSystem GetSpiritButton() { var newSpiritButton = Instantiate(SpiritButtonPrefab, RarityGOs[(int)spiritData.Base.Rarity].transform).GetComponent <SpiritButtonSystem>(); newSpiritButton.Set(spiritData, Owner, newSpiritButtonPos, spiritCount); newSpiritButton.PlaceNewSpiritClicked += OnPlaceNewSpiritClicked; newSpiritButton.AllThisSpiritsPlaced += OnAllThisSpiritsUsed; return(newSpiritButton); void OnPlaceNewSpiritClicked(SpiritData obj) => PlaceNewSpiritClicked?.Invoke(obj); void OnAllThisSpiritsUsed(SpiritButtonSystem spiritButton) { spiritButtons.Remove(spiritButton); var buttonRects = new List <RectTransform>(); spiritButtons.ForEach(button => { if (button.Element == spiritButton.Element) { if (button.Rarity == spiritButton.Rarity) { buttonRects.Add(button.GetComponent <RectTransform>()); } } }); for (int i = 0; i < buttonRects.Count; i++) { var isNewButtonPosBusy = false; var newButtonPos = (Vector2)buttonRects[i].localPosition - newSpiritButtonPos; for (int j = 0; j < buttonRects.Count; j++) { if (newButtonPos.y == buttonRects[j].localPosition.y) { isNewButtonPosBusy = true; break; } } if (isNewButtonPosBusy) { break; } else if (newButtonPos.y >= 0) { buttonRects[i].localPosition = newButtonPos; } } } } }