英文:
Cannot set image of image object dynamically
问题
I'm trying to make an inventory system. When the player presses tab, a UI opens with three images. I dragged all three images from the Hierarchy into an Image array, and I want to iterate through this array and set the image of each image object to an image path that I have in a string array.
我正在尝试创建一个库存系统。当玩家按下Tab键时,会打开一个UI,其中包含三个图像。我将这三个图像从Hierarchy中拖到了一个Image数组中,并且我想遍历这个数组,将每个图像对象的图像设置为我在一个字符串数组中拥有的图像路径。
The images are not updating at all, and I don't know why.
The errors being spammed are strange and are thrown even when I comment out the call to updateimages(), so I'm not sure if they are relevant.
图像根本没有更新,我不知道为什么。
错误信息非常奇怪,即使我注释掉对updateimages()的调用,它们仍然被不断地抛出,所以我不确定它们是否相关。
the error being spammed:
不断抛出的错误:
// update images dynamically.
//array is a string array filled with image paths.
//objects is an array of images from the Hierarchy.
// 动态更新图像。
// array是一个填充有图像路径的字符串数组。
// objects是从Hierarchy中获取的图像数组。
private void updateimages()
{
for (int i = 0; i < 3; i++)
{
objects[i].GetComponent<Image>().sprite = Resources.Load<Sprite>(array[i]);
// not working, images are still blank.
}
/* Does not work either.
for (int i = 0; i < 3; i++)
{
Texture2D spriteTexture = LoadTexture(array[i]);
if (spriteTexture != null)
{
Sprite sprite = Sprite.Create(spriteTexture, new Rect(0, 0, spriteTexture.width, spriteTexture.height), Vector2.zero);
objects[i].sprite = sprite;
}
}
*/
}
private Dictionary<int, int> itemInventory; // Item ID to quantity mapping
private Dictionary<int, string> itemNameLookup; // Item ID to name mapping
private Dictionary<string, string> itemToImage; // Item Name to image path mapping
itemToImage = new Dictionary<string, string>();
itemToImage.Add("Health Pack", Application.dataPath + "/Player_Types/Item_Health.png");
itemToImage.Add("Poop", Application.dataPath + "/Player_Types/Item_Poop.png");
this block fills the array with image paths.
// iterate through inventory
foreach (KeyValuePair<int, int> entry in itemInventory)
{
// iterate through item names
foreach (KeyValuePair<int, string> entry_i in itemNameLookup)
{
// if inventoryitem.key == itemname.key
if (entry.Key == entry_i.Key)
{
//get image path from itemName.value into an array.
array.Append(itemToImage[entry_i.Value]);
}
}
}
这个代码块填充了包含图像路径的数组。
// 遍历库存
foreach (KeyValuePair<int, int> entry in itemInventory)
{
// 遍历物品名称
foreach (KeyValuePair<int, string> entry_i in itemNameLookup)
{
// 如果inventoryitem.key == itemname.key
if (entry.Key == entry_i.Key)
{
// 从itemName.value获取图像路径并放入数组中。
array.Append(itemToImage[entry_i.Value]);
}
}
}
<details>
<summary>英文:</summary>
I'm trying to make an inventory system. When the player presses tab, a UI opens with three images. I dragged all three images from the Hierarchy into an Image array, and I want to iterate through this array and set the image of each image object to an image path that I have in a string array.
The images are not updating at all, and I don't know why.
The errors being spammed are strange and are thrown even when I comment out the call to updateimages(), so I'm not sure if they are relevent.
the error being spammed:
> Edit: relaunching Unity got rid of the error being spammed. Images are still not being updated.
[![Hierarchy](https://i.stack.imgur.com/GXhHi.png)](https://i.stack.imgur.com/GXhHi.png)
[![error](https://i.stack.imgur.com/KR0sN.jpg)](https://i.stack.imgur.com/KR0sN.jpg)
```c#
// update images dynamically.
//array is a string array filled with image paths.
//objects is an array of images from the Hierarchy.
private void updateimages()
{
for (int i = 0; i < 3; i++)
{
objects[i].GetComponent<Image>().sprite = Resources.Load<Sprite>(array[i]);
// not working, images are still blank.
}
/* Does not work either.
for (int i = 0; i < 3; i++)
{
Texture2D spriteTexture = LoadTexture(array[i]);
if (spriteTexture != null)
{
Sprite sprite = Sprite.Create(spriteTexture, new Rect(0, 0, spriteTexture.width, spriteTexture.height), Vector2.zero);
objects[i].sprite = sprite;
}
}
*/
}
private Dictionary<int, int> itemInventory; // Item ID to quantity mapping
private Dictionary<int, string> itemNameLookup; // Item ID to name mapping
private Dictionary<string, string> itemToImage; // Item Name to image path mapping
itemToImage = new Dictionary<string, string>();
itemToImage.Add("Health Pack", Application.dataPath + "/Player_Types/Item_Health.png");
itemToImage.Add("Poop", Application.dataPath + "/Player_Types/Item_Poop.png");
this block fills the array with image paths.
// iterate through inventory
foreach (KeyValuePair<int, int> entry in itemInventory)
{
// iterate through item names
foreach (KeyValuePair<int, string> entry_i in itemNameLookup)
{
// if inventoryitem.key == itemname.key
if (entry.Key == entry_i.Key)
{
//get image path from itemName.value into an array.
array.Append(itemToImage[entry_i.Value]);
}
}
}
答案1
得分: 0
资源API中使用的路径是相对于项目中的资源文件夹的。移除路径中的Application.dataPath
部分,并确保路径相对于资源文件夹。
来自文档
例如,加载位于
"Assets/Guns/Resources/Shotgun.prefab"
的游戏对象只需要"Shotgun"
作为路径。
资源已经编译到您的项目中。
您无法向已编译构建的资源添加内容。
英文:
The path used in the resources api is relative to the Resources folder(s) in your project. Remove the Application.dataPath
portion of the paths and make sure the paths are relative to the Resources folder(s).
From Docs
>for example loading a GameObject at "Assets/Guns/Resources/Shotgun.prefab"
would only require "Shotgun"
as the path.
The Resources are compiled into your project.
You cannot add to the resources of a compiled build.
答案2
得分: 0
问题行:
objects[i].GetComponent<Image>().sprite = Resources.Load<Sprite>(array[i]);
解决方案:
Sprite tmp = Sprite.Create(ACTIVE_INVENTORY[i].texture, new Rect(0.0f, 0.0f, ACTIVE_INVENTORY[i].texture.width, ACTIVE_INVENTORY[i].texture.height), new Vector2(0.5f, 0.5f), 100.0f);
objects[i].sprite = tmp; // 将库存槽的图像设置为物品的图像
objectNames[i].text = ACTIVE_INVENTORY[i].itemName; // 将物品名称设置为物品名称。
英文:
My problem was trying to load textures into a sprite. I thought a sprite was just an image, so setting the sprite to a .png file I set to a texture in the engine made sense to me. I fixed it by creating a sprite with the texture, then setting the image object's sprite to the sprite I created.
Problem line:
objects[i].GetComponent<Image>().sprite = Resources.Load<Sprite>(array[i]);
soulution:
Sprite tmp = Sprite.Create(ACTIVE_INVENTORY[i].texture, new Rect(0.0f, 0.0f, ACTIVE_INVENTORY[i].texture.width, ACTIVE_INVENTORY[i].texture.height), new Vector2(0.5f, 0.5f), 100.0f);
objects[i].sprite = tmp; // set the inventory slot's image to the image of the item
objectNames[i].text = ACTIVE_INVENTORY[i].itemName; // set item name to item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论