简体   繁体   English

无法动态设置图像 object 的图像

[英]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.当玩家按下 Tab 键时,会打开一个包含三张图片的 UI。 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.我将层次结构中的所有三个图像拖到一个图像数组中,我想遍历此数组并将每个图像 object 的图像设置为我在字符串数组中的图像路径。

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.被垃圾邮件发送的错误很奇怪,即使我注释掉对 updateimages() 的调用也会被抛出,所以我不确定它们是否相关。

the error being spammed:垃圾邮件错误:

Edit: relaunching Unity got rid of the error being spammed.编辑:重新启动 Unity 摆脱了被垃圾邮件发送的错误。 Images are still not being updated.图片仍未更新。

等级制度 错误

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

The path used in the resources api is relative to the Resources folder(s) in your project.资源 api 中使用的路径是相对于项目中的 Resources 文件夹的。 Remove the Application.dataPath portion of the paths and make sure the paths are relative to the Resources folder(s).删除路径的Application.dataPath部分并确保路径是相对于 Resources 文件夹的。

From Docs来自文档

for example loading a GameObject at "Assets/Guns/Resources/Shotgun.prefab" would only require "Shotgun" as the path.例如,在"Assets/Guns/Resources/Shotgun.prefab"加载一个游戏对象只需要"Shotgun"作为路径。

The Resources are compiled into your project.资源被编译到您的项目中。
You cannot add to the resources of a compiled build.您不能添加到已编译构建的资源中。

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.我认为精灵只是一个图像,所以将精灵设置为 .png 文件我在引擎中设置为纹理对我来说很有意义。 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM