- Home /
how to read/load sprite into array during run time c# unity
HI all
I am creating a card game (52).I have a single sprite in my Unity and Its having all playing card in a single image and I adjusted through sprite editor and it shows different values for each card.
I have 8 slots in the game area and I need to display any 8 cards into these slots using c#. In order to happen this , I have started with array and I find difficult to read from sprite and show each cards in slot.
Can any one guide me best prcatise and how to do ?
Answer by Reynarz · Oct 21, 2017 at 02:16 PM
Well, what you can do is create a folder called "Resources" (Assets/Resources) and there put all your sprites.. And in your code you can do something like:
mySpriteRenderer.Sprite = Resources.Load<Sprite>("MySpriteName");
Remember that all your sprites need to be inside the "Resources" folder, but if you create another folder inside it:
mySpriteRenderer.Sprite = Resources.Load<Sprite>("folderName/MySpriteName");
Now multiples sprites:
var sprites = Resources.LoadAll<Sprite>("folderName");
for(int i = 0; i < sprites.Length; i++)
{
myImagesArray[i].sprite = sprites[i];
}
Thanks for your reply, but I need to access each image in my large sprite. It has been named as card_0,card_1 card_2 up to card_51. These names I need to pass to front area from C# so that I have defined an array and I need to show this in layout group. I think layout group is good or if you have any other method, please suggest.
$$anonymous$$aybe i misunderstood you. an Image is a component and an sprite is a property of the image component. then you have multiples images and do you want to access to every sprite property?
Answer by E_minency · Oct 03, 2018 at 08:32 PM
What's myImagesArray? I'd like you to tell me more about it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Assigning a Sprite to a Script and creating a Gameobject out of that sprite 1 Answer
How to change the sprite and the animator in script to make a character selection? 2 Answers
Sprite Collision with Tilemap with 3D Colliders? And would just using 3D walls be as good or worse? 0 Answers