Hệ thống Kho đồ Đơn giản

Tags:
UI
Gameplay
Inventory
ScriptableObject
Architecture

C# Code

/* --- ItemData (ScriptableObject) --- */
// (Giữ nguyên hoặc tương tự snippet scriptableobject-for-data)
// [CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
// public class ItemData : ScriptableObject { ... }

/* --- InventorySlot.cs (Gắn vào từng ô UI) --- */
using UnityEngine;
using UnityEngine.UI;

public class InventorySlot : MonoBehaviour
{
    public Image icon;
    public Button removeButton;

    private ItemData item;

    public void AddItem(ItemData newItem)
    {
        item = newItem;
        icon.sprite = item.icon;
        icon.enabled = true;
        removeButton.interactable = true;
    }

    public void ClearSlot()
    {
        item = null;
        icon.sprite = null;
        icon.enabled = false;
        removeButton.interactable = false;
    }

    public void OnRemoveButton()
    {
        Inventory.instance.Remove(item);
    }
}

/* --- Inventory.cs (Singleton) --- */
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public static Inventory instance;
    void Awake() { if(instance != null) { Destroy(gameObject); return; } instance = this; }

    public List<ItemData> items = new List<ItemData>();
    public int space = 20;

    public delegate void OnItemChanged();
    public OnItemChanged onItemChangedCallback;

    public bool Add(ItemData item)
    {
        if (items.Count >= space) {
            Debug.Log("Not enough room.");
            return false;
        }
        items.Add(item);
        onItemChangedCallback?.Invoke();
        return true;
    }

    public void Remove(ItemData item)
    {
        items.Remove(item);
        onItemChangedCallback?.Invoke();
    }
}

/* --- InventoryUI.cs (Quản lý toàn bộ UI) --- */
using UnityEngine;

public class InventoryUI : MonoBehaviour
{
    public Transform itemsParent;
    private Inventory inventory;
    private InventorySlot[] slots;

    void Start()
    {
        inventory = Inventory.instance;
        inventory.onItemChangedCallback += UpdateUI;
        slots = itemsParent.GetComponentsInChildren<InventorySlot>();
    }

    void UpdateUI()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (i < inventory.items.Count)
            {
                slots[i].AddItem(inventory.items[i]);
            }
            else
            {
                slots[i].ClearSlot();
            }
        }
    }
}

Một hệ thống kho đồ cơ bản sử dụng ScriptableObject cho dữ liệu vật phẩm và quản lý các ô chứa trong UI.

Bình luận (0)

Bạn cần đăng nhập để có thể bình luận.

Chưa có bình luận nào. Hãy là người đầu tiên!

Bài viết liên quan

Một hệ thống đơn giản để hiển thị các đoạn hội thoại có nhiều câu và lựa chọn cho người chơi, sử dụng ScriptableObject.

UI
Gameplay
Interaction
ScriptableObject

Một cấu trúc cơ bản để tạo, theo dõi và hoàn thành nhiệm vụ, sử dụng ScriptableObject để định nghĩa nhiệm vụ.

Gameplay
Quest
ScriptableObject
Architecture

Một hệ thống linh hoạt cho phép người chơi tương tác với nhiều loại đối tượng (cửa, vật phẩm, NPC, công tắc) chỉ bằng một nút bấm (E) khi ở trong tầm, kèm hiển thị gợi ý trên màn hình.

Interaction
Gameplay
Player
Architecture
Interface
UI

Hiển thị các điểm đánh dấu (marker) cho các mục tiêu trên một thanh la bàn UI, giúp người chơi định hướng.

UI
Gameplay
Navigation

Quản lý thanh thể lực cho các hành động như chạy nước rút. Thể lực sẽ giảm khi chạy và tự động hồi lại khi đứng yên.

Gameplay
Player
Stamina
UI

Một cấu trúc Finite State Machine (FSM) đơn giản sử dụng enum và switch-case để quản lý các trạng thái khác nhau của AI, như đi tuần, truy đuổi và tấn công.

AI
Architecture
Design Pattern
Gameplay

Một hệ thống cho phép người chơi tương tác với nhiều loại đối tượng (cửa, NPC, vật phẩm) chỉ bằng một nút bấm khi ở trong tầm.

Gameplay
Interaction
Player
Architecture
Interface

Một hệ thống UI cho phép người chơi kéo thả vật phẩm giữa các ô trong kho đồ. Yêu cầu một CanvasGroup trên prefab của item để xử lý raycast.

UI
Inventory
Drag and Drop
Input

Áp dụng mẫu MVP để tách biệt logic game (Model), UI (View), và phần điều khiển trung gian (Presenter). Giúp code dễ dàng unit test và quản lý các luồng dữ liệu phức tạp trong UI.

Architecture
Design Pattern
UI
MVP

Một ví dụ cơ bản về cách sử dụng Coroutine để thực hiện một hành động sau một khoảng thời gian chờ, mà không làm đóng băng game.

Gameplay
Timer
Coroutine
Architecture