Beispiel #1
0
    public void updateCraft()
    {
        if (result.childCount > 0)
        {
            Destroy(result.GetChild(0).gameObject);
        }
        List <int[]> list     = new List <int[]>();
        int          countRec = reciples.Length;

        for (int i = 0; i < countRec; i++)
        {
            int[] arr = new int[9] {
                (int)reciples[i].materials.A.x,
                (int)reciples[i].materials.A.y,
                (int)reciples[i].materials.A.z,
                (int)reciples[i].materials.B.x,
                (int)reciples[i].materials.B.y,
                (int)reciples[i].materials.B.z,
                (int)reciples[i].materials.C.x,
                (int)reciples[i].materials.C.y,
                (int)reciples[i].materials.C.z,
            };
            list.Add(arr);
        }

        for (int i = 0; i < items.childCount; i++)
        {
            int id = 0;
            if (items.GetChild(i).childCount > 0)
            {
                id = items.GetChild(i).GetChild(0).GetComponent <Drag>().item.id;
            }
            for (int j = 0; j < countRec; j++)
            {
                if (list[j] == null)
                {
                    break;
                }
                if (list[j][i] != id)
                {
                    list[j] = null;
                }
            }
        }

        for (int j = 0; j < countRec; j++)
        {
            if (list[j] != null)
            {
                Reciple    current = reciples[j];
                Item       item    = current.item.GetComponent <Item>();
                GameObject img     = Instantiate(container);
                img.transform.SetParent(result);
                img.GetComponent <Image>().sprite = Resources.Load <Sprite>(item.sprite);
                img.GetComponent <Drag>().item    = item;
                break;
            }
        }
    }
Beispiel #2
0
    public void updateCraft()
    {
        // если в результе что-то есть - удаляем
        if (result.childCount > 0)
        {
            Destroy(result.GetChild(0).gameObject);
        }

        List <int[]> list     = new List <int[]>(); // лист массивов с рецептами
        int          countRec = reciples.Length;    // кол-во рецептов

        // заполняем лист
        for (int i = 0; i < countRec; i++)
        {
            int[] arr = new int[9] {
                (int)reciples[i].materials.A.x,
                (int)reciples[i].materials.A.y,
                (int)reciples[i].materials.A.z,
                (int)reciples[i].materials.B.x,
                (int)reciples[i].materials.B.y,
                (int)reciples[i].materials.B.z,
                (int)reciples[i].materials.C.x,
                (int)reciples[i].materials.C.y,
                (int)reciples[i].materials.C.z
            };
            list.Add(arr);
        }


        // перебираем детей панельки крафта и смотрим совпадают ли они с индексами id из листа массива рецептов
        for (int i = 0; i < items.childCount; i++)
        {
            // смотрим есть ли ребенок . если нет - ячейка пуста
            int id = 0;
            if (items.GetChild(i).childCount > 0)
            {
                id = items.GetChild(i).GetChild(0).GetComponent <Drag>().item.id;
            }

            // перебираем лист на совпадение ID с id ребенка
            for (int j = 0; j < countRec; j++)
            {
                if (list[j] == null)
                {
                    break;
                }
                if (list[j][i] != id)
                {
                    list[j] = null;                   // зануляем, а не выкидываем из массива чтобы в конце j-индекс совпадал с индексом reciples
                }
            }
        }

        // перебираем ещё раз и смотрим остался ли хоть 1 целый List
        for (int j = 0; j < countRec; j++)
        {
            if (list[j] != null)
            {
                Reciple current = reciples[j];


                // вставляем в ячейку резулта крафта

                Item       item = current.item.GetComponent <Item>();                     // получаем наш Item
                GameObject img  = Instantiate(inventory_cell_container);                  // создаём объект (из префаба)
                img.transform.SetParent(result);                                          // делаем дочерним нашей ячейки
                img.GetComponent <Image>().sprite = Resources.Load <Sprite>(item.sprite); //добавляем иконку.
                // выкидываем предмет
                // img.AddComponent<Button>().onClick.AddListener(() => remove_item(current_item, img));

                img.GetComponent <Drag>().item = item;


                break;
            }
        }
    }