I have inventory and there is a frame. Moving it in a simple array.

if (Input.GetKeyDown(KeyCode.A)) { if(indexSlotForFrame - 1 >= 0) { indexSlotForFrame -= 1; leftRight--; } } if (Input.GetKeyDown(KeyCode.D)) { if(indexSlotForFrame + 1 <= slots.Count - 1) { indexSlotForFrame += 1; leftRight++; } } if (Input.GetKeyDown(KeyCode.W)) { if(indexSlotForFrame - frameAcross >= 0) { indexSlotForFrame -= frameAcross; } } if (Input.GetKeyDown(KeyCode.S)) { if(indexSlotForFrame + frameAcross <= slots.Count - 1) { indexSlotForFrame += frameAcross; } } 

and I just memorize the number and already transfer the slot

 frame.transform.parent = slots[indexSlotForFrame].transform; frame.transform.position = slots[indexSlotForFrame].transform.position; 

But this system is not convenient, as can be seen from the code, because if I want to open a chest, where there is another array, I cannot connect them. Someone can throw off a video for me or write it myself, how to correctly make a system dragging a frame? (Inventory for the game, which will be on the console) In order that it was clear how I want to do, here is an example:

example

    1 answer 1

    And what's stopping you from "combining" arrays? Make a list of arrays and memorize the current array. As a result, when you are "busting" the elements, just check to see if you go abroad. If the new index is -1, switch the cursor to the last element of the previous array (having received the array first and then its length), if the index is equal to the length of the current array, try switching to the next array.

    In other words, the concept of movement is approximately as follows:

     class Class1 { List<Slot[,]> _menus; int _xIndex; int _yIndex; int _maxX; int _maxY; Slot[,] _curentMenu; void Update() { if (Input.GetKeyDown(KeyCode.A)) { _xIndex--; if (_xIndex < 0) { ChangeMenu(Direction.Previous); } } if (Input.GetKeyDown(KeyCode.D)) { _xIndex++; if (_xIndex > _maxX) { ChangeMenu(Direction.Next); } } if (Input.GetKeyDown(KeyCode.W)) { _yIndex = Mathf.Clamp(++_yIndex, 0, _maxY); } if (Input.GetKeyDown(KeyCode.S)) { _yIndex = Mathf.Clamp(--_yIndex, 0, _maxY); } MoveFrame(); } void ChangeMenu(Direction direction) { var index = GetNewIndex(direction); _curentMenu = _menus[index]; //Меняем текущий массив слотов _maxX = _curentMenu.GetUpperBound(0); //Получаем количество слотов по X _maxY = _curentMenu.GetUpperBound(1); //Получаем количество слотов по Y _yIndex = _yIndex > _maxY ? _maxY : _yIndex; //Если текущий слот по Y превышает высоту нового меню, устанавливаем высоту на последний слот по высоте _xIndex = direction == Direction.Next ? 0 : _maxX - 1; //Если перемещались в следующий - устанавливаем слот в ноль, если в предыдущий в послдний слот по X } int GetNewIndex(Direction direction) { var curentIndex = _menus.IndexOf(_curentMenu); //Ищем индекс текущего меню в списка, чтобы понять, к кому мы можем перейти switch (direction) { case Direction.Next: curentIndex++; curentIndex = curentIndex == _menus.Count ? 0 : curentIndex; break; case Direction.Previous: curentIndex--; curentIndex = curentIndex < 0 ? _menus.Count - 1 : curentIndex; break; } return curentIndex; } void MoveFrame() { var slot = _curentMenu[_xIndex, _yIndex]; //Перемещаем рамку в позицию текущего слота } enum Direction { Next, Previous } 
    • It is interesting to see how you move the cursor up and down, storing items in a one-dimensional list without a lot of extra checks and conditions :) - RiotBr3aker
    • @ RiotBr3aker, so the vehicle showed its code) he did not ask to show how to do to move up and down, he has only left-right - M. Green
    • 4 keys and a gif like hint :) - RiotBr3aker
    • It is already better, but it is much easier to check for going beyond the boundaries and, moreover, to make more "convenient functionality" - take the module from the number - RiotBr3aker