mirror of
https://github.com/DerTyp7/harvestdale-unity.git
synced 2025-10-30 04:57:09 +01:00
draggable items
This commit is contained in:
34
Assets/Scripts/UI/DraggableSlotContentUI.cs
Normal file
34
Assets/Scripts/UI/DraggableSlotContentUI.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/DraggableSlotContentUI.cs.meta
Normal file
11
Assets/Scripts/UI/DraggableSlotContentUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b88f5aa1c0fe8349953801d1f513200
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user