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.
public GameObject invSprite;
public GameObject spriteParent;
public Vector3 LastSprite;
Vector3 previousSpritePos;
void Start()
{
previousSpritePos = LastSprite;
}
public void AddInvSprite(string name)
{
GameObject newSprite = Instantiate(invSprite,transform.position,Quaternion.identity);
newSprite.transform.SetParent(spriteParent.gameObject.transform,false);
newSprite.transform.localPosition = previousSpritePos - new Vector3(0f, 220f, 0f);
Sprite invImage = Resources.Load<Sprite>("UI/NikNakImages/" + name);
newSprite.GetComponent<Image>().sprite = invImage;
newSprite.GetComponent<Image>().SetNativeSize();
newSprite.transform.localScale = new Vector3(.2f, .2f, .2f);
previousSpritePos = newSprite.transform.localPosition;
}
The string grab from txt
public NikNak_Inventory nikNakInv;
void Start()
{
string path = "niknak.txt";
StreamReader reader = new StreamReader(path);
string fileContents = reader.ReadToEnd();
reader.Close();
var lines = fileContents.Split("\n"[0]);
foreach (string line in lines)
{
string[] splitArray = line.Split(char.Parse(","));
if (splitArray[2] == "true")
{
nikNakInv.AddInvSprite(splitArray[4]);
}
}
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.
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.
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.
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.
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.
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).
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.
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.)
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.