T echnojs
Already have an account?
login Login

Or,

Don't have an account?
group_add Register

Showing suggestions...

close
cookie
Cookies Consent

This site uses cookies to offer you a better browsing experience.

Unity 2D: Building UI System for Monster Chase game

UI system of Monster Chase game consists of background, text, player Chosen button, player score and high score.

  1. Create a new scene for UI system of Monster Game.
    /*
    * Go to File » New Scene » Basic 2D (Built-in) » Create (button)
    * Hierarchy » Untitled » three dot menu » Save scene » Scenes (choose directory) » MainMenu (named) » Save (button)
    */
  2. Create a new UI image by right-click on empty space of hierarchy, then select "Image" from UI menu and renamed it with "BG".
  3. When creating a UI element for the first time in a project scene, Canvas & EventSystem object will be automatically created by default.
  4. Because every UI element must be wrapped by a Canvas object and any event on the UI element must be tracked by an EventSystem object.
    /*
    * Inspector (Canvas GameObject)
    * Canvas » Render Mode » Screen Space - Camera (select)
    * Canvas » Render Mode » Render Camera » Main Camera (drag from hierarchy)
    * Canvas Scaler » UI Scale Mode » Scale With Screen Size (select)
    * Canvas Scaler » Reference Resolution : x = 1920, y = 1080
    * Canvas Scaler » Match » 0.5
    */
  5. Add the background sprite image to the BG UI and adjust the position of background on the scene.
    /*
    * Inspector (BG GameObject)
    * Image » Source Image » Background (drag from Sprites folder)
    * Rect Transform » Anchor » Stretch center » Scale the image (using Rect Tool)
    * Rect Transform » Left: 0, Top: 0, Pos Z: 0, Right: 0, Bottom: 0
    */
  6. Create a new UI image object called "Moon" under the Canvas obj and add moon sprite image on it.
    /*
    * Inspector (Moon GameObject)
    * Image » Source Image » Moon (drag from Sprites folder)
    * Image » Set Native Size (button) (click)
    * Rect Transform » Anchor » top left
    * Rect Transform » Pos X: 208 (any), Pos Y: -189 (any), Pos Z: 0
    * Rect Transform » Width: 493, Height: 446
    */
  7. Create a new UI text object called "Game Logo" under the Canvas obj and add text on it.
    /*
    * Inspector (Game Logo GameObject)
    * Rect Transform » Pos X: 73 (any), Pos Y: 307 (any),Pos Z: 0
    * Rect Transform » Width: 1511 (any), Height: 433 (any)
    * Text » Text » Monster Chase (enter)
    * Text » Character » Font » FEASFBI_ (drag from Fonts folder)
    * Text » Character » Font Style » Bold
    * Text » Character » Font Size » 230
    * Text » Paragraph » Alignment » Horizontal : Center, Vertical : Center  
    * Text » Color » White (#fff)
    */
  8. Select the Player Select.png asset from the sprites assets directory.
    /*
    * Inspector (Player Select.png Sprite Asset)
    * Sprite Mode » Multiple (change) » Apply (button)
    * Sprite Editor » Fullscreen (maximize) 
    * Sprite Editor » Slice » Type » Automatic (select) » Slice (button)
    * Sprite Editor » Player Select_0 (slice outlined) » Player 1 Select (rename)
    * Sprite Editor » Player Select_1 (slice outlined) » Player 2 Select (rename)
    * Sprite Editor » Apply (button)
    */
  9. Create a new object on the hierarchy called "Main Menu Controller" and reset it transform.
    /*
    * Inspector (Main Menu Controller GameObject)
    * Transform » Reset
    * Add Component » New Script » MainMenuController (named)» Create and Add
    */
  10. Create two new UI button object called "0" & "1" under the Canvas obj and add player select sprite image on it.
    /*
    * Inspector (0 & 1 GameObject)
    * First delete text object of button from the hierarchy
    * 0 (obj) » Rect Transform » Pos X: -326 (any), Pos Y: -150 (any), Pos Z: 0
    * 1 (obj) » Rect Transform » Pos X: 326 (any), Pos Y: -150 (any), Pos Z: 0
    * Rect Transform » Width: 341 (any), Height: 341 (any)
    * 0 (obj) » Image » Source Image » Player 1 Select (drag from Sprites folder)
    * 1 (obj) » Image » Source Image » Player 2 Select (drag from Sprites folder)
    * On Click () » Add empty event » Main Menu Controller (drag from hierarchy) » PlayGame (select method from MainMenuController script)
    */
  11. To compile all the scene for the build, you must add the scenes on the build settings.
    // File » Build Settings... » Scenes In Build » Add Open Scenes (button)
    
  12. Create an "Game Manager" empty game object on hierarchy with transform reset.
    /*
    * Inspector (Game Manager GameObject)
    * Transform » Reset
    * Add Component » New Script » GameManager (named) » Create and Add
    * Game Manager (Script) » Characters (property) » Player 1 & Player 2 (drag from prefabs)
    */
  13. Scripts: unity-scripts-for-2d-monster-chase-game
by
question_answer Comments
bar_chart Views
people_alt Contributors
category Category workspaces Group
public Visibility
monetization_on Monetisation
checklist Authorization
save Created
today Updated

Any Question / Leave a comment ?
--- Thank you for your attention! ---

Related Posts