所以我有这个代码,现在它没有显示任何图像,因为有些东西不能与Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);
一起使用,当我运行代码时,它不会输入项目图标和这让我连续三天生气...我的代码Stack Overflow上没有正确的答案......那可能是什么呢?
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]);
}
}
答案 0 :(得分:0)
如果你看看Unitys&#39;文档(https://docs.unity3d.com/ScriptReference/Resources.Load.html),它声明......
Resources.Load
::加载存储在Resources文件夹中路径的资产。
我猜这个问题就在这里。确保&#34; Assets / Sprites / Item Icons / Wanderer Crate /&#34; 的父文件夹是名为&#34; Resources&#34; 的文件夹EM>