- Home /
Making a card
I am making a card-based game similar to Triple Triad. The game is played in top-down 2D over a 3x3 grid where players place their cards onto one of the open grid positions. Therefore I need to make a card which is 2D, holding a picture on its face and a different picture on its back so that when animating a card to flip it would look like an actual card.
The game will have about 30 cards with different pictures and other things (each card has 4 values in the north, east, south and west positions on the face of the card).
How would I go about making these cards to use in my game? I have read something about prefabs but just asking in case there is another option or perhaps a better example for my case?
Answer by robertbu · Feb 11, 2013 at 05:24 PM
Create a single card prefab. The prefab would be composed of a background with a blank front, a plane to display the face of the card on and a script. The script would contain some public variables or methods to set the value for the face of the card. In the script there would be an array of textures something like:
public Texture[] artex; // C#
var artex : Texture[]; // Unityscript
All your face textures for all your cards would go into this array. Then at runtime, you would Instantiate() the deck of 30 cards (or only the 9 you need) and assign them values. The card would then set the texture for the face of the card according to the value you assigned to the card. Something like:
renderer.material.mainTexture = artex[i];
If your target platform is mobile, I'd test performance of this (or any) solution early. If it is too slow, you can use a texture atlas instead of individual textures for the face of the cards. Add-on packages like EZGUI and I assume NGUI make this fairly easy.
When you say 'the prefab would be composed of a background with a blank front', how do I go about making that in Unity? Am I right in thinking you mean create a Cube and then put a plane in front of the cube?
A cube and a plane would work, though unless you UV map it, the edges and the blank face will get a copy of the back. If you don't care about thickness, two planes back to back would work. One would be a child of the other. FYI, the build-in planes are one sided.
And if you use two built-in planes, I'd parent both to an empty game object. Planes are horizontal by default, so later if you want to do some form of script-based animation, you have to deal with the rotation. Parenting to an empty game object so the cards are in their play position when the empty game object as rotation of (0,0,0) will simplify things. You might also take a look at the CreatePlane editor script. It will make vertical planes and use less triangles (probably not an issue for your game).
I have created a 'Card' prefab which is two planes. I have made a script called 'Card' also which is added to the prefab and contains:
4 int's for the four values (N,E,S,W, e.g. 1, 7, 2, 3). 1 string for name of card and a constructor method public Card to set the values
So, because my cards all have different values on them (e.g. one could have 1, 7, 2, 3 and another could have 5, 9, 1, 1) how would I go about creating the cards, would I just make them one at a time using the constructor? $$anonymous$$g. creating new cards when user clicks New Game:
Card card1 = new Card(name, topValue etc)
If this is good enough how would I then set the face of each card, could I send a texture parameter to the constructor and then do something like call the following method inside it:
void ApplyTexture(Texture t) { renderer.material.mainTexture = t;
}
Would that set the individual card to the texture specified?
First off, if the script is attached to a game object, it should not have a constructor. Initialization should be done in Start().
How you create cards will partly depend on the game. I don't understand the values assigned to the cards. How many unique cards can exist in a game? In a traditional deck there are four suits, and 13 value cards. But if you use for integers (even assu$$anonymous$$g you confine them to (0 - 9), you will have 10 10 10 * 10 possible cards, but you indicate in your original question that you will only have 30 total cards.
And how many cards can show at one time? Will there only be 9 cards showing?
And is this game for mobile, web, or desktop?
Your answer
Follow this Question
Related Questions
Multiple timers on server as objects or prefabs? 0 Answers
Instantied 2D Prefab Is Invisible 1 Answer
How to instantiate a gameobject by aligning it to another one 2 Answers
Infinity runner - best way to spawn board in runtime. 0 Answers
Avoid getting same object as previous one with Random.Range? 2 Answers