Hello everyone,
I am working on a top down rpg 2D, as of now everything worked fine but as I am quite new on programming I face a big issue. I am trying to create a drag and drop system, in order to drag and drop the sprite, labelText & stackSizeText of the scriptable object in the different slots of the UI list.
I have an inventory that works well with a list and dictionary, they store scriptable objects for now potions.
This is the script component of the scriptable objects
]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class Loot : ScriptableObject
{
public Sprite lootSprite;
public string lootName;
public int dropChance;
public Loot(string lootName, int dropChance)
{
this.lootName = lootName;
this.dropChance = dropChance;
}
}
The code I use to store the scriptable objects in the list/dictionary is this one:
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static event Action<List<InventoryItem>> OnInventoryChange;
public List<InventoryItem> inventory = new List<InventoryItem>();
private Dictionary<Loot, InventoryItem> itemDictionary = new Dictionary<Loot, InventoryItem>();
private void OnEnable()
{
Hazarditem.OnHazarditemCollected += Add;
}
private void OnDisable()
{
Hazarditem.OnHazarditemCollected -= Add;
}
public void Add(Loot itemData)
{
if(itemDictionary.TryGetValue(itemData, out InventoryItem item))
{
item.AddToStack();
Debug.Log($"{item.itemData.lootName} total stack is now {item.stackSize}");
OnInventoryChange?.Invoke(inventory);
}
else
{
InventoryItem newItem = new InventoryItem(itemData);
inventory.Add(newItem);
itemDictionary.Add(itemData, newItem);
Debug.Log($"Added{itemData.lootName} to the inventory for the first time");
OnInventoryChange?.Invoke(inventory);
}
}
public void Remove(Loot itemData)
{
if (itemDictionary.TryGetValue(itemData, out InventoryItem item))
{
item.RemoveFromStack();
if (item.stackSize == 0)
{
inventory.Remove(item);
itemDictionary.Remove(itemData);
}
OnInventoryChange?.Invoke(inventory);
}
}
}
Each UIslot has a script called InventorySlot, where it stores the sprite the name & stacksize.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
public Image lootSprite;
public TextMeshProUGUI labelText;
public TextMeshProUGUI stackSizeText;
public void ClearSlot()
{
lootSprite.enabled = false;
labelText.enabled = false;
stackSizeText.enabled = false;
}
public void DrawSlot (InventoryItem item)
{
if(item == null)
{
ClearSlot();
return;
}
lootSprite.enabled = true;
labelText.enabled = true;
stackSizeText.enabled = true;
lootSprite.sprite = item.itemData.lootSprite;
labelText.text = item.itemData.lootName;
stackSizeText.text = item.stackSize.ToString();
}
}
This is the script used for the UI List:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryManager : MonoBehaviour
{
public GameObject slotPrefab;
public List<InventorySlot> inventorySlots = new List<InventorySlot>(20);
private void OnEnable()
{
Inventory.OnInventoryChange += DrawInventory;
}
private void OnDisable()
{
Inventory.OnInventoryChange -= DrawInventory;
}
void ResetInventory()
{
foreach (Transform childTransform in transform)
{
Destroy(childTransform.gameObject);
}
inventorySlots = new List<InventorySlot>(20);
}
void DrawInventory(List<InventoryItem> inventory)
{
ResetInventory();
for (int i = 0; i < inventorySlots.Capacity; i++)
{
// Create the 20 slots
CreateInventorySlot();
}
for (int i = 0; i < inventory.Count; i++)
{
inventorySlots[i].DrawSlot(inventory[i]);
}
}
void CreateInventorySlot() // Create the 20 slots
{
GameObject newSlot = Instantiate(slotPrefab);
newSlot.transform.SetParent(transform, false);
InventorySlot newSlotComponent = newSlot.GetComponent<InventorySlot>();
newSlotComponent.ClearSlot();
inventorySlots.Add(newSlotComponent);
}
}
I also have other scripts that are linked to these scripts but I think these 4 are the most important ones to solve the problem. I already tried multiple times to find a solution but I am stuck…
I hope that you will be able to help me and don’t hesitate if you have any question.
Thank you,