0
votes

I am trying to implement a card (think trump card) in Unity 4.6 2D. I am generally working on a prefab Image, let's call the prefab Card.

Card has the Image script (of course), with the Source Image set to the backside of a card. This would be the same for all cards. How do I implement the frontside, with a unique texture/image/sprite for each type of card (five of hearts, king of spades etc)?

I currently do the flipping using animations. The default state of the card is face down. The "flip up" animation does this:

  • Halfways through the anim, set rotate Y axis to 90 degrees.
  • Set a new Source Image - for now this is "hard coded" to a "hearts_5.png", i.e. five of hearts.
  • Set scale X to -1. (the keyframe would need to be edited to turn off the tween that would occur over time from start of animation!)
  • As a last keyframe in the anim, Rotate Y axis again, to 180 degrees.

Because I rotate the card to 180 degrees, the Source Image would be shown mirrored, that's why I need to set the scale X to -1 in order to mirror the texture back.

It works pretty well, I just now need to find out how to (from C# code) set another Sprite for the Card(GameObject)>component(Image)>sprite variable depending on the actual card (Spades ten etc). How do I do that?

If there's any better way to accomplish this, go ahead.

1

1 Answers

0
votes
public List<Sprite> sprites;
    private int m_index;

    public SpriteRenderer m_renderer;

    void Start() {
        m_index = 0;
        m_renderer = GetComponent<SpriteRenderer>();
    }
    public void SwitchSprite(somecardtype) {
        m_renderer.sprite = sprites[(int)somecardtype];

    }

or you can use that private m_index variable to index the array of sprites that will hold all of your card sprites. Does this help or do I need to elaborate more?