Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Resources.Load not grabbing images from folder

Discussion in 'Scripting' started by Swaparson, Jan 25, 2022.

  1. Swaparson

    Swaparson

    Joined:
    Dec 20, 2019
    Posts:
    5
    Hi,

    I've been looking around for a while and haven't encountered any solution to this strange problem I have been facing.

    I have been taking a string from a text file, then applying that string to an instantiated UI image using resources.load. Previously it worked perfectly(last Friday) but after opening it on Monday it no longer calls the images. After using debug.log I can see that it's still giving the correct address but alas still not pulling the images.

    Code (CSharp):
    1.     public GameObject invSprite;
    2.     public GameObject spriteParent;
    3.     public Vector3 LastSprite;
    4.  
    5.     Vector3 previousSpritePos;
    6.  
    7.     void Start()
    8.     {
    9.         previousSpritePos = LastSprite;
    10.     }
    11.  
    12.     public void AddInvSprite(string name)
    13.     {
    14.         GameObject newSprite = Instantiate(invSprite,transform.position,Quaternion.identity);
    15.         newSprite.transform.SetParent(spriteParent.gameObject.transform,false);
    16.         newSprite.transform.localPosition = previousSpritePos - new Vector3(0f, 220f, 0f);
    17.         Sprite invImage = Resources.Load<Sprite>("UI/NikNakImages/" + name);
    18.         newSprite.GetComponent<Image>().sprite = invImage;
    19.         newSprite.GetComponent<Image>().SetNativeSize();
    20.         newSprite.transform.localScale = new Vector3(.2f, .2f, .2f);
    21.         previousSpritePos = newSprite.transform.localPosition;
    22.     }
    The string grab from txt

    Code (CSharp):
    1.     public NikNak_Inventory nikNakInv;
    2.     void Start()
    3.     {
    4.         string path = "niknak.txt";
    5.         StreamReader reader = new StreamReader(path);
    6.         string fileContents = reader.ReadToEnd();
    7.         reader.Close();
    8.         var lines = fileContents.Split("\n"[0]);
    9.         foreach (string line in lines)
    10.         {
    11.             string[] splitArray = line.Split(char.Parse(","));
    12.             if (splitArray[2] == "true")
    13.             {
    14.                 nikNakInv.AddInvSprite(splitArray[4]);
    15.             }
    16.         }
    After testing the resource.load by inputting the straight name it works, it just won't work with the string that the txt file is offering anymore.

    Is there something really obvious I'm missing?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It's hard for us to conjecture without seeing the text file contents and your project's structure. Put in some logging statements inside your AddInvSprite method and confirm that name is what you're assuming it is.
     
    Swaparson likes this.
  3. Swaparson

    Swaparson

    Joined:
    Dec 20, 2019
    Posts:
    5
    The text file contains several lines structured like this "Coral Reef - Amazon River,20,false,true,IMG_0677" the final section being the file name in the folder.

    The directory that the script is trying to pull from is exactly right when I use a debug log. It also works if I replace the pulled name with a string that I typed in manually.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,039
    Since the name is at the end of the line, my first suspicion is extra invisible characters at the end of IMG_0677 making it fail. Print it out with Debug.Log() but do so with great suspicion bu adding immediately adjacent quote marks before and after it to reveal if there's a gap or newline.
     
    Swaparson likes this.
  5. Swaparson

    Swaparson

    Joined:
    Dec 20, 2019
    Posts:
    5
    Using a debug.log it doesn't show any spaces through the call. I even checked for hidden spaces at the end to which there were none.
    upload_2022-1-25_15-18-27.png
    upload_2022-1-25_15-19-43.png
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,039
    So your folder structure looks like

    Resources/UI/NikNakImages/


    Correct?

    Also, I noticed you're loading a Sprite, not a Texture2D.

    This means that
    IMG_0672
    and
    IMG_0672test
    must be the correct internal name of the Sprite that was cut out of the Texture.

    This is the case only if it was a single sprite, whereas multiple sprites can be named whatever but default to _0, _1, _2 suffixes to the image name.

    Also just as one more note of style, the identifier
    name
    is part of the basic UnityEngine.Object, the root of all Unity stuff. You can legally reuse it as your identifier but it can be an legitimate source of future confusion. I never reuse such identifiers if I can avoid it.
     
    Swaparson and Bunny83 like this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,114
    Yep that would be my first and only question ^^. Resources.Load can only load assets from a Resources folder.

    Note that Resources.Load does not really load "files" but "assets" from the asset database that have been placed in a Resources folder at edit time. It can not load actual files which are outside a Resources folder (since those assets are not shipped with the game unless directly referenced somewhere) or that you ship manually alongside your build. For manually loading external image files you would need to use the normal file io routines (System.IO.File.xxx) and then use LoadImage on a Texture2D object. Note that Unity only supports loading jpg or png images at runtime. The editor can import all sorts of images, but at runtime only those types are supported (at the moment).
     
    Swaparson likes this.
  8. Swaparson

    Swaparson

    Joined:
    Dec 20, 2019
    Posts:
    5
    yes the file format is exactly Resources/UI/NikNakImages/

    I have also checked and the images are named exactly IMG_0672 due to it being a single sprite. Finally, I have also changed the variable from name to nikName to avoid those issues you stated but it still doesn't want to work.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,039
    Then start removing lines. I've never used that SetNativeSize call in code. Is that the problem? It may also take into account scale, which you set on the next line. Reverse them, remove them, rip-tear-shred, try stuff again and again, combinations, fast, rapid, find out what is going on quickly, press pause and study the editor...look for anchors, RectTransform bounds, pivot points, drag in the sprite manually AFTER the editor is running, find out what's happening!

    If you try one thing at a time and come back to the forum post it's gonna be a really long slog. :)

    (All this obviously assumes you're using professional source control so you can instantly and trivially revert.)
     
    Bunny83 and Swaparson like this.
  10. Swaparson

    Swaparson

    Joined:
    Dec 20, 2019
    Posts:
    5
    I have now switched to using system.io to call for the image and it now works perfectly TY!!

    I also used this super helpful tutorial as well


    Thanks to everyone who helped out!
     
    MaxODidilyUniAcc likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,039
    Excellent. Just beware this will not work in a build, only in the editor.

    In an actual build all folders are removed and assets are compressed combined into the asset database.
     
    Bunny83 likes this.
  12. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,114
    :) Gotta love your extreme black and white view points ^^. Well if it works in a build or not highly depends on the target platform and on the path and how the files get there. As I said above when "loading external image files" you could use usual file io routines. Though I said you have to ship those files manually.

    This does work for standalone builds just fine. On different target platforms you usually have to use different approaches. A WebGL build may load other resources with a UnityWebRequest from your server. For an Android build (and any other platform), the streaming assets folder may be the best solution. Though, especially for Android you have to use a UnityWebRequest to load those files.

    Yes, I cound have been more clear about the fact, that files outside the Resources folder are not shipped with the game as actual files. A Unity project is just an authoring environment. The "Assets" folder does not exist in a build game.
     
    GroZZleR likes this.