LoginSignup
0
0

FarmRPG Unity Inventory&Item Part1 1/2  InvnetoryManager ScritableObjectのデータを読み込む

Posted at

概要

今回はItemのScriptableObjectのデータを読み込むInventoryManagerクラスを作成します。

インスペクター上では確認できないのでRiderのデバッグ画面で読み込みを確認
Startメソッドの終了時点を見ています。

image.png

開発環境

IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10

UnityEditor上の設定

新規オブジェクトInventoryManagerを作成して InventoryManagerスクリプトをアタッチします。
itemListの引数はScriptableObjectを指定します。

image.png

コード部分

InventoryManager

InventoryManager.cs
using System.Collections.Generic;
using UnityEngine;


public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{
    private Dictionary<int, ItemDetails> itemDetailDictionary;
    [SerializeField] private SO_ItemList itemList = null;

    private void Start()
    {
        CreateItemDetailsDictionary();
    }

    private void CreateItemDetailsDictionary()
    {
        itemDetailDictionary = new Dictionary<int, ItemDetails>();

        foreach (ItemDetails itemDetails in itemList.itemDetails)
        {
            itemDetailDictionary.Add(itemDetails.itemCode,itemDetails);
        }
    }
}


SO_ItemList

SO_ItemList.cs
[CreateAssetMenu(fileName = "so_ItemList", menuName = "Scritable Objects/Item/Item List")]
public class SO_ItemList : ScriptableObject
{
    [SerializeField]
    public List<ItemDetails> itemDetails;
    
}


ItemDetail

ItemDetail.cs
using UnityEngine;

[System.Serializable]
public class ItemDetails
{
    public int itemCode;
    public ItemType itemType;
    public string itemDescription;
    public Sprite itemSprite;
    public string itemLongDescription;
    public short itemUseGridRadius;
    public float itemUseRadius; 
    public bool isStartingItem;
    public bool canBePickedUp;
    public bool canBeDropped;
    public bool canBeEaten;
    public bool canBeCarried;
}



参考

C#

Dictionary

image.png

Dictionary.Add(TKey, TValue)

image.png

Unity Editor コンポーネント

Unity スクリプト

その他

Section7 22

github コミット分(個人確認用 privateなので見れません)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0