Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Right runtime resource load (sprite/image)

Discussion in 'Scripting' started by zurisar, Nov 6, 2019.

  1. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Hi again folks! It's me again and another stupid question about runtime resource loading.

    I try to code some ingame shop, I don't use any item manager system or scriptable objects for now. Few items just hardcoded in script.

    So, for example I has that item:
    Code (CSharp):
    1.     public virtual void SelectItem(int item)
    2.     {
    3.         switch (item)
    4.         {
    5.             case 0: // One chiken leg
    6.                 selectedShopItem = 0;
    7.                 itemPrice = 3;
    8.                 itemName = "One chicken leg";
    9.                 itemDesc = "Restore 1 hunger";
    10.                 itemIcon = Resources.Load<Sprite>("Items/food_meat_1");
    11.                 itemRestoreHP = 1.0f;
    12.                 itemRestoreMP = 0;
    13.                 UIManager.Instance.LoadSelectedItem(itemName, itemDesc, itemPrice, itemIcon);
    14.                 selectedItemPanel.SetActive(true);
    15.                 break;
    My UIManager has next function LoadSelectedItem()
    Code (CSharp):
    1.     public void LoadSelectedItem(string itemName, string itemDesc, int itemPrice, Sprite itemIcon)
    2.     {
    3.         shopSelectedItemNameText.text = itemName;
    4.         shopSelectedItemDescText.text = itemDesc;
    5.         shopSelectedItemIcon.sprite = itemIcon;
    6.         shopSelectedItemPrice.text = itemPrice.ToString();
    7.     }
    So I just drug and drop needed UI elements in Inspector to variables at my Canvas, UIManager also attached to Canvas, that section clear for me. Also like selectedItemPanel attached to my shop keeper object.

    I has next folders structure:
    - Assets
    -- Resources
    --- Items
    ---- food_meat (spritesheet), TextureType - Sprite 2D and UI

    As you can see I want to load sprite image to my shop interface, logically when player click to item, selectedItemPanel enabled and player can see what he pick and read some info. But for now I get only white square.

    I think the answer is near and I just miss something in paths or Sprite variable or some way to load single sprite from spriteshit. I read forums and question on stackoverflow but can't find answer what I do wrong. So help me a bit, please.
     
  2. zurisar

    zurisar

    Joined:
    Nov 4, 2019
    Posts:
    24
    Hey guys, 1 hour searching more and I got it. Because I has spritesheet and not single sprite I need load it to array.

    Code (CSharp):
    1.  public virtual void SelectItem(int item)
    2.     {
    3.         Sprite[] itemIconSheet = Resources.LoadAll<Sprite>("Items/food_meat"); // Use original file name here
    4.  
    5.         switch (item)
    6.         {
    7.             case 0: // One chiken leg
    8.                 selectedShopItem = 0;
    9.                 itemPrice = 3;
    10.                 itemName = "One chicken leg";
    11.                 itemDesc = "Restore 0.5 hunger";
    12.                 itemIcon = itemIconSheet[1]; // Use array index to pick needed image
    13.                 itemRestoreHP = 0.5f;
    14.                 itemRestoreMP = 0;
    15.                 UIManager.Instance.LoadSelectedItem(itemName, itemDesc, itemPrice, itemIcon);
    16.                 selectedItemPanel.SetActive(true);
    17.                 break;
    That's all, and it work.