iLMS知識社群歷程檔Login
Position: 黃國哲 > Unity
1100827 物品資料建立
by 黃國哲 2021-08-27 08:06:29, Reply(0), Views(504)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Item",menuName = "Inventory/New Item")]
public class item : ScriptableObject
{
    public string itemName;
    public Sprite itemImage;
    public int itemHeld;
    [TextArea]
    public string itemInfo;
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Inventory",menuName = "Inventory/New Inventory")]
public class inventory : ScriptableObject
{
    public List<item> itemlist = new List<item> (); 
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class itemonwork : MonoBehaviour
{
    public item thisItem ; //掛在物體上,定義該物品的屬性,資料來源是寫好的類別item
    public inventory playerInventory ; //選擇物品要放入的背包,資料來源是寫好的類別inventory

    public void 當滑鼠點下()
    {         
        AddNewItem();  //執行加入物品這個動作(動作模式在下面))
        Destroy(gameObject);   //摧毀碰到的這個物件             
    }

    public void AddNewItem()
    {
        if (!playerInventory.itemlist.Contains(thisItem))
        {playerInventory.itemlist.Add(thisItem);} //如果包包沒有這個物品,就加入這個物品

        else
        {
            thisItem.itemHeld += 1; //如果已經有了,就持有數量+1
        }
    }

}


Reply