public void AddCraft(int id) { Craft craftToAdd = database.FetchCraftByID(id); if (craftToAdd == null) // if the id does not exist. { Debug.Log("craftIdNotFound"); return; } // Instantiate the recipeItem int category = craftToAdd.category; GameObject recipeslt = Instantiate(recipeSlot); recipeslt.transform.SetParent(recipePanel[category].transform.Find("Panel"), false);// Add a recipeSlot in the approppriate Panel //recipeslt.transform.GetChild(3).gameObject.SetActive(true); // Lock the receipt Transform product = recipeslt.transform.Find("Slot").Find("Product Panel"); Transform component = recipeslt.transform.Find("Slot").Find("Component Panel"); // Instantiate the productItem int productID = craftToAdd.itemsID[0]; int productAmount = craftToAdd.itemsAmount[0]; Item productItem = idatabase.FetchItemByID(productID); ItemView productObj = ItemView.CreateStandalone(productItem, productAmount); productObj.transform.SetParent(product, false); productObj.transform.localPosition = Vector2.zero; // sets the position of the item according to the slot //productObj.transform.localPosition = Vector2.zero; // sets the position of the item according to the slot productObj.transform.localScale = Vector3.one; // Instantiate the componentItems int numberOfComp = craftToAdd.itemsID.Length - 1; int[] componentID = new int[numberOfComp]; int[] componentAmount = new int[numberOfComp]; Item[] componentItem = new Item[numberOfComp]; int[] amount = new int[numberOfComp]; for (int i = 0; i < numberOfComp; i++) { componentID[i] = craftToAdd.itemsID[i + 1]; componentAmount[i] = craftToAdd.itemsAmount[i + 1]; componentItem[i] = idatabase.FetchItemByID(componentID[i]); amount[i] = craftToAdd.itemsAmount[i + 1]; } for (int i = 0; i < numberOfComp; i++) { //recipeslt.transform.GetChild(1); ItemView compObj = ItemView.CreateStandalone(componentItem[i], amount[i]); compObj.transform.SetParent(component, false); compObj.transform.localPosition = Vector2.zero; // sets the position of the item according to the slot compObj.transform.localScale = Vector3.one; } // Instantiate the create Button Function Button button = recipeslt.GetComponentInChildren <Button>(); button.onClick.AddListener(() => { createObject(productID, productAmount, componentID, componentAmount); }); product.GetComponent <HorizontalFitter>().Refit(); //component.GetComponent<HorizontalFitter>().Refit(); LayoutRebuilder.ForceRebuildLayoutImmediate(component.GetComponent <RectTransform>()); //float width = component.GetComponent<RectTransform>().rect.width; //float height = component.GetComponent<RectTransform>().rect.height - component.GetComponent<GridLayoutGroup>().padding.vertical; //float size = Mathf.Min(width, height) / Mathf.Sqrt(component.transform.childCount); //component.GetComponent<GridLayoutGroup>().cellSize = new Vector2(size - component.GetComponent<GridLayoutGroup>().spacing.x, size - component.GetComponent<GridLayoutGroup>().spacing.y); LayoutRebuilder.MarkLayoutForRebuild(product.GetComponent <RectTransform>()); LayoutRebuilder.MarkLayoutForRebuild(recipeslt.GetComponent <RectTransform>()); LayoutRebuilder.MarkLayoutForRebuild(GetComponent <RectTransform>()); }
public void AddCraft(int id) { Craft craftToAdd = database.FetchCraftByID(id); if (craftToAdd == null) // if the id does not exist. { Debug.Log("craftIdNotFound"); return; } // Instantiate the recipeItem int category = craftToAdd.category; GameObject recipeslt = Instantiate(recipeSlot, recipePanel[category].transform.Find("Panel")); // Add a recipeSlot in the approppriate Panel //recipeslt.transform.GetChild(3).gameObject.SetActive(true); // Lock the receipt // Instantiate the productItem int productID = craftToAdd.itemsID[0]; int productAmount = craftToAdd.itemsAmount[0]; Item productItem = idatabase.FetchItemByID(productID); GameObject productObj = Instantiate(craftItem, recipeslt.transform.GetChild(0).GetChild(0)); // creates the productItem gameObject in the productSlot of the product panel of the recipeSlot productObj.GetComponent <Image>().sprite = productItem.Sprite; // sets the sprite productObj.GetComponent <ItemData>().item = productItem; // sets the item productObj.transform.localPosition = Vector2.zero; // sets the position of the item according to the slot productObj.name = productItem.Title; // sets the name of the gameObject // Instantiate the componentItems int numberOfComp = craftToAdd.itemsID.Length - 1; int[] componentID = new int[numberOfComp]; int[] componentAmount = new int[numberOfComp]; Item[] componentItem = new Item[numberOfComp]; for (int i = 0; i < numberOfComp; i++) { componentID[i] = craftToAdd.itemsID[i + 1]; componentAmount[i] = craftToAdd.itemsAmount[i + 1]; componentItem[i] = idatabase.FetchItemByID(componentID[i]); } for (int i = 0; i < numberOfComp; i++) { GameObject compSlot = Instantiate(craftSlot, recipeslt.transform.GetChild(1)); // creates the ComponentSlot of the Component panel of the recipeSlot GameObject compObj = Instantiate(craftItem, compSlot.transform); // creates the craftItem gameObject in the ComponentSlot of the component panel of the recipeSlot compObj.GetComponent <ItemData>().item = componentItem[i]; // sets the compObj.GetComponent <Image>().sprite = componentItem[i].Sprite; // sets the sprite compObj.transform.localPosition = Vector2.zero; // sets the position of the item according to the slot compObj.name = componentItem[i].Title; // sets the name of the gameObject compSlot.GetComponentInChildren <Text>().text = componentAmount[i].ToString(); compSlot.GetComponentInChildren <Text>().transform.SetAsLastSibling(); } // Instantiate the create Button Function Button button = recipeslt.transform.GetChild(2).GetComponent <Button>(); button.onClick.AddListener(() => { createObject(productID, productAmount, componentID, componentAmount); }); }