public void OnEndDrag(PointerEventData eventData) { // return if there is no assigned cell if (Cell == null) { return; } // get object below mouse GameObject currentRaycast = eventData.pointerCurrentRaycast.gameObject; if (currentRaycast) { if (currentRaycast.tag == "ToolbarSlot") { // get toolbar cell below mouse ToolbarCellUI toolbarCell = currentRaycast.GetComponent <ToolbarCellUI>() ?? currentRaycast.GetComponentInChildren <ToolbarCellUI>() ?? currentRaycast.GetComponentInParent <ToolbarCellUI>(); int index = toolbarCell.index; // if can set assigned cell's item to toolbar if (InventorySystem.CanSetToolbarItem(index, Cell, out toolbarCell)) { if (toolbarCell.Cell != null) { // swap cells InventoryCellUI cell = Cell; SetCell(toolbarCell.Cell); toolbarCell.SetCell(cell); } else { // move cell toolbarCell.SetCell(Cell); ClearCell(); } } } } else { // reset cell Cell.toolbarIndex = -1; ClearCell(); } // reset position etc. transform.position = startPosition; canvasGroup.blocksRaycasts = true; canvasGroup.alpha = 1f; }
public void OnEndDrag(PointerEventData eventData) { if (item == null) { return; } // used to know if player wants to drop item, if true player won't drop item bool setToolbar = false; // get object below mouse GameObject currentRaycast = eventData.pointerCurrentRaycast.gameObject; if (currentRaycast) { if (currentRaycast.tag == "InventorySlot") { // get inventory cell below mouse InventoryCellUI target = currentRaycast.gameObject.GetComponent <InventoryCellUI>(); // swap current cell with target cell ^ Player.localPlayer.CmdSwapItems(new Vector2Byte(indexPosition), new Vector2Byte(target.indexPosition)); ToolbarCellUI currentCell = InventorySystem.GetToolbarCell(toolbarIndex); ToolbarCellUI targetCell = InventorySystem.GetToolbarCell(target.toolbarIndex); // swap toolbar indexes int nToolbarIndex = toolbarIndex; toolbarIndex = target.toolbarIndex; target.toolbarIndex = nToolbarIndex; // swap toolbar cells currentCell?.SetCell(target); targetCell?.SetCell(this); } else if (currentRaycast.tag == "ToolbarSlot") { // get toolbar cell below mouse ToolbarCellUI toolbarCell = currentRaycast.GetComponent <ToolbarCellUI>() ?? currentRaycast.GetComponentInChildren <ToolbarCellUI>() ?? currentRaycast.GetComponentInParent <ToolbarCellUI>(); int index = toolbarCell.index; // if can set current item to toolbar if (InventorySystem.CanSetToolbarItem(index, this, out toolbarCell)) { // clear toolbar InventorySystem.ClearToolbarItem(toolbarIndex); // don't drop item setToolbar = true; // assing cell toolbarIndex toolbarIndex = index; // set toolbarCell to this toolbarCell.SetCell(this); } } } // drop item if mouse position X is lower than inventory panel position X (left side) and toolbarIndex == false if (!setToolbar && eventData.position.x < InventorySystem.Instance.dropItemPositionX) { Player.localPlayer.CmdDropItem(new Vector2Byte(indexPosition)); } // reset position etc. transform.position = startPosition; canvasGroup.blocksRaycasts = true; canvasGroup.alpha = 1f; }