将图像实现为脚本的最佳方式

The best way to implement images into script

所以我有这段代码,但现在它不显示任何图像,因为某些东西不适用于 Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name); 并且当我 运行 编码时它没有进入项目图标和这让我连续 3 天发疯...我的代码在 Stack Overflow 上没有正确答案...那可能是什么?

Picture of Inspector's Item

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

[System.Serializable]
public class Item {
    public string itemName;
    public int itemID;
    public float itemPrice;
    public Texture2D itemIcon;
    public ItemType1 itemCrate;

    public enum ItemType1
    {
        Wanderer_Crate
    }

    public ItemType2 itemType;

    public enum ItemType2
    {
        Key,
        Mask,
        Pants,
        Hat,
        Shoes,
        Shirt,
        Glasses,
        Jacket
    }

    public ItemType3 itemRarity;

    public enum ItemType3
    {
        Red,
        Purple,
        Violet,
        Blue,
        Green,
        Gray
    }

    public Item(string name, int id, float price, ItemType1 crate, ItemType2 type, ItemType3 rarity)
    {
        itemName = name;
        itemID = id;
        itemPrice = price;
        itemIcon = Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);
        itemCrate = crate;
        itemType = type;
        itemRarity = rarity;
    }
}

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

public class Inventory : MonoBehaviour
{
    public static Inventory Instance { set; get; }

    public List<Item> inventory = new List<Item>();
    public List<Item> slots = new List<Item>();

    public GameObject slotPreset;
    public GameObject parentInvetoryGridForSlots;

    public Text itemNameText;

    private ItemDatabase database;

    public void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
        //Add item to invetory code - inventory.Add(database.Items[16]);

        inventory.Add(database.Items[16]);
        inventory.Add(database.Items[3]);
        inventory.Add(database.Items[4]);
        inventory.Add(database.Items[5]);
        inventory.Add(database.Items[6]);
        inventory.Add(database.Items[7]);

        for (int i = 0; i < inventory.Count; i++)
        {
            GameObject spawnedSlot = Instantiate(slotPreset);
            spawnedSlot.transform.SetParent(parentInvetoryGridForSlots.gameObject.transform);

            spawnedSlot.GetComponentInChildren<Text>().text = inventory[i].itemName;
        }
    }

    public void inventoryRefresh(int itemIDofItem)
    {
        GameObject spawnedSlot = Instantiate(slotPreset);
        spawnedSlot.transform.SetParent(parentInvetoryGridForSlots.gameObject.transform);

        spawnedSlot.GetComponentInChildren<Text>().text = database.Items[itemIDofItem].itemName;
    }

    public void addItemToInventory(int itemIDofItem)
    {
        inventory.Add(database.Items[itemIDofItem]);
    }
}

如果您查看 Unitys 的文档 (https://docs.unity3d.com/ScriptReference/Resources.Load.html),它会指出...

Resources.Load :: Loads an asset stored at path in a Resources folder.


我猜问题出在这里。确保 "Assets/Sprites/Item Icons/Wanderer Crate/" 的父文件夹是名称为 "Resources".

的文件夹