< Return to Video

2D Roguelike 12 of 14 : UI & Levels

  • 0:02 - 0:05
    In this video we're going to add some
  • 0:05 - 0:08
    user interface elements to our game
  • 0:08 - 0:12
    including title cards for when we start and change levels
  • 0:12 - 0:15
    and some text to reflect player's score.
  • 0:16 - 0:19
    To do this we're going to use Unity's UI system.
  • 0:19 - 0:25
    We're going to go to Game Object - UI - Canvas.
  • 0:28 - 0:30
    The canvas is going to hold all of our
  • 0:30 - 0:32
    user interface elements.
  • 0:33 - 0:35
    The first element that we're going to create is
  • 0:35 - 0:37
    going to be an image which we're going to use
  • 0:37 - 0:40
    as a background for our title cards.
  • 0:40 - 0:44
    Let's go to Game Object - UI - Image.
  • 0:46 - 0:49
    Notice that it's created as a child of the canvas
  • 0:49 - 0:51
    and the first thing that we're going to do
  • 0:51 - 0:54
    is switch over to our scene view and zoom out.
  • 0:56 - 0:58
    And we're going to use the anchor presets here
  • 0:58 - 1:02
    to stretch this to fit by option-clicking.
  • 1:07 - 1:10
    We're also going to set the colour to black.
  • 1:16 - 1:18
    We'll call this Level Image.
  • 1:21 - 1:24
    Next we're going to create a text object
  • 1:24 - 1:26
    that is going to display what level
  • 1:26 - 1:28
    the player is currently on
  • 1:28 - 1:30
    as we start and change levels.
  • 1:30 - 1:32
    Let's highlight our level image
  • 1:33 - 1:37
    Game Object - UI - Text.
  • 1:37 - 1:39
    We'll call this Level Text.
  • 1:39 - 1:41
    The first thing that we're going to do is anchor it to
  • 1:41 - 1:44
    the centre of the screen, we'll click on our anchor presets
  • 1:44 - 1:47
    and option or alt click the centre preset.
  • 1:51 - 1:53
    Next we'll set the colour of our text
  • 1:53 - 1:55
    to white by clicking on Colour
  • 1:56 - 1:58
    and then dragging it to white.
  • 1:59 - 2:01
    We'll set out font size to 32.
  • 2:03 - 2:06
    Notice that went we do this the text disappears.
  • 2:06 - 2:08
    That's because the text has become too
  • 2:08 - 2:10
    big to fit within the width and
  • 2:10 - 2:13
    hight dimensions specified in the rect transform.
  • 2:13 - 2:15
    Because we're going to have text that's going to vary
  • 2:15 - 2:19
    in size displaying both our level number and our game over message
  • 2:19 - 2:21
    we're going to use the horizontal and vertical
  • 2:21 - 2:24
    overflows to allow the text to overflow.
  • 2:25 - 2:28
    We're going to set horizontal overflow to overflow
  • 2:28 - 2:31
    and vertical overflow to overflow as well.
  • 2:31 - 2:35
    Now our text reappears, we're also going to centre
  • 2:36 - 2:38
    and middle align it.
  • 2:40 - 2:43
    Let's set the font by clicking on the asset picker.
  • 2:44 - 2:46
    And selecting Press Start
  • 2:46 - 2:48
    to play a regular.
  • 2:48 - 2:50
    This font ships with the project.
  • 2:52 - 2:55
    Finally we'll set our text default, which is going to be Day 1.
  • 2:56 - 2:58
    And so that should be good for the level text.
  • 2:58 - 3:01
    We're going to make a it child of our level image
  • 3:01 - 3:03
    so that when we deactivate the image
  • 3:03 - 3:06
    the level text will be deactivated as well.
  • 3:08 - 3:11
    Next we're going to create our food text display.
  • 3:14 - 3:15
    Highlight the canvas,
  • 3:15 - 3:19
    choose Game Object - UI - text.
  • 3:19 - 3:21
    This text we're going to align to the bottom
  • 3:21 - 3:23
    of the screen in the centre.
  • 3:23 - 3:25
    Let's click on our anchor presets
  • 3:25 - 3:28
    and click the bottom centre preset
  • 3:28 - 3:30
    while holding down the option or alt keys.
  • 3:36 - 3:37
    Again we'll set the colour to white.
  • 3:38 - 3:40
    Click Colour, drag to white.
  • 3:42 - 3:44
    We're going to set the size to 24.
  • 3:49 - 3:51
    Centre and middle align
  • 3:52 - 3:55
    and we'll set our text to Food 100.
  • 4:02 - 4:04
    We'll set our font
  • 4:05 - 4:07
    to PressStart2P-Regular
  • 4:07 - 4:10
    Notice again that the text is being cut off.
  • 4:10 - 4:12
    We'll set our horizontal
  • 4:13 - 4:15
    and vertical overflows to Overflow.
  • 4:17 - 4:19
    Let's label our text FoodText
  • 4:19 - 4:22
    and what we can see here is that the food text
  • 4:22 - 4:26
    is being displayed in front of our title card image.
  • 4:26 - 4:28
    We don't want this to happen so what we're going to do
  • 4:28 - 4:30
    is move FoodText up in the hierarchy
  • 4:30 - 4:32
    so it's directly under the canvas.
  • 4:33 - 4:35
    This means the it will be rendered behind
  • 4:35 - 4:38
    the LevelImage and it's child LevelText.
  • 4:39 - 4:41
    The last thing that we want to do is
  • 4:41 - 4:44
    let's temporarily deactivate the LevelImage
  • 4:44 - 4:46
    and we're going to move our FoodText up
  • 4:46 - 4:48
    just a little bit from the bottom.
  • 4:48 - 4:50
    We're going to do this by setting the anchors.
  • 4:51 - 4:55
    We're going to set the Y anchor to 0.05.
  • 4:55 - 4:58
    We can see that it moves in the UI
  • 4:58 - 5:01
    and we're going to set the Max to 0.05 as well.
  • 5:02 - 5:04
    Notice that the text hasn't moved when we've done this.
  • 5:05 - 5:09
    But a -13 pixel offset has been incurred in the rect transform.
  • 5:09 - 5:12
    Let's now set the position Y to 0
  • 5:12 - 5:14
    and we'll see our text move up.
  • 5:16 - 5:19
    Let's reactivate our LevelImage
  • 5:19 - 5:21
    and then we can get in to some scripting
  • 5:21 - 5:23
    to control these UI elements.
  • 5:29 - 5:31
    Let's go to our Scripts folder
  • 5:32 - 5:34
    and open our Game Manager in Monodevelop.
  • 5:35 - 5:37
    Next we're going to add the code that we need
  • 5:37 - 5:40
    to our Game MAnager to manage our UI
  • 5:40 - 5:43
    and also to handle transitions between levels.
  • 5:43 - 5:46
    Let's reset our private integer level to 1
  • 5:46 - 5:48
    so that we start on level 1 now.
  • 5:48 - 5:50
    The first thing that we're going to add to our Game Manager script
  • 5:50 - 5:54
    is the namespace declaration using UnityEngine.UI.
  • 5:54 - 5:56
    Next we're going to add a public float
  • 5:56 - 5:58
    variable called levelStartDelay.
  • 5:59 - 6:01
    This is going to be the time to wait before
  • 6:01 - 6:03
    starting levels, in seconds.
  • 6:03 - 6:06
    Next we're going to add a private variable
  • 6:06 - 6:08
    of the type Text called levelText.
  • 6:09 - 6:11
    This is going to be the text that is going to display
  • 6:11 - 6:14
    the current level number which we've currently got set
  • 6:14 - 6:16
    to day 1 in the editor.
  • 6:17 - 6:19
    We're also going to declare a private variable
  • 6:19 - 6:22
    of the type GameObject called levelImage.
  • 6:22 - 6:25
    We're going to use this to store a reference to our level image
  • 6:25 - 6:28
    so that we can activate it and deactivate it
  • 6:28 - 6:30
    as we want to show and hide it.
  • 6:30 - 6:32
    We're also going to add a private boolean
  • 6:32 - 6:35
    called doingSetup which we're going to use to
  • 6:35 - 6:38
    check if we're setting up the board and prevent
  • 6:38 - 6:40
    the player from moving during setup.
  • 6:40 - 6:42
    After awake we're going to add
  • 6:42 - 6:44
    a new private function that returns void
  • 6:44 - 6:47
    called OnLevelWasLoaded which takes an integer
  • 6:47 - 6:50
    parameter called index.
  • 6:50 - 6:53
    OnLevelWasLoaded is part of the Unity API
  • 6:53 - 6:56
    and it's called every time a scene is loaded.
  • 6:57 - 6:59
    We're going to use this to add to our level number
  • 6:59 - 7:01
    and to call our InitGame function
  • 7:01 - 7:03
    after a new level has been loaded.
  • 7:04 - 7:07
    We're going to use InitGame to manage
  • 7:07 - 7:10
    our UI elements and setup each level.
  • 7:10 - 7:13
    We're going to start by setting our doingSetup boolean to true.
  • 7:14 - 7:16
    This means the player won't be able to move while
  • 7:16 - 7:17
    the title card is up.
  • 7:17 - 7:19
    Next we're going to get a reference
  • 7:19 - 7:21
    to our LevelImage object
  • 7:21 - 7:23
    using GameObject.Find.
  • 7:23 - 7:25
    Here we're finding by name so you want
  • 7:25 - 7:27
    to make sure that your game object name
  • 7:27 - 7:30
    in the editor matches the string that we're using
  • 7:30 - 7:32
    with GameObject.Find.
  • 7:32 - 7:34
    We're going to do the same thing for our LevelText
  • 7:34 - 7:36
    but we're also going to get a component reference
  • 7:36 - 7:37
    to the text component.
  • 7:37 - 7:39
    Next we're going to set the text of
  • 7:39 - 7:42
    our LevelText to our current level number.
  • 7:43 - 7:45
    The text that we're setting is going to be a string so
  • 7:45 - 7:47
    we're going to provide Day with a
  • 7:47 - 7:49
    space and then we're going to append
  • 7:49 - 7:52
    our level integer at the end,
  • 7:52 - 7:56
    so in this case it'll read 'Day 1'.
  • 7:57 - 7:59
    We're going to activate our LevelImage
  • 7:59 - 8:01
    game object using SetActive.
  • 8:02 - 8:04
    Next we're going to declare a private function
  • 8:04 - 8:07
    that returns void called HideLevelImage.
  • 8:07 - 8:09
    We're going to use this to turn off our LevelImage
  • 8:09 - 8:11
    when we're ready to start the level and we're going to
  • 8:11 - 8:13
    invoke it from within InitGame.
  • 8:14 - 8:17
    Inside HideLevelImage we're going to turn off
  • 8:17 - 8:18
    our LevelImage.
  • 8:19 - 8:21
    And we're also going to set DoingSetup
  • 8:21 - 8:24
    to false so that the player can now move.
  • 8:24 - 8:26
    Back in InitGame we're going to
  • 8:26 - 8:28
    invoke HideLevelImage
  • 8:28 - 8:32
    parsing in LevelStartDelay as our delay time.
  • 8:33 - 8:35
    This means that once we've displayed our title card
  • 8:35 - 8:38
    we're going to wait 2 seconds before turning it off.
  • 8:39 - 8:41
    In update we're going to add a
  • 8:41 - 8:44
    check for DoingSetup to our If statement.
  • 8:45 - 8:47
    This is what's going to prevent the player from moving
  • 8:47 - 8:49
    if DoingSetup is true.
  • 8:49 - 8:52
    In GameOver we're going to show a message to the player
  • 8:52 - 8:54
    saying how many days they've survived for.
  • 8:55 - 8:57
    The message is going to read
  • 8:57 - 9:01
    'After level number of days you starved'.
  • 9:02 - 9:04
    We're also going to enable our black background.
  • 9:08 - 9:10
    Lets save our script and return to the editor.
  • 9:13 - 9:15
    The next thing that we need to do is add a few
  • 9:15 - 9:17
    lines of code to our Player
  • 9:17 - 9:19
    so that our player can update the FoodText
  • 9:19 - 9:21
    as the value changes.
  • 9:21 - 9:23
    Let's open our Player in Monodevelop
  • 9:24 - 9:26
    In Player we're also going to add the namespace
  • 9:26 - 9:29
    declaration using UnityEngine.UI.
  • 9:29 - 9:33
    Next we're going to add a public variable
  • 9:33 - 9:35
    of the type Text called foodText.
  • 9:35 - 9:37
    In start we're going to set the value
  • 9:37 - 9:40
    of foodText to the current Food score.
  • 9:42 - 9:44
    In AttemptMove we're going to do the same thing
  • 9:44 - 9:47
    after we've subtracted from the player's food points score.
  • 9:48 - 9:50
    We're also going to display a message
  • 9:50 - 9:52
    when the player picks up a food object
  • 9:52 - 9:54
    or a soda object.
  • 10:01 - 10:04
    So when the player picks up a food object
  • 10:04 - 10:07
    we're going to display + pointsPerFood
  • 10:07 - 10:09
    along with their current food points.
  • 10:09 - 10:11
    We're going to do the same thing for soda.
  • 10:13 - 10:15
    In loseFood we're going to display a similar
  • 10:15 - 10:17
    message showing how many food points
  • 10:17 - 10:19
    the player lost when they were attacked.
  • 10:24 - 10:26
    Let's save our script and return to the editor.
  • 10:29 - 10:31
    In the editor we need to assign a reference
  • 10:31 - 10:34
    to our FoodText object to our player.
  • 10:35 - 10:37
    Let's choose Player
  • 10:37 - 10:39
    and then we're going to drag in FoodText
  • 10:42 - 10:44
    to our FoodText variable slot.
  • 10:46 - 10:48
    Let's play our scene and give it a try.
  • 10:49 - 10:51
    So we see our title card displays, that's working.
  • 10:51 - 10:54
    When we move our Food text updates.
  • 10:57 - 10:59
    We get our delay before moving to a new level.
  • 10:59 - 11:01
    Title card displays with the updated level number.
  • 11:02 - 11:03
    When we're attacked by our enemy
  • 11:03 - 11:05
    we see our -10 message.
  • 11:07 - 11:09
    And so it looks like everything is working.
  • 11:10 - 11:12
    Now that we've got our UI elements working
  • 11:12 - 11:14
    In the next video we're going to add
  • 11:14 - 11:17
    some sound effects and music
  • 11:17 - 11:20
    along with the scripts that we need to control them.
Title:
2D Roguelike 12 of 14 : UI & Levels
Description:

more » « less
Duration:
11:26

English subtitles

Revisions