CS2335
Master_v2
Master_v2
  • Introduction
  • Introduction
    • Introduction
      • Design
      • Game Design
    • Unity - Download
    • Visual Studio - IDE
    • Hero's Journey
  • Unity Basics
    • Unity Editor Windows
    • MonoBehavior - Base-Class
    • Unity Engine - Event Functions
  • Getting Started
    • UI-Elements
    • Animator Controller
      • Animation Steps
    • PlayerController Flow Chart
    • PlayerController Code
      • PlayerController - V1 - S20
      • PlayerController V2 S20
      • PlayerController V3 S20
  • Project 1 - Simple Game
    • Overview
    • Project 1 - Get Started
      • UML Class Diagram
    • Player GameObject
      • PlayerController.cs V2
      • PlayerController.cs V3
    • Create 2D Sprite Prefab: Rock
    • Sorting Layers
  • Project1 Code
    • PickUp PreFabs
    • Player GameObject
    • PlayerController - jump
    • GameData Version1
    • PlayerStats Version1
    • MiniGameManager
      • Logic Diagram
    • Simple Spawner
    • Utility Class
  • Project1 Enhancements
    • PickUp - SelfDestruct
    • Spawn from List of Prefabs
  • Project 2 - StateManager
    • Project 2 - Learning Objectives
    • Project 2 - Starter Assets
    • Project 2
      • State Machine Framework
        • Singleton Pattern
      • StateManager - Singleton Design Pattern
      • IStateBase, BeginState
      • Project 2 -Steps: Create new Scene and State
      • Project 2 - List of Steps
        • Project 2 - Starter Code
  • Project 2 -Dialog
    • Hide_Show_Panel Script
    • Configure TitlePanel, DecisionPanel
    • Simple Dialog Prefab
    • Conversation Scriptable Objects
    • DialogManager_ConvList
    • Image Transitions for Buttons
  • UI Components
    • Finding Game Objects
    • Game Objects: UI vs. 2D Sprite
    • UI Elements
      • Canvas: Screen-Space Render-Mode
      • UI-Buttons To Change Scene
      • Text Input
  • Project2 Resources
    • Visual Novel in Unity-Links
    • Scriptable Object Factory
      • ScriptableObjects
    • Dialog Prefab Packages
  • Project 3 - Overview
    • Branching Story Structures
    • Dictionary Data-Structure
      • Unity PlayerPrefs Dictionary
    • Dictionary: User-Choice Data
      • User-Choices - Example
        • Dictionary Value to Disable Options
    • Simplified Mini-Game
      • PlayerController_v2 Mods
        • PlayerController_v2_final
      • MiniGameManager_v2
  • Proj3: Inventory System
    • Inventory-System
      • Install and Configure
      • Diagrams, Resources
        • Item, Gem, Potion Classes
        • Inventory Class
      • InventoryDisplay, Slot UI
        • InventoryDisplay Class
        • Slot Class
        • Hazard Class
        • Layout Groups
      • Customization Steps
        • Configure Animation
        • AddItem Button
        • Concrete Class: Food
        • MiniGame Mods
          • PlayerController Mods
      • Code: InventorySystem
        • GameData, PickUp Mods
      • Resources: Data Structures
  • Proj3: Custom UnityEvents
    • Event Publishing Patterns
    • Custom Event Messaging
  • Proj3: Mini-Game
    • MiniGame-Overview-Proj3
    • LevelManager
      • LevelManager Logic Diagram
      • LevelManager FSM
      • LoadLevel, StartLevel Logic
      • Code Framework
    • Timer
  • Project 3 - Code Mods
    • Project 3 - Steps
    • Project 3 - Code
      • Code: Final Versions
        • PlayerController Mods
          • PlayerController_v2 Mods
        • GameData - Final
        • LevelManager
        • PlayerStats - Final
        • PickUp, Hazard, ScorePickUp
        • Spawner - Final
        • CameraFollow
        • ScreenFader
        • MiniGameState
        • Example: EndState
      • MiniGameWin Logic
  • Optional, Supplemental Content
    • Optional Content
      • Adding Audio
      • Screen Fading and Reloading
      • ScriptableObjects
      • Disable Debug Logging
      • Events and Actions
      • Saving Data - Serialization
      • Parallax Scrolling
      • Change Sprites
  • C# Language
    • C# Language
      • Variables
      • Enum
      • Encapsulation
        • C# Properties
        • Access Modifiers
      • Inheritance
      • Polymorphism
      • Interface
      • Switch-Case
      • List< T >
      • Queue< T >
      • Dictionary
      • Foreach
      • Static
      • Ternary Operator: ?
      • this
      • Delegates
    • Diagrams
      • State Machine Framework
      • UML Class Diagrams
      • Level Manager Logic Diagram
      • Flow-Chart: NumberGame
      • FSM: NumberGame
    • Glossary
    • References and Resources
    • Random Thoughts
Powered by GitBook
On this page
  • Canvas
  • Canvas: Configure ScreenSpace-Camera
  • UI-Panel and Text for Score
  • UI-Button, Child-GameObject: Text
  • Set Position, Color of Button,
  • Animation: Set Canvas Render Mode to Screen Space-Camera
  • Animation: Add UI Panel and UI Text.
  • UI-Panel and Text for GameOver Notification.

Was this helpful?

  1. Getting Started

UI-Elements

PreviousUnity Engine - Event FunctionsNextAnimator Controller

Last updated 5 years ago

Was this helpful?

User Interaction Elements provide easy to configure gameObjects that allow for user interaction with the game.

In this section, we'll discuss how to create and position UI elements in a scene.

Canvas

All UI elements are children of the Canvas class. When the first UI element is added to a scene, in addition, a Canvas gameObject and an EventSystem gameObject are also added to a scene, these are required to be in the Hierarchy for every scene that has UI elements to be displayed and have interactivity.

Canvas: Configure ScreenSpace-Camera

When the Canvas is added to a scene, by default, it's layout is offset from the main camera. For our purposes, it is easiest to configure the Canvas Component so that it's rendered in the same position as the regular game scene. To do that, we need to change the Render-Mode of the Canvas, we'll change it from Screen-Space Overlay to Screen Space - Camera, and we'll select the Main Camera as the Render Camera for the Canvas Component. See image and animation below to configure the Render Mode of the Canvas to be Screen Space Camera, with the Render Camera set to Main Camera.

UI-Panel and Text for Score

Add a UI-Panel to the scene, name it: 'ScorePanel'. Add a Text gameObject as a child of the 'ScorePanel', name this 'ScoreText'. For the 'ScorePanel', set the anchors to that it's anchored to the upper right corner of the scene. Configure the 'ScoreText', text element, so it is stretched to fit the 'ScorePanel', and set the starting text value to show 'Score: 0', with BestFit and alignment center, center as shown in the image below. Select a color for the panel and text so they will be visible during gameplay.

UI-Button, Child-GameObject: Text

Add a UI Button to the game scene. Name the button 'StartButton'. The StartButton has a child gameObject: Text. Configure the Text GameObject, so it's Text Component', Text field has a value of 'Start Game'. See the animation below to add a StartButton gameObject to the scene.

Set Position, Color of Button,

Use the RectTransform tool to set the anchors and position of the button to the center of the scene. Set the 'normal' and 'highlight' colors of the button, so that you'll be able to see the button's highlight state when moused-over.

Animation: Set Canvas Render Mode to Screen Space-Camera

The animation below shows that when you add a UI element, (such as a Button GameObject, to your scene, a Canvas and EventSystem are also added to the Hierarchy. Then you need to configure the Canvas GameObject so it's set to Screen Space-Camera as described above.

Animation: Add UI Panel and UI Text.

The animation below shows how to add a UI-Panel, then how to add a UI-Text element and make that a child of the UI-Panel. Next, the panel is resized using the rect tool in the upper-right tools menu.The panel's Rect Transform is set so that the panel is anchored to the upper-right corner of the scene, we can see 4 triangles clustered in the upper-right corner showing that the all 4 anchors are set to the upper-right corner of the canvas. Then the Text has it's Rect Transform set so it's stretched and anchored to it's parent, the UI Panel. Then Best Fit is selected for the text, and it's color is changed to white, it's Text field is set to display: Score: 0. The UI-Panel should be named: ScorePanel, the UI-Text should be named: ScoreText

UI-Panel and Text for GameOver Notification.

Create a UI-Panel and a child-gameObject that is a Text element, that'll be used for the display of the GameOver notification. Name the Panel-gameObject: 'GameOverPanel' and name the Text-gameObject: 'GameOverText'. Configure the rect-transform for the Text so it's stretched to fit the Panel, anchor it to the corners of it's parent. Resize the Panel and move to the center of the scene. Set the color of the panel and text so they are visible during gameplay.