using UnityEngine;
[System.Serializable]
public class CraftingIngredient
{
public ItemData item;
public int quantity;
}
Create >> Scripting >> ScriptableObject Script
using UnityEngine;
[CreateAssetMenu(fileName = "New Recipe", menuName = "Crafting/Recipe")]
public class CraftingRecipe : ScriptableObject
{
public CraftingIngredient[] ingredients;
public ItemData result;
}
Items
- Water
- Apple juice
- Orange juice
Recipe items
- Apple juice
- Orange juice
select apple juice , orange juice and water icon then change the sprite mode to single
remember to click apply
create >> Inventory>> ItemData (please check the inventory system tutorial)
create apple juice, orange juice and water ItemData
remember to fill in the apple itemdata properties
do this also to orange juice and water
Update your InventorySystem.cs
public bool HasItems(CraftingIngredient[] required)
{
foreach (var entry in required)
{
if (!items.ContainsKey(entry.item) || items[entry.item] < entry.quantity)
return false;
}
return true;
}
//Check if we have the enough ingredient
public void RemoveItems(CraftingIngredient[] required)
{
foreach (var entry in required)
{
if (items.ContainsKey(entry.item))
items[entry.item] -= entry.quantity;
}
}
//remove the ingredient after crafting
Full code for InventorySystem.cs
using NUnit.Framework;
using System.Collections.Generic;
using UnityEngine;
public class InventorySystem : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public static InventorySystem instance;
public Dictionary<ItemData, int> itemDict = new Dictionary<ItemData, int>();
private void Awake()
{
if (instance == null) instance = this;
else Destroy(gameObject);
}
public void AddItem(ItemData item)
{
if(itemDict.ContainsKey(item))
{
itemDict[item]++;
}
else
{
itemDict[item] = 1;
}
Debug.Log("Picked up:" + item.itemName + "x" + itemDict[item]);
InventoryUI.instance.RefreshUI();
}
public Dictionary<ItemData, int> GetAllItems()
{
return itemDict;
}
public bool HasItems(CraftingIngredient[] required)
{
foreach(var entry in required)
{
if(!itemDict.ContainsKey(entry.item)|| itemDict[entry.item]<entry.quantity)
{
return false;
}
}
return true;
}
public void RemoveItems(CraftingIngredient[] required)
{
foreach (var entry in required)
{
if(itemDict.ContainsKey(entry.item))
{
itemDict[entry.item] -= entry.quantity;
}
}
}
}
craftingSystem.cs (right click create empty and rename it as crafting system, attach your code to this gameobject)
using UnityEngine;
public class CraftingSystem : MonoBehaviour
{
public static CraftingSystem instance;
public CraftingRecipe[] recipes;
private void Awake()
{
instance = this;
}
public bool Craft(CraftingRecipe recipe)
{
if (InventorySystem.instance.HasItems(recipe.ingredients))
{
InventorySystem.instance.RemoveItems(recipe.ingredients);
InventorySystem.instance.AddItem(recipe.result);
return true;
}
return false;
}
}
attach your script to an empty object called CraftingSystem
then add your recipes to your script.