UNITY | PROJECT | SPACE SHOOTER 2D | 8. Lives UI, Play Button, & Game Manager

2
Importing UI Sprites
3
Adding UI Image
  • Adding UI Image for Score & Lives

 

4

  • Adding Sprite to UI Image as “Source Image”

 

5
UI Image Applied

 

6
UI Anchor

 

7.1
Adding & Configuring Sprite to UI Image for Scores UI
  • Adding Sprite to UI Image as “Source Image”

 

7.2
UI Anchor

 

8
Creating UI Text
  • Creating UI Text to count Player’s Life

 

9
Adding & Configuring UI Text to Image UI
  • In UI Text
    • set the default Text as “0”
    • change Font Style & Size
108-e1531887916889.png
UI Anchor

 

11
Creating UI Button
  • Create UI Button for Play Button

 

12

13

14
Adding Sprite to UI Button
  • In UI Button
    • Set the size
    • Add Sprites for “Target Graphic”(usual) & “Pressed Sprite”(pressed)

 

15
UI Anchor

 

16
Creating “GameManager” Script
17
Adding Reference
181.png
“GameManager” Script
  • To control PlayButton&Player with GameManager
    • create Public Object as a Reference
    • create an Enumeration, “GameManagerState” to tell Game State
      • Opening, GamePlay, and GameOver
    • create a Function, “UpdateGameManagerState()”
      • use “Switch Statement”
        • make 3 cases

 

19.1
“Play Button”
  • now, Play Button reacts to Clicking but nothing happens

 

19.2
“PlayerControl” Script
스크린샷 2018-07-18 오전 2.21.17
“Player”
20.1
“PlayerControl” Script
ec8aa4ed81aceba6b0ec83b7-2018-07-18-ec98a4eca084-1-22-31.png
“PlayerControl” Script
  • create “GameManager” GameObject as a reference
  • set “Player” inactive for the first moment of Game
  • In “Init()” method, which is called when Game Starts
    • set the current life(“lives”) as MaxLife
    • update UIText by using Text.text property
    • change the number(int “lives”) to string by using int.Tostring() method
    • set Player active when Game starts
  • In “OnTriggerEnter2D()” method(when a Collision with Player happens,)
    • the life is subtracted
    • when the life is 0,
      • use “SetManagerState” in “GameManager” Class with “GameOver”()*(putting the name of “enum”)
      • hide Player GameObject by set it “false”

 

8. Live UI, Play Button, - GameManager
“GameManager” Script
  • update “GameManager” Script
    • in “Start()” method
      • set “GMState” in “Opening”
    • in “UpdateGameManagerState()”
      • in case of “GamePlay”,
        • Set “PlayButton” active to hide it when Game Starts
        • Activate “Init()” method
    • Create” SetGameMangerState()” method to change the Game’s State, having “GameManagerState”(enum) as Parameter
    • in “StartGamePlay()” method
      • change “GMState” into “GamePlay”
      • Don’t forget to put “UpdateGameManagerState()”

 

 

21
“PlayButton” | Sprite & “On Click()”

 

  • In Play Button,
    • Add Sprite for “Target Graphics”(Usual) and “Pressed Sprite”(Pressed)
    • Add “On Click()” event and designate it “StartGamePlay()” method in “GameManager” Class
    • with this part, No Need to write to Star Game

 

231-e1531888420395.png
Adding Reference
ezgif.com-video-to-gif
“PlayButton” & “PlayerLife” working
26
“GameOver” Image added
  • Add “GameOver” Sprite
    • add the sprite to GameManager
    • add the Sprite to the scene
27
GameOver “Off”
  • Turn off “GameOver” for it to be hidden in the first moment of Game

 

28
“EnemySpawner” Script
  • Create a Function, “UnscheduleEnemySpawner()”, to stop the spawning when the game is over” by using “CancelInvoke()”

 

29
“GameManager” Script
  • Create “ChangeToOpeningState()” method
    • use “SetGameMangerState()” method
  • In Case of “GameOver”,
    • put “UnscheludeEnemySpawner()” method to stop the spanwing
  • In Case of “Opening”
    • turn off “GameOver” by using “GameObject.Setactive()”
30
Adding Reference
ezgif.com-video-to-gif
Game Working