download 3 models and icons,
log
bolt
wood chair
using UnityEngine;
public enum ItemType { Material, Food, Drink}
[CreateAssetMenu(fileName = "itemSO", menuName = "Scriptable Objects/itemSO")]
public class itemSO : ScriptableObject
{
public string itemName;
public Sprite icon;
public ItemType type;
public int maxStack = 99;
public GameObject itemModel;
}
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "ItemDatabase", menuName = "Scriptable Objects/ItemDatabase")]
public class ItemDatabase : ScriptableObject
{
public List<itemSO> items;
public itemSO GetItemByName(string name)
{
return items.Find(x => x.itemName == name);
}
}
create >> scriptable object >> itemDatabase
add all the available itemSO
using System.Collections.Generic;
using UnityEngine;
public class GridManager : MonoBehaviour
{
public float cellSize = 1f; // grid spacing
// Keep track of used grid cells
private HashSet<Vector3Int> occupiedCells = new HashSet<Vector3Int>();
// Convert world position to grid coordinate
public Vector3Int GetGridPosition(Vector3 worldPos)
{
return Vector3Int.RoundToInt(worldPos / cellSize);
}
// Check if grid cell available
public bool IsCellAvailable(Vector3Int gridPos)
{
return !occupiedCells.Contains(gridPos);
}
// Mark grid as used
public void OccupyCell(Vector3Int gridPos)
{
occupiedCells.Add(gridPos);
}
// Free spot if item removed (optional)
public void FreeCell(Vector3Int gridPos)
{
if (occupiedCells.Contains(gridPos))
occupiedCells.Remove(gridPos);
}
}
open an emtpy object name it as grid manager
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; // <-- required!
using UnityEngine.UI;
public class ClickableImage : MonoBehaviour, IPointerClickHandler
{
private Image img;
string spriteName = null;
GridManager gridManager;
GameObject itemPrefab =null;
public ItemDatabase database;
bool inventoryIconSelected = false;
public Dictionary<string, itemSO> itemDict;
void Start()
{
img = GetComponent<Image>();
gridManager = GameObject.Find("gridManager").GetComponent<GridManager>();
//itemSO[] allItems = Resources.LoadAll<itemSO>("Items");
//itemDict = new Dictionary<string, itemSO>();
//foreach (itemSO item in allItems)
// itemDict[item.itemName] = item;
//foreach (var key in itemDict.Keys)
// Debug.Log("Loaded item: " + key);
}
public void OnPointerClick(PointerEventData eventData)
{
//if (img.sprite == null)
//{
// Debug.LogWarning("This image has no sprite assigned!");
// return;
//}
//spriteName = img.sprite.name;
////itemSelected = itemDict["Apple"];
////itemPrefab = itemSelected.itemModel;
////foreach (var key in itemDict.Keys)
//// Debug.Log("Loaded item: " + key);
//Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
void Update()
{
if(inventoryIconSelected)
{
placeitemFunction();
}
}
public void placeitemFunction()
{
if (spriteName != null)
if (Input.GetMouseButtonDown(0)) // left click
{
if (spriteName != null)
{
itemSO itemSelected = database.GetItemByName(spriteName);
itemPrefab = itemSelected.itemModel;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
Vector3Int gridPos = gridManager.GetGridPosition(hit.point);
if (gridManager.IsCellAvailable(gridPos))
{
float itemHeight = itemPrefab.GetComponent<Renderer>().bounds.extents.y;
Vector3 spawnPos = new Vector3(gridPos.x, itemHeight, gridPos.z) * gridManager.cellSize;
Instantiate(itemPrefab, spawnPos, Quaternion.identity);
gridManager.OccupyCell(gridPos);
Debug.Log("Placed at " + gridPos);
inventoryIconSelected = false;
}
else
{
Debug.Log("❌ Grid not available! Something already placed.");
}
}
}
}
public void iconClick()
{
if (img.sprite == null)
{
Debug.LogWarning("This image has no sprite assigned!");
return;
}
spriteName = img.sprite.name;
inventoryIconSelected = true;
Invoke("iconselectedStatus",10f);
Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
public void iconselectedStatus()
{
inventoryIconSelected = false;
}
}
add clickable Image script to your icon, add the database with your item database
add button component to your icon
for the onclick, add the icon to it and select the ClickableImage.iconClick
add recipeSO for your chair
add crafting button
using UnityEngine;
public enum ItemType { Material, Food, Drink, Furniture , others}
[CreateAssetMenu(fileName = "itemSO", menuName = "Scriptable Objects/itemSO")]
public class itemSO : ScriptableObject
{
public string itemName;
public Sprite icon;
public ItemType type;
public int maxStack = 99;
public GameObject itemModel;
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; // <-- required!
using UnityEngine.UI;
public class ClickableImage : MonoBehaviour, IPointerClickHandler
{
private Image img;
string spriteName = null;
GridManager gridManager;
GameObject itemPrefab =null;
public ItemDatabase database;
bool inventoryIconSelected = false;
ItemType itemType = ItemType.others;
public Dictionary<string, itemSO> itemDict;
itemSO itemSelected;
void Start()
{
img = GetComponent<Image>();
gridManager = GameObject.Find("gridManager").GetComponent<GridManager>();
}
public void OnPointerClick(PointerEventData eventData)
{
//if (img.sprite == null)
//{
// Debug.LogWarning("This image has no sprite assigned!");
// return;
//}
//spriteName = img.sprite.name;
////itemSelected = itemDict["Apple"];
////itemPrefab = itemSelected.itemModel;
////foreach (var key in itemDict.Keys)
//// Debug.Log("Loaded item: " + key);
//Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
void Update()
{
if(inventoryIconSelected && itemType == ItemType.Furniture )
// if (inventoryIconSelected)
{
placeitemFunction();
}
}
public void placeitemFunction()
{
if (spriteName != null)
if (Input.GetMouseButtonDown(0)) // left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
Vector3Int gridPos = gridManager.GetGridPosition(hit.point);
if (gridManager.IsCellAvailable(gridPos))
{
float itemHeight = itemPrefab.GetComponent<Renderer>().bounds.extents.y;
Vector3 spawnPos = new Vector3(gridPos.x, itemHeight, gridPos.z) * gridManager.cellSize;
Instantiate(itemPrefab, spawnPos, Quaternion.identity);
gridManager.OccupyCell(gridPos);
Debug.Log("Placed at " + gridPos);
inventoryIconSelected = false;
}
else
{
Debug.Log("❌ Grid not available! Something already placed.");
}
}
}
}
public void iconClick()
{
if (img.sprite == null)
{
Debug.LogWarning("This image has no sprite assigned!");
return;
}
spriteName = img.sprite.name;
inventoryIconSelected = true;
Invoke("iconselectedStatus",20f);
if (spriteName != null)
{
itemSelected = database.GetItemByName(spriteName);
itemPrefab = itemSelected.itemModel;
itemType = itemSelected.type;
Debug.Log("item type" + itemType);
}
Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
public void iconselectedStatus()
{
inventoryIconSelected = false;
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; // <-- required!
using UnityEngine.UI;
public class ClickableImage : MonoBehaviour, IPointerClickHandler
{
private Image img;
string spriteName = null;
GridManager gridManager;
GameObject itemPrefab =null;
public ItemDatabase database;
bool inventoryIconSelected = false;
ItemType itemType = ItemType.others;
public Dictionary<string, itemSO> itemDict;
itemSO itemSelected;
Inventory inventory;
void Start()
{
img = GetComponent<Image>();
gridManager = GameObject.Find("gridManager").GetComponent<GridManager>();
inventory = GameObject.Find("inventory").GetComponent<Inventory>();
}
public void OnPointerClick(PointerEventData eventData)
{
//if (img.sprite == null)
//{
// Debug.LogWarning("This image has no sprite assigned!");
// return;
//}
//spriteName = img.sprite.name;
////itemSelected = itemDict["Apple"];
////itemPrefab = itemSelected.itemModel;
////foreach (var key in itemDict.Keys)
//// Debug.Log("Loaded item: " + key);
//Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
void Update()
{
if(inventoryIconSelected && itemType == ItemType.Furniture )
// if (inventoryIconSelected)
{
placeitemFunction();
}
}
public void placeitemFunction()
{
if (spriteName != null)
if (Input.GetMouseButtonDown(0)) // left click
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
Vector3Int gridPos = gridManager.GetGridPosition(hit.point);
if (gridManager.IsCellAvailable(gridPos))
{
float itemHeight = itemPrefab.GetComponent<Renderer>().bounds.extents.y;
Vector3 spawnPos = new Vector3(gridPos.x, itemHeight, gridPos.z) * gridManager.cellSize;
Instantiate(itemPrefab, spawnPos, Quaternion.identity);
gridManager.OccupyCell(gridPos);
Debug.Log("Placed at " + gridPos);
inventoryIconSelected = false;
inventory.RemoveItem(itemSelected, 1);
inventory.refreshUI();
}
else
{
Debug.Log("❌ Grid not available! Something already placed.");
}
}
}
}
public void iconClick()
{
if (img.sprite == null)
{
Debug.LogWarning("This image has no sprite assigned!");
return;
}
spriteName = img.sprite.name;
inventoryIconSelected = true;
Invoke("iconselectedStatus",20f);
if (spriteName != null)
{
itemSelected = database.GetItemByName(spriteName);
itemPrefab = itemSelected.itemModel;
itemType = itemSelected.type;
Debug.Log("item type" + itemType);
}
Debug.Log("Clicked Image Sprite Name: " + spriteName);
}
public void iconselectedStatus()
{
inventoryIconSelected = false;
}
}