public IEnumerator TwoItemsCanBeAddedToShop() { //Create new object to test ShopManager shop_manager = MonoBehaviour.Instantiate(Resources.Load <GameObject>("Prefabs/Shop")).GetComponent <ShopManager>(); //Create test items ShopManager.ShopItem shop_item = new ShopManager.ShopItem(); shop_item.ID = 1; ShopManager.ShopItem shop_item_two = new ShopManager.ShopItem(); shop_item_two.ID = 2; //Add test items to list List <ShopManager.ShopItem> shop_items = new List <ShopManager.ShopItem>(); shop_items.Add(shop_item); shop_items.Add(shop_item_two); //Run fuction with test parameter shop_manager.AddItemsToShop(shop_items); yield return(new WaitForSeconds(0.1f)); //Test result Assert.AreEqual(shop_manager.purchasable_items.Count, 2); //Get rid of the garbage Object.Destroy(shop_manager.gameObject); }
public IEnumerator MissingItemFromPurchaseByItemReturnsFalse() { //Create new object to test ShopManager shop_manager = MonoBehaviour.Instantiate(Resources.Load <GameObject>("Prefabs/Shop")).GetComponent <ShopManager>(); ShopManager.ShopItem shop_item = new ShopManager.ShopItem(); shop_item.ID = 1; yield return(new WaitForSeconds(0.1f)); Assert.AreEqual(shop_manager.PurchaseItem(shop_item), false); //Get rid of the garbage Object.Destroy(shop_manager.gameObject); }
// Update is called once per frame void Update() { //Debug Press `K` to add test item to shop if (Application.isEditor) { if (Input.GetKeyDown(KeyCode.K)) { ShopManager.ShopItem item = new ShopManager.ShopItem(); item.ID = 5; item.name = "Test Item"; item.type = "DevType"; item.cost = 5; ShopManager.main.AddItemToShop(item); Debug.Log("Added " + item.name + " to shop"); } } }
public IEnumerator ItemCanBePurchasedViaSlotId() { //Create new object to test ShopManager shop_manager = MonoBehaviour.Instantiate(Resources.Load <GameObject>("Prefabs/Shop")).GetComponent <ShopManager>(); ShopManager.ShopItem shop_item = new ShopManager.ShopItem(); shop_item.ID = 1; shop_manager.AddItemToShop(shop_item); yield return(new WaitForSeconds(0.1f)); Assert.AreEqual(shop_manager.PurchaseItem(0), true); //Get rid of the garbage Object.Destroy(shop_manager.gameObject); }
//Dynamically update GUI object values based upon ShopItem struct void UpdateSlotGameObject(ShopManager.ShopItem item, GameObject obj) { //Check each child object for (int i = 0; i < obj.transform.childCount; i++) { //Test if name corresponds to a field of the struct var field = item.GetType().GetField(obj.transform.GetChild(i).gameObject.name); if (field != null) { //Checks if the value should have a text component if (field.FieldType == typeof(int) || field.FieldType == typeof(float) || field.FieldType == typeof(string)) { string text_to_display = ""; if (field.FieldType == typeof(string)) { text_to_display = (string)field.GetValue(item); } else { //Convert floats and ints to strings text_to_display = field.GetValue(item).ToString(); } //Update text component if it exists Text textObj = obj.transform.GetChild(i).GetComponent <Text>(); if (textObj != null) { textObj.text = text_to_display; } } //Checks if the value should have a SpriteRenderer component else if (field.FieldType == typeof(Sprite)) { //Update SpriteRenderer component Image renderer = obj.transform.GetChild(i).GetComponent <Image>(); if (renderer != null) { renderer.sprite = (Sprite)field.GetValue(item); } } } } }
void CreateSlots() { for (int i = 0; i < ShopManager.main.purchasable_items.Count; i++) { ShopManager.ShopItem item = ShopManager.main.purchasable_items[i]; //Get object shop's items //Create GUI object GameObject obj = Instantiate(slotPrefab) as GameObject; //Update prefab to match item obj.name = "Slot: " + i.ToString(); UpdateSlotGameObject(item, obj); int slotId = i; obj.GetComponent <Button>().onClick.AddListener(() => ShopManager.main.PurchaseItem(slotId)); //Move GameObject into scene obj.transform.SetParent(contentContainer); } }