コード例 #1
0
    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>());
    }