- Home /
How to load (and display) a texture png from my Resources folder
I've been following various pieces of advice but nothing has seemed to solve this problem. I'd like to set up a sequence of planets, each based on a Planet prefab. I've got the planets instantiating and I can customize their size and so on, but I need to specify which surface textures (atmosphere, etc) each will use.
I've been using...
_textures.Add(Resources.Load("/Textures/img_surface1.png" ) as Texture);
Debug.Log("_textures[0] = "+_textures[0]);
...where _textures is a List, but _textures[0] is logged as nothing (it doesn't say 'null', the console logs only "_textures[0] = ").
Later, to add the texture to the planet, I do....
GetComponent<Renderer>().material.SetTexture("_MainTex", _textures[0]);
..but the planets just display all white.
I'd like to know what I'm doing wrong. If it's because I'm loading a png, what's the alternative? I've tried using a jpg. Am I supposed to be loading an actual Unity Texture type? If so, how would I get one external to Unity to begin with? I've tried all manner of URLs; with the slash, without; with 'Resources' at the start, without; both with a slash and without.
My Resources folder is in the Assets folder of my project and it has a Textures folder where the img_surface1.png image (amongst others) lives.
Is there anything obviously wrong with what I'm doing?
Any help appreciated. Cheers.
@moosefetcher hey Do you have any idea what is wrong with my code: Renderer rend = GetComponent();
Texture textureFace = Resources.Load("face2") as Texture;
rend.material.mainTexture = textureFace;
the game isn't responding to this
Answer by fffMalzbier · Nov 13, 2015 at 11:04 AM
A friendly quote from the documentation
The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
http://docs.unity3d.com/ScriptReference/Resources.Load.html
Change the path from
"/Textures/img_surface1.png"
to
"/Textures/img_surface1"
Thanks for your answer. I should have said I also tried it without the file extension. The Instantiated planets were still displayed white. I'm pretty sure it must be the loading that's not working though(Surely Logging _texture[0] would show something, right?). No clue what's wrong. If there's anything else about my code that looks wrong, please let me know.
Answer by el_kloklo · Jan 06, 2017 at 09:54 AM
I had the same problem, and found solution in older question, you should not have the root "slash" and, no extension. Change the path from "/Textures/img_surface1.png" -> "Textures/img_surface1"
Answer by saschandroid · Nov 13, 2015 at 11:04 AM
From unity script reference: "The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted." So: no ".png" at the and :)
Hi there. Thanks for the answer. I should have said that I had tried it without the extension too. Is there anything else the code needs to be doing? Does the texture load instantaneously or do I need to listen for a 'loaded' event maybe? Really not sure what's up. If you have any ideas, I'd be keen to hear them.
Answer by TouxStudio · Mar 15, 2019 at 11:31 PM
Hi all, exists some way to avoid write exter al permission to load a png img from Resources?
Greetens