/// <summary>Creates the business panels.</summary> private void CreateBusinessPanels() { //create business panels panels = new MonoBehaviour[GameData.instance.numberOfBusinesses]; for (int i = 0; i < panels.Length; i++) { Business business = PlayerManager.instance.GetBusiness(i); if (business.isUnlocked) { BusinessPanel panel = Instantiate(businessPanelPrefab, scrollViewContent); panel.Initialize(business); panels[i] = panel; } else { BuyBusinessPanel panel = Instantiate(buyBusinessPanelPrefab, scrollViewContent); panel.Initialize(business.name, business.costToUnlock); panels[i] = panel; //set up delegate int temp = i; //a temporary value for the closure panel.OnBuyBusinessButtonPressed += (() => { OnBuyBusinessButtonPressed(temp); }); } } }
/// <summary>Callback when a BuyBusinessButton is pressed.</summary> /// <param name="businessIndex">The business (as an index).</param> private void OnBuyBusinessButtonPressed(int businessIndex) { Business business = PlayerManager.instance.GetBusiness(businessIndex); if (PlayerManager.instance.cash >= business.costToUnlock) { //instantiate a new business panel and add it at position businessIndex in the scroll view hierarchy BusinessPanel panel = Instantiate(businessPanelPrefab, scrollViewContent); panel.transform.SetSiblingIndex(businessIndex); panel.Initialize(business); //delete the BuyBusinessPanel and update the array Destroy(panels[businessIndex].gameObject); //on destroy the delegate will be removed panels[businessIndex] = panel; //update player's cash PlayerManager.instance.DecrementCashBy(business.costToUnlock); business.SetUnlocked(); UpdateUI(); } }