Creating a memory game in Unity - #3 Generating Cards

00:10:06
https://www.youtube.com/watch?v=4bE618eYtQ4

Summary

TLDRThe video tutorial guides viewers through the process of creating a card prefab and generating a grid layout for a card game. It covers the creation of a parent image for the card, the addition of front and back images, and the implementation of scripts to manage card behavior and canvas settings. The tutorial emphasizes the importance of difficulty settings and how they influence the game. By the end of the video, viewers learn how to instantiate cards in a grid layout based on specified parameters.

Takeaways

  • 🃏 Create a card prefab for the game.
  • 📐 Use a vertical layout group for card arrangement.
  • 🔄 Implement a card controller for flipping cards.
  • 🎨 Add a canvas manager to handle layout and card generation.
  • ⚙️ Store difficulty settings as scriptable objects.
  • 🔢 Generate a specified number of cards based on difficulty.
  • 📊 Use a list to manage card controllers.
  • 🛠️ Instantiate cards in a grid layout.
  • 🔍 Debugging tips for card instantiation.
  • 🎮 Prepare for randomization of card types in future videos.

Timeline

  • 00:00:00 - 00:05:00

    In this segment, the speaker introduces the process of creating a card prefab for a game. They outline the structure of the card, which includes a main image as a border and two child images representing the front and back faces of the card. The speaker discusses adding a vertical layout group to manage spacing and padding, and mentions the creation of a card controller script to handle card flipping mechanics. Additionally, a canvas manager script is introduced to manage the canvas size and generate the required number of cards based on difficulty settings.

  • 00:05:00 - 00:10:06

    The second segment focuses on implementing the canvas manager script to set up the card grid layout. The speaker explains how to retrieve game data and difficulty settings, and how to instantiate the required number of card prefabs in a grid format. They emphasize the importance of organizing the card instances and managing their properties through a list of card controllers. The segment concludes with a demonstration of the generated card grid, showcasing a 4x5 layout based on normal difficulty, and hints at future work on card types and randomization.

Mind Map

Video Q&A

  • What is the purpose of the card prefab?

    The card prefab serves as a template for creating individual cards in the game.

  • How does the card controller script function?

    The card controller script manages the card's behavior, including flipping between the front and back faces.

  • What does the canvas manager script do?

    The canvas manager script handles the canvas size, generates the required number of cards, and assigns random card types.

  • How are difficulty settings implemented?

    Difficulty settings are stored as scriptable objects and can be chosen from the menu.

  • What layout is used for the card grid?

    A vertical layout group is used to arrange the cards in a grid format.

View more video summaries

Get instant access to free YouTube video summaries powered by AI!
Subtitles
en
Auto Scroll:
  • 00:00:00
    hi devs i'm cray and today we are going
  • 00:00:02
    to create our card and generate the grid
  • 00:00:06
    layout so the right amount of cards
  • 00:00:09
    let's start with creating the folder for
  • 00:00:11
    our prefabs
  • 00:00:16
    create the card prefab from one of the
  • 00:00:19
    existing cards
  • 00:00:22
    the main image will be used as a border
  • 00:00:25
    it will be a parent of two more images
  • 00:00:27
    one for the back face and one for the
  • 00:00:30
    front face of the card with the script
  • 00:00:32
    we will switch between those two cards
  • 00:00:35
    visibility we will add the vertical
  • 00:00:37
    layout group to the parent image and
  • 00:00:40
    create some padding and spacing to make
  • 00:00:42
    the parent work as a border
  • 00:01:02
    and add some color to see the child
  • 00:01:05
    images
  • 00:01:06
    and let's set the visibility one of them
  • 00:01:09
    to none
  • 00:01:14
    and let's drag this to the prefab folder
  • 00:01:21
    and after that let's create a card
  • 00:01:23
    controller script this will help us to
  • 00:01:26
    manage the card impulse and it will be
  • 00:01:28
    responsible for the flipping mechanism
  • 00:01:30
    later we add it to the card prefab and
  • 00:01:33
    write the functionality later
  • 00:01:37
    create canvas manager script it will be
  • 00:01:40
    handling the size of our canvas
  • 00:01:42
    generating the needed amount of cards
  • 00:01:44
    and set random card type to each of them
  • 00:01:51
    it needs the card prefab
  • 00:01:56
    this will be
  • 00:01:57
    generated
  • 00:01:59
    this type of game object will be
  • 00:02:00
    generated and we need the
  • 00:02:04
    card collection with all
  • 00:02:06
    the
  • 00:02:08
    cards available for this game
  • 00:02:11
    and we need the difficulty settings
  • 00:02:14
    as the
  • 00:02:16
    scriptable objects we created in the
  • 00:02:18
    last video and the card collection as
  • 00:02:20
    well
  • 00:02:21
    was created in the last video
  • 00:02:24
    we need the easy the normal and the hard
  • 00:02:27
    data
  • 00:02:31
    this difficulty will be choosable from
  • 00:02:34
    the menu later
  • 00:02:41
    and we need to store
  • 00:02:43
    this kind of data so we need the local
  • 00:02:46
    variable called game data
  • 00:02:53
    in the wake function we should create
  • 00:02:57
    three functions
  • 00:02:59
    one will be responsible for choosing the
  • 00:03:02
    right game that i saw the other
  • 00:03:06
    will be responsible to set the grid
  • 00:03:09
    layout data to the grid card grid layout
  • 00:03:12
    and the third one will be responsible
  • 00:03:14
    for generating the right amount of cards
  • 00:03:18
    so in the first the get game details we
  • 00:03:20
    will create a switch case with all the
  • 00:03:24
    difficulty params
  • 00:03:26
    and we will be store the difficulty
  • 00:03:28
    settings
  • 00:03:29
    choosed in the menu
  • 00:03:31
    in the player prefabs
  • 00:03:33
    and it will be stored as an int so we
  • 00:03:36
    can read it out
  • 00:03:39
    and of course because we not have any
  • 00:03:43
    kind of money yet we can set a default
  • 00:03:46
    value you can use it
  • 00:03:49
    as you set the second parameter to the
  • 00:03:51
    getint function so it it will try to get
  • 00:03:54
    an end called difficulty in the player
  • 00:03:57
    prefabs and if it's do not get it it
  • 00:04:00
    will set it to normal difficulty and
  • 00:04:03
    after that
  • 00:04:04
    the newly created game that is
  • 00:04:07
    variable will be set to is it normal or
  • 00:04:10
    hard based on the value readout from the
  • 00:04:15
    player prefabs
  • 00:04:26
    and after that the second function will
  • 00:04:28
    be
  • 00:04:29
    to set it to the cartridge layout
  • 00:04:33
    in that function we will
  • 00:04:36
    read the perms from from the gamedata's
  • 00:04:40
    scriptable object
  • 00:04:42
    and set it to the cartridge layout
  • 00:04:45
    the plan is that we will add this canvas
  • 00:04:49
    manager to the same game object that
  • 00:04:52
    stores the
  • 00:04:55
    card grid layout
  • 00:04:57
    so
  • 00:04:59
    first we can use find object of type it
  • 00:05:02
    will find that type of object
  • 00:05:05
    if it's in any other
  • 00:05:07
    game object but it's a little bit better
  • 00:05:10
    solution to store it in the same
  • 00:05:13
    so the grid panel will have the card
  • 00:05:16
    grid layout
  • 00:05:17
    script and the canvas manager script
  • 00:05:20
    in that case
  • 00:05:22
    we can
  • 00:05:27
    and of course we set all the
  • 00:05:30
    params
  • 00:05:31
    to this canvas manager
  • 00:05:33
    the
  • 00:05:34
    game letters
  • 00:05:36
    and the collection
  • 00:05:41
    and the newly created card prefab as
  • 00:05:44
    well
  • 00:05:57
    and with that
  • 00:05:59
    solution
  • 00:06:00
    in the canvas manager we can use the
  • 00:06:05
    get component
  • 00:06:07
    because it's on the same game object
  • 00:06:11
    and after that
  • 00:06:13
    we set all the parameters
  • 00:06:16
    from the game letters to the card layout
  • 00:06:21
    so the preferred padding which will be
  • 00:06:23
    responsible from the padding around the
  • 00:06:27
    grid layout
  • 00:06:30
    the
  • 00:06:31
    rows and columns
  • 00:06:33
    which will be responsible for how many
  • 00:06:36
    cards do we have
  • 00:06:43
    and lastly
  • 00:06:44
    we have the spacing which will be
  • 00:06:47
    responsible for the spacing between the
  • 00:06:49
    cards
  • 00:06:51
    so that's all we need to set
  • 00:06:55
    with all these data datas we can now
  • 00:06:58
    generate the
  • 00:07:00
    cards
  • 00:07:02
    now we can't do anything in the editor
  • 00:07:06
    or at least we can press play but won't
  • 00:07:08
    see anything because we have this
  • 00:07:11
    already added images so
  • 00:07:14
    we should create the our third function
  • 00:07:17
    which is the generate cards
  • 00:07:20
    it will be
  • 00:07:22
    creating all the needed amount of card
  • 00:07:25
    prefabs created in the start of the
  • 00:07:28
    video so we need a card count variable
  • 00:07:32
    in which we store the needed card amount
  • 00:07:36
    based on the rows and columns stored in
  • 00:07:39
    the game data
  • 00:07:41
    and with that number we can easily
  • 00:07:43
    create a for loop and instantiate all
  • 00:07:46
    the cards needed
  • 00:07:49
    and in the
  • 00:07:51
    instantiate function we use the two
  • 00:07:54
    parameters
  • 00:07:55
    version where we add the the
  • 00:07:59
    prefab which
  • 00:08:00
    we would want to instance it and we add
  • 00:08:03
    the parent which will be the grid panel
  • 00:08:07
    that has the component the canvas
  • 00:08:09
    manager so it will be this transform
  • 00:08:14
    add as a parent
  • 00:08:15
    shown in this
  • 00:08:20
    helper
  • 00:08:22
    hyper tip
  • 00:08:25
    and for
  • 00:08:26
    easier debugging and nicer hierarchy we
  • 00:08:30
    can add the name to each of our cards
  • 00:08:33
    with the number in the name we can
  • 00:08:36
    separate it
  • 00:08:42
    and for later use when we want to
  • 00:08:45
    create matches or just add the random
  • 00:08:48
    card types to each of the cards we
  • 00:08:51
    should store them in some way and
  • 00:08:54
    earlier we said that card controller
  • 00:08:56
    will be responsible for the card datas
  • 00:09:00
    so we can create a list of card
  • 00:09:02
    controllers
  • 00:09:05
    we should instantiate that list that
  • 00:09:08
    that is that is a new list of card
  • 00:09:10
    controllers
  • 00:09:14
    and we can now add each of our cards to
  • 00:09:18
    this list
  • 00:09:20
    because we add the card controller to
  • 00:09:22
    the prefabs each of our newly created
  • 00:09:25
    game object has a component called card
  • 00:09:28
    controller
  • 00:09:35
    so after that we should delete all the
  • 00:09:38
    created cards and
  • 00:09:40
    press play and now we can see all of our
  • 00:09:42
    20 cards based on the normal difficulty
  • 00:09:45
    it will be a four by five grid
  • 00:09:48
    that's it for this video
  • 00:09:54
    in the next video we will work on the
  • 00:09:56
    card types for each position and the
  • 00:09:58
    randomization in the card grid
  • 00:10:01
    thanks for watching and see you next
  • 00:10:03
    time bye
Tags
  • card prefab
  • grid layout
  • card controller
  • canvas manager
  • difficulty settings
  • game development
  • Unity
  • scriptable objects
  • card game
  • instantiation