- Home /
Loading in Sprites outside of unity?
I need to load a .png file at a specified location on the computer. In this case im making a way to mod the game i am making and i need to read a string from a file(which works) and find the file at the location and load it to a variable. I was using: tempGolem.icon = Resources.LoadAssetAtPath(Vars.modsPath + "/" + golems[a]["icon"]) as Sprite;
but it doesn't seem to find anything. I know everything is spelled right, but when i try to use that sprite its just a white box. Any help?
Answer by Dave-Carlile · Jun 21, 2015 at 02:05 AM
According to the documentation, Resources.LoadAssetAtPath is deprecated and shouldn't be used. It was replaced with AssetDatabase.LoadAssetAtPath. However, I'm not sure you'll be able to use that for modding since it's only available in the Unity editor. It can't load arbitrary files off the disk. So I guess it depends on exactly what you're doing.
That said, you can load a .PNG from anywhere in the file system into a texture using something like this...
using System.IO;
...
byte[] data = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(64, 64, TextureFormat.ARGB32, false);
texture.LoadImage(data);
texture.name = Path.GetFileNameWithoutExtension(path);
...
Then create a Sprite
instance using that texture and Sprite.Create.
This likely won't work in the web player, but if you're supporting modding I assume that isn't an issue for you.
Thank you but now will that work for a specific .png file or will it load all of them at the specified location? Because i have the user input the location of a specific .png file.
EDIT: I likely wont be using the webplayer unless i want to make a demo of the game in which case ill be removing modding anyway.
EIDT2: and i tried it and i get an access denied exception... but i can read text files from teh same location. Any idea why?
EDIT3: If it helps this is where im trying to get the file from: C:\Users\USERNA$$anonymous$$E\AppData\LocalLow\AcornRifle\Golemancy\$$anonymous$$ods And i am cosidered an ad$$anonymous$$ on that computer since i think that matters aswell.
Alright i managed to get it to not throw an error but when i get the sprite an assign it to the sprite of an image component its just blank.
$$anonymous$$y sample code is only going to load a single image at a time. You would need to identify all of the files you want to load and load them one at a time.
I know that, and i have it in a for loop that hsould do what i need but its still just blank.
for(int a = 0; a < golems.Count; a++) {
Golem tempGolem = new Golem();
tempGolem.golemName = golems[a]["name"];
tempGolem.tier = golems[a]["tier"].AsInt;
byte[] data = File.ReadAllBytes(Vars.modsPath + "/$$anonymous$$ainContent/icon.png");
Texture2D texture = new Texture2D(128, 128, TextureFormat.ARGB32, false);
texture.LoadImage(data);
texture.name = Path.GetFileName(Vars.modsPath + "/$$anonymous$$ainContent/icon.png");
tempGolem.icon = Sprite.Create(texture, new Rect(0.0f, 0.0f, 128.0f, 128.0f), new Vector2(0.5f, 0.5f), 1000);
$$anonymous$$od.golems.Add(tempGolem);
}
Some of that code isnt relevant but i'll keep it in there just in case. i wasnt exactly sure what the Rect was for so i just set it to the size of the image.
and this is how i assign it
currentButton.GetComponentInChildren<Image> ().sprite = $$anonymous$$od.golems[i].icon;
EDIT: and as i posted this i checked again in it suddenly works... I quess i forgot to save one of the scripts. Thanks for your help.
You're welcome. Don't forget to click the check mark under the voting buttons.
Your answer
Follow this Question
Related Questions
Resources.Load renaming/moving resources. 0 Answers
Get path to loaded resource. 1 Answer
Subfolders with Resources.Load not working 2 Answers
Resources.Load dynamic path 2 Answers
Resources.Load folders path? 2 Answers