所以我有这段代码,现在它不显示任何图像,因为某些东西不适用于Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);
,当我运行代码时它不会输入项目图标,这使得我疯了连续3天......没有正确的答案堆栈溢出为我的代码...那么可能是什么呢?实现图像到脚本的最佳方式
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]);
}
}
我没有答案,但你知道,要找到Unity和游戏开发的答案,最好去这里:https://gamedev.stackexchange.com – DHLopez