2017-09-25 39 views
0

所以我有这段代码,现在它不显示任何图像,因为某些东西不适用于Resources.Load<Texture2D>("Assets/Sprites/Item Icons/Wanderer Crate/" + name);,当我运行代码时它不会输入项目图标,这使得我疯了连续3天......没有正确的答案堆栈溢出为我的代码...那么可能是什么呢?实现图像到脚本的最佳方式

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]); 
    } 
} 
+0

我没有答案,但你知道,要找到Unity和游戏开发的答案,最好去这里:https://gamedev.stackexchange.com – DHLopez

回答

0

如果你看一看Unitys'文件(https://docs.unity3d.com/ScriptReference/Resources.Load.html),它指出...

Resources.Load ::将存储在路径的资源文件夹中的资产。


我猜这个问题在这里。确保的父文件夹“Assets/Sprite/Item Icons/Wanderer Crate /”是名为“Resources”的文件夹。

+0

这就像一个魅力...我喜欢你,非常感谢你,这件事让我疯狂,教程中的那个人没有告诉我,我从来没有看过他的文件夹结构,谢谢你太多了 –