iLMS知識社群歷程檔登入
位置: 黃國哲 > Unity
1100902 背包搜尋腳本
by 黃國哲 2021-09-02 23:34:41, 回應(0), 人氣(372)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryDisplay : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform targetTransform;
    public InventoryItemDisplay InventoryDisplayPrefab;

    public void Prime(List<InventoryItem> items)
    {
        foreach (InventoryItem item in items)
        {
            InventoryItemDisplay display = (InventoryItemDisplay)Instantiate(InventoryDisplayPrefab);
            display.transform.SetParent(targetTransform,false);
            display.Prime(item);
        }
    }
}

///////////////////

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

public class Inventory : MonoBehaviour
{
    public List<InventoryItem> items = new List<InventoryItem>();
    public  InventoryDisplay    InventoryDisplayPrefab;
    // Start is called before the first frame update
    void Start()
    {
        InventoryDisplay Inventory = (InventoryDisplay)Instantiate(InventoryDisplayPrefab);
        Inventory.Prime(items);
    }

}

/////////////////////////////////

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

public class InventoryItemDisplay : MonoBehaviour
{
    public Text textName;
    public Image sprite;

    public InventoryItem item;

    // Start is called before the first frame update
    void Start()
    {
        if (item != null) Prime (item);
    }

    public void Prime(InventoryItem item)
    {
        this.item = item;
        if (textName != null)
            textName.text = item.displayName;
        if (sprite != null)
            sprite.sprite = item.sprite;
    }
}

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

[CreateAssetMenu(fileName = "List Item",menuName = "Inventory/List Item")]

public class InventoryItem : ScriptableObject
{
    

    public string displayName;
    [Multiline]
    public string disc;
    public Sprite sprite;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


回應