Design Pattern for card game objects

In my game, I will have more than 100 types of cards,
and before the actual game, player can select 6 of them and will be shown as 6 buttons on the scene.
The card have its own ID, card name, and a 3D game Object prefab, and have a function call perfromCardAction which performs different actions.

public class cardA:cardInterface {
     private int ID;
     private string cardName;
     private Texture2D cardImage;
     private GameObject card3DObject;
 
     ...Constructor...
     
     private override void prefromCardAction(){
     ...do Card A action...
     }
}

A script cardManager should get the 6 object player selected and stored in an array.
And set the button’s Image as card’s 2D image.
when the button is click, that card’s prefromCardAction should be called.

So my main quesstions are:
What pattern should I used so that easier manage my cards objects?
I believe I should use Factory so that I can do something like:

selectedCardArray[0]=CardFactory.createCard("CardA");

If yes, I would like to ask:
How should I code all my card class (cardA, cardB, cardC…)?
separate into different c# script and implement cardInterface and override the perfromCardAction?

Well, that’s up to you. A lot people always implement each class / interface / enum / … into a seperate file which has the name of the class. That has the big advantage that:

  • you can easily find a certain class even when you don’t have a “clever IDE” at hand.
  • It reduces the risk of loosing a huge part of your code due to a possible file corruption.

On the other hand writing several 7-liner scripts often seems a waste and usually is a pain to work with.

Apart from how you might organise the classes in your project, i strongly recommend to use a base-card class since it looks like all your classes share a huge part of the private variables. Make those protected so the concrete classes can access them. Your base class probably should be abstract as well as your “prefromCardAction” method.

As for your factory pattern, it would depend on how you actually want / need to access those classes. For such cases it’s very common to use reflection to gather all System.Types of subclasses of a baseclass and use the Activator to create an instance.

However your biggest problem is how you want to link up those references to the Unity objects (texture and gameobject)? If i got you right each of your 100 card types has it’s own class? So a class is never reused for a similar card?

In Unity it’s usually way easier to use prefabs. By creating prefabs for each card you can setup all data the class requires in one place. You can easily instantiate a certain card from the prefab. The prefab reference can be used like a normal class instance which just isn’t visible in the scene.

ps: In C# the common syntax is that method names and class / type names should be UpperCamelCase