draggable items

This commit is contained in:
Janis
2023-02-22 13:39:32 +01:00
parent 5773aa0af7
commit 13902bc589
6 changed files with 218 additions and 29 deletions

View File

@@ -1,5 +1,67 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4444084577065566223
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 243536393184956053}
- component: {fileID: 8405146060336643116}
- component: {fileID: 9171659427163776436}
m_Layer: 5
m_Name: Content
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &243536393184956053
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4444084577065566223}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 9079120990588915013}
- {fileID: 9079120991702230699}
m_Father: {fileID: 9079120991637034567}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &8405146060336643116
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4444084577065566223}
m_CullTransparentMesh: 1
--- !u!114 &9171659427163776436
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4444084577065566223}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4b88f5aa1c0fe8349953801d1f513200, type: 3}
m_Name:
m_EditorClassIdentifier:
parentAfterDrag: {fileID: 0}
image: {fileID: 9079120990588915018}
--- !u!1 &9079120990588915012
GameObject:
m_ObjectHideFlags: 0
@@ -25,12 +87,12 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9079120990588915012}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 9079120991637034567}
m_Father: {fileID: 243536393184956053}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@@ -66,7 +128,7 @@ MonoBehaviour:
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Sprite: {fileID: 21300000, guid: 31eee06fb7f67e44095a14ddf9b93f29, type: 3}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
@@ -88,6 +150,7 @@ GameObject:
- component: {fileID: 9079120991637034565}
- component: {fileID: 9079120991637034564}
- component: {fileID: 2634354024091149696}
- component: {fileID: 2202512530307776240}
m_Layer: 5
m_Name: Slot
m_TagString: Untagged
@@ -107,8 +170,7 @@ RectTransform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 9079120990588915013}
- {fileID: 9079120991702230699}
- {fileID: 243536393184956053}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -167,11 +229,36 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 6c881951842cc4e4799994f5eb91dd90, type: 3}
m_Name:
m_EditorClassIdentifier:
contentObject: {fileID: 4444084577065566223}
inventoryItem:
item: {fileID: 0}
count: 0
itemSpriteImage: {fileID: 9079120990588915018}
quantityText: {fileID: 9079120991702230696}
--- !u!114 &2202512530307776240
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9079120991637034566}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8a8695521f0d02e499659fee002a26c2, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Padding:
m_Left: 0
m_Right: 0
m_Top: 0
m_Bottom: 0
m_ChildAlignment: 4
m_StartCorner: 0
m_StartAxis: 0
m_CellSize: {x: 43, y: 43}
m_Spacing: {x: 0, y: 0}
m_Constraint: 0
m_ConstraintCount: 2
--- !u!1 &9079120991702230698
GameObject:
m_ObjectHideFlags: 0
@@ -197,12 +284,12 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 9079120991702230698}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 9079120991637034567}
m_Father: {fileID: 243536393184956053}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}

View File

@@ -7,7 +7,17 @@ public class Inventory : MonoBehaviour
public bool isPlayerInventory = false;
public int maxSlots = 20; // maximum number of slots in the inventory
public List<InventoryItem> items = new List<InventoryItem>(); // list of items in the inventory
public InventoryItem[] items; // list of items in the inventory
private void Awake()
{
items = new InventoryItem[maxSlots];
for (int i = 0; i < maxSlots; i++)
{
items[i] = null;
}
}
// adds an item to the inventory, returns the quantity of items which could not be added
public int Add(Item item, int count)
@@ -17,10 +27,12 @@ public class Inventory : MonoBehaviour
// check if the item is stackable
if (item.stackable)
{
// look for an existing stack of the same item in the inventory
foreach (InventoryItem invItem in items)
for (int i = 0; i < items.Length; i++)
{
if (invItem.item == item && invItem.count < item.maxStackSize)
InventoryItem invItem = items[i];
if (invItem != null && invItem.item == item && invItem.count < item.maxStackSize)
{
// add as many items as possible to the stack
int space = item.maxStackSize - invItem.count;
@@ -43,11 +55,30 @@ public class Inventory : MonoBehaviour
}
// if there is still remaining count, add new stacks of the item
while (remainingCount > 0 && items.Count < maxSlots)
for (int i = 0; i < items.Length; i++)
{
int toAdd = Mathf.Min(item.maxStackSize, remainingCount);
items.Add(new InventoryItem(item, toAdd));
remainingCount -= toAdd;
InventoryItem invItem = items[i];
Debug.Log(invItem);
if (invItem.item == null)
{
int toAdd = Mathf.Min(item.maxStackSize, remainingCount);
items[i] = new InventoryItem(item, toAdd);
remainingCount -= toAdd;
// exit the loop if all items have been added
if (remainingCount == 0)
{
if (isPlayerInventory)
{
OnPlayerInventoryChanged?.Invoke();
}
return 0;
}
}
}
// return the quantity of items which could not be added
@@ -55,6 +86,7 @@ public class Inventory : MonoBehaviour
{
OnPlayerInventoryChanged?.Invoke();
}
return remainingCount;
}
@@ -65,10 +97,10 @@ public class Inventory : MonoBehaviour
int remainingCount = count;
// look for an existing stack of the item in the inventory
for (int i = 0; i < items.Count; i++)
for (int i = 0; i < items.Length; i++)
{
InventoryItem invItem = items[i];
if (invItem.item == item)
if (invItem != null && invItem.item == item)
{
// remove as many items as possible from the stack
int toRemove = Mathf.Min(invItem.count, remainingCount);
@@ -78,7 +110,7 @@ public class Inventory : MonoBehaviour
// remove the stack if it's now empty
if (invItem.count == 0)
{
items.RemoveAt(i);
items[i] = null;
}
// exit the loop if all items have been removed
@@ -100,6 +132,17 @@ public class Inventory : MonoBehaviour
}
return remainingCount;
}
public void SwapItems(int index1, int index2)
{
InventoryItem temp = items[index1];
items[index1] = items[index2];
items[index2] = temp;
if (isPlayerInventory)
{
OnPlayerInventoryChanged?.Invoke();
}
}
}

View File

@@ -0,0 +1,34 @@
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DraggableSlotContentUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public int slotIndex;
[HideInInspector] public Transform parentAfterDrag;
public Image image;
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("Start Drag");
parentAfterDrag = transform.parent;
transform.SetParent(transform.root);
transform.SetAsLastSibling();
image.raycastTarget = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragging");
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("End drag");
transform.SetParent(parentAfterDrag);
image.raycastTarget = true;
// invoke
Inventory.OnPlayerInventoryChanged?.Invoke();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b88f5aa1c0fe8349953801d1f513200
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -25,7 +25,9 @@ public class InventoryUI : MonoBehaviour
for (int i = 0; i < playerInventory.maxSlots; i++)
{
slotUIList.Add(Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>());
SlotUI newSlot = Instantiate(slotPrefab, Vector3.zero, Quaternion.identity, slotListObj.transform).GetComponent<SlotUI>();
newSlot.slotIndex = i;
slotUIList.Add(newSlot);
}
}
private void UpdateSlots()
@@ -34,14 +36,9 @@ public class InventoryUI : MonoBehaviour
int i = 0;
foreach (SlotUI slotUi in slotUIList)
{
if (i < playerInventory.items.Count)
{
slotUi.SetInventoryItem(playerInventory.items[i]);
}
else
{
slotUi.SetInventoryItem(null);
}
slotUi.SetInventoryItem(playerInventory.items[i]);
i++;
}

View File

@@ -2,16 +2,21 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SlotUI : MonoBehaviour
using UnityEngine.EventSystems;
public class SlotUI : MonoBehaviour, IDropHandler
{
public int slotIndex;
[SerializeField] private GameObject contentObject;
[SerializeField] private InventoryItem inventoryItem;
[SerializeField] private Image itemSpriteImage;
[SerializeField] private TMPro.TextMeshProUGUI quantityText;
private Inventory playerInventory;
private void Start()
{
UpdateSlot();
contentObject.GetComponent<DraggableSlotContentUI>().slotIndex = slotIndex;
playerInventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
public void SetInventoryItem(InventoryItem newInventoryItem)
{
@@ -24,11 +29,14 @@ public class SlotUI : MonoBehaviour
if (inventoryItem != null)
{
itemSpriteImage.sprite = inventoryItem?.item?.sprite ?? null;
Debug.Log(itemSpriteImage.sprite);
if (itemSpriteImage.sprite == null)
{
itemSpriteImage.color = new Color(0, 0, 0, 0);
}
else
{
itemSpriteImage.color = new Color(255, 255, 255, 1);
}
quantityText.SetText(inventoryItem.count != 0 ? inventoryItem.count.ToString() : "");
}
@@ -41,4 +49,13 @@ public class SlotUI : MonoBehaviour
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log("DROP");
DraggableSlotContentUI dropped = eventData.pointerDrag.GetComponent<DraggableSlotContentUI>();
playerInventory.SwapItems(dropped.slotIndex, slotIndex);
}
}