Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by xopods · Jul 18, 2014 at 03:44 AM · spriteruntimesprite renderer

Setting a 2D sprite to a specific frame at runtime

I'm making a simple card game. It doesn't use a standard deck, but that doesn't matter to the question... pretend I am, so it's easier to get what I'm talking about.

Rather than making 52 separate card images, it makes sense to put them all in a single atlas, like what you'd do for a spritesheet, then chop them up within Unity using the Sprite Editor, and name the frames e.g. 2c, 3c, 4c... Kc, Ac, 2d, 3d, 4d... etc.

My question is then simply how I set the "frame" to the appropriate card at runtime. I.e. I instantiate a card from the prefab and then want a function in my script that I can call like:

SetCard("Jc");

To change the card's texture to the right one for the Jack of Clubs. Surely this is possible and easy... I just don't know what function to call, or even what component I need to call it on. Obviously, I could just set up a 52-state animation controller with 52 different one-frame animations, but that is a lot of tedious work and also obviously a stupid kludge.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 18, 2014 at 03:48 AM 0
Share

Various forms of this question were posted back in $$anonymous$$arch/April/$$anonymous$$ay, and no solutions were offered at the time. I know there have been some improvements/additions to the 2D stuff, so maybe there is a solution today. If not, you can put your cards in a sprite sheet (an atlas where all the individual images are on a regular grid) in your image editor. It is then relatively easy to modify the uvs of a Unity Quad to display the right card. Depending on the resolution, you may want a separate sprite sheet for each suit.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by yagizpoyraz · Jul 18, 2014 at 04:12 AM

Hi,

You should have a card prefab. It may basically consist of a sprite renderer. You should create a cardScript and add the script to spriterenderer as a component. You do it by dragging the script on to sprite renderers components.

So when you create a card from prefab, you can reach your script by getcomponent And Simply call e method that sets spriterenderers sprite.

In your script the method could be something like this:

 void SetImage(string imageName)
 {
 gameObject.GetComponent<SpriteRenderer>().sprite =  Resources.Load(imagename, typeof(Sprite)) as Sprite;
 }

I wrote this answer from my cell phone, hope it helped. If it doesnt works please write to me, i will check again.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by xopods · Jul 18, 2014 at 03:24 PM

No, this doesn't seem to work. If I do:

 myRenderer.sprite = Resources.Load("cards",Sprite) as Sprite;

That works, and applies the image of the first card in the deck ("cards" is the spritesheet as a whole). However, trying to set a specific card like:

 myRenderer.sprite = Resources.Load("Jd",Sprite) as Sprite;

Does nothing. Doesn't even throw an error. Also tried the following, neither of which worked.

 myRenderer.sprite = Resources.Load("cards/Jd",Sprite) as Sprite;
 myRenderer.sprite = Resources.Load("cards.Jd",Sprite) as Sprite;

I guess I'll just have to do it the stupid way and either bring the cards in as 52 (well, 28 for my custom deck) separate graphic resources and make a single object that loads the appropriate one.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by xopods · Jul 18, 2014 at 03:24 PM

Aha! Figured it out. The answer is Resources.LoadAll instead of Resources.Load and store them in an array, then set them by index:

 var sprites : Array;
 var rndr: SpriteRenderer;
 
 function Start () {
     sprites = Resources.LoadAll("cards",Sprite);
     rndr = gameObject.AddComponent("SpriteRenderer");
     SetCard(1,11);
 }
 
 function SetCard(suit:int,rank:int) {
    rndr.sprite = sprites[suit*13+rank];
 }

That would give you the King of Diamonds, for instance (assuming you have the cards laid out as Clubs, Diamonds, Hearts, Spades, and with the Ace at the end of the row after the King and not at the start).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Best way to handle many versions of same GameObject? 1 Answer

How to change a sprite collider at runtime 2 Answers

How do importing image when game is running (Run Time) ???? 0 Answers

Can I get a sprite from a sprite sheet? 1 Answer

Best way to sacale entire object in runtime?? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges