Converting a string name to a sprite
To Mods: -- I had to create two topics on this because the first one wasn't going live. Sorry about that.
Hi, I need to convert a string to a sprite.
I tried making an array of 99 sprites, but it causes the scene load time to be slow (this scene is loaded often)
I decided to instead, have an array of 99 strings, pull a string from the array and convert it to sprite.. But can't seem to find anywhere how to do this:
Pseudocode:
String imagename = "Thetestimage";
...
gameObject.Sprite = imagename;
instead of (which is the way I think most people will suggest doing this)
public Sprite spritename ;
...
gameObject.Sprite = spritename;
Thanks for you time.
This seems trivial, but I've searched google and the unity manual for the answer and just can't find it...
I tried this, but I get a bunch of AABB errors and stuff...
Sprite random_sprite = Resources.Load("Assets/_VisualAssets/Puzzles/Vehicles/Aircrafts/"+random_string+".png", typeof(Sprite)) as Sprite;
It also doesn't load the image :P
If you want to use function Resources.Load(), you have to put the files in a folder named "Resources".
Answer by ClearRoseOfWar · Jan 07, 2016 at 06:01 AM
If you want to use function Resources.Load(), you have to put the files in a folder named "Resources".
Thank you! That worked perfect :)
Sprite DaSprite = Resources.Load(StringUsed, typeof(Sprite)) as Sprite;//randomstring converted to sprite
Answer by toddisarockstar · Jan 07, 2016 at 05:44 AM
are you looking for an image on a hard drive ? on the internet? in your assets folder? peeps can answer here quickly if you give us this info.
Hello, I have the same problem too. I received the images from computer through socket, and I could not change the string to sprite too.
Your answer
Follow this Question
Related Questions
Any way to update a string array mid game?,Update the strings in a string array mid game? 1 Answer
Add Add Button UI to a turning page book (1 UI for page) 1 Answer
Remove empty "" strings from array String[] 1 Answer
Cycling through array not working. Pulling my hair over this one. 0 Answers
How to count down by mouse click? 1 Answer