Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Unity 2018 Cookbook
Unity 2018 Cookbook

Unity 2018 Cookbook: Over 160 recipes to take your 2D and 3D game development to the next level , Third Edition

Arrow left icon
Profile Icon Matt Smith Profile Icon Francisco Queiroz
Arrow right icon
₱1742.99 ₱2490.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
eBook Aug 2018 794 pages 3rd Edition
eBook
₱1742.99 ₱2490.99
Paperback
₱3112.99
Subscription
Free Trial
Arrow left icon
Profile Icon Matt Smith Profile Icon Francisco Queiroz
Arrow right icon
₱1742.99 ₱2490.99
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
eBook Aug 2018 794 pages 3rd Edition
eBook
₱1742.99 ₱2490.99
Paperback
₱3112.99
Subscription
Free Trial
eBook
₱1742.99 ₱2490.99
Paperback
₱3112.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Unity 2018 Cookbook

Responding to User Events for Interactive UIs

In this chapter, we will cover the following:

  • Creating UI Buttons to move between scenes
  • Animating button properties on mouse-over
  • Organizing image panels and changing panel depths via buttons
  • Displaying the value of an interactive UI Slider
  • Displaying a countdown timer graphically with a UI Slider
  • Setting custom mouse cursors for 2D and 3D GameObjects
  • Setting custom mouse cursors for UI controls
  • Interactive text entry with an Input Field
  • Toggles and radio buttons via Toggle Groups
  • Creating text and image icon UI Dropdown menus
  • Displaying a radar to indicate the relative locations of objects

Introduction

Almost all the recipes in this chapter involve different interactive UI controls. Although there are different kinds of interactive UI controls, the basic way to work with them, and have scripted actions respond to user actions, is all based on the same idea: events triggering the execution of object method functions.

Then, for fun, and an example of a very different kind of UI, the final recipe demonstrates how to add to your game a sophisticated real-time communication of the relative positions of objects in the scene (that is, a radar!).

The big picture

The UI can be used for three main purposes:

  1. To display static (unchanging) values, such as the name or logo image of the game, or word labels such as Level...

Creating UI Buttons to move between scenes

As well as scenes where the player plays the game, most games will have menu screens, which display to the user messages about instructions, high scores, the level they have reached so far, and so on. Unity provides UI Buttons to offer users a simple way to indicate their choices.

In this recipe, we'll create a very simple game consisting of two screens, each with a button to load the other one, as illustrated in the screenshot:

How to do it...

To create a button-navigable multi-scene game, follow these steps:

  1. Create a new Unity 2D project.
  2. Save the current (empty) scene, in a new folder, _Scenes, naming the scene page1.
  3. Add a UI Text object positioned at the top center of...

Animating button properties on mouse-over

At the end of the previous recipe, we illustrated two ways to visually communicate buttons to users. The Animation of button properties can be a highly effective, and visually interesting, way to reinforce to the user that the item their mouse is currently over is a clickable, active button. One common animation effect is for a button to get larger when the mouse is over it, and then it shrinks back to its original size when the mouse pointer is moved away. Animation effects are achieved by choosing the Animation option for the Transition property of a Button GameObject, and by creating an animation controller with triggers for the Normal, Highlighted, Pressed, and Disabled states.

How to do it...

...

Organizing image panels and changing panel depths via buttons

UI Panels are provided by Unity to allow UI controls to be grouped and moved together, and also to visually group elements with an Image background (if desired). The sibling depth is what determines which UI elements will appear above or below others. We can see the sibling depth explicitly in the Hierarchy, since the top-to-bottom sequence of UI GameObjects in the Hierarchy sets the sibling depth. So, the first item has a depth of 1, the second has a depth of 2, and so on. The UI GameObjects with larger sibling depths (further down the Hierarchy and so drawn later) appear above the UI GameObjects with lower sibling depths.

In this recipe, we'll create three UI Panels, each showing a different playing card image. We'll also add four triangle arrangement buttons to change the display order (move to bottom,...

Displaying the value of an interactive UI Slider

This recipe illustrates how to create an interactive UI Slider, and execute a C# method each time the user changes the UI Slider value:

How to do it...

To create a UI Slider and display its value on the screen, follow these steps:

  1. Create a new 2D project.
  1. Add a UI Text GameObject to the scene with a Font size of 30 and placeholder text, such as slider value here (this text will be replaced with the slider value when the scene starts). Set Horizontal- and Vertical- Overflow to Overflow.
  2. In the Hierarchy add a UI Slider GameObject to the scene choose menu: GameObject | UI | Slider.
  3. In the Inspector, modify the settings for the position of the UI Slider GameObject...

Displaying a countdown timer graphically with a UI Slider

There are many cases where we wish to inform the player of the proportion of time remaining, or at the completion of some value at a point in time, for example, a loading progress bar, the time or health remaining compared to the starting maximum, or how much the player has filled up their water bottle from the fountain of youth. In this recipe, we'll illustrate how to remove the interactive "handle" of a UI Slider, and change the size and color of its components to provide us with an easy-to-use, general purpose progress/proportion bar. In this recipe, we'll use our modified UI Slider to graphically present to the user how much time remains for a countdown timer:

Getting ready

...

Setting custom mouse cursors for 2D and 3D GameObjects

Cursor icons are often used to indicate the nature of the interaction that can be done with the mouse. Zooming, for instance, might be illustrated by a magnifying glass; shooting, on the other hand, is usually represented by a stylized target. In this recipe, we will learn how to implement custom mouse cursor icons to better illustrate your gameplay or just to escape the Windows, macOS, and Linux default UI:


Getting ready

For this recipe, we have prepared the folders that you'll need in the 02_06 folder.

How to do it...

...

Setting custom mouse cursors for UI controls

The previous recipe demonstrated how to change the mouse pointer for 2D and 3D GameObjects receiving OnMouseEnter and OnMouseExit events. Unity UI controls do not receive OnMouseEnter and OnMouseExit events. Instead, UI controls can be made to respond to PointerEnter and PointerExit events if we add a special Event Trigger component to the UI GameObject. In this recipe, we'll change the mouse pointer to a custom magnifying glass cursor when it moves over a UI Button GameObject:

Getting ready

For this recipe, we'll use the same asset files as for the previous recipe, and its CustomCursorPointer C# script class, all of which can be found in the 02_07 folder.

...

Interactive text entry with an Input Field

While often we just wish to display non-interactive text messages to the user, there are times (such as name entry for high scores) where we want the user to be able to enter text or numbers into our game. Unity provides the UI Input Field component for this purpose. In this recipe, we'll create an Input Field that prompts the user to enter their name:

Having interactive text on the screen isn't of much use unless we can retrieve the text entered to use in our game logic, and we may need to know each time the user changes the text content and act accordingly. This recipe adds an event-handler C# script that detects each time the user has completed editing the text, and updates an extra message on screen confirming the newly-entered content.

...

Toggles and radio buttons via Toggle Groups

Users make choices, and often, these choices have one of two options (for example, sound on or off), or sometimes one of several possibilities (for example, difficulty level as easy/medium/hard). Unity UI Toggles allows users to turn options on and off; and when combined with Toggle Groups, they restrict choices to one of the group of items. In this recipe, we'll first explore the basic Toggle, and a script to respond to a change in values. Then, we'll extend the example to illustrate Toggle Groups, and styling these with round images to make them look more like traditional radio buttons.

The screenshot shows how the button's status changes are logged in the Console panel when the scene is running:

Getting ready

...

Creating text and image icon UI Dropdown menus

In the previous recipe, we created radio-style buttons with a Toggle Group, to present the user with a choice of one of many options. Another way to offer a range of choices is with a drop-down menu. Unity provides the UI Dropdown control for such menus. In this recipe, we'll offer the user a drop-down choice for the suit of a deck of cards (hearts, clubs, diamonds, or spades).

Note, the UI Dropdown created by default includes a scrollable area, in case there isn't space for all the options. We'll learn how to remove the GameObjects and components, to reduce complexity when such a feature is not required.

Then we'll learn how to add icon images with each menu option, as shown in the screenshot:

Getting ready

...

Displaying a radar to indicate the relative locations of objects

A radar displays the locations of other objects relative to the player, usually based on a circular display, where the center represents the player, and each graphical blip indicates how far away and what relative direction objects are to the player. Sophisticated radar displays will display different categories of objects with different colored or shaped blip icons.

In the screenshot, we can see two red square blips, indicating the relative position of the two red cube GameObjects tagged Cube near the player, and a yellow circle blip indicating the relative position of the yellow sphere GameObject tagged Sphere. The green circle radar background image gives the impression of an aircraft control tower radar or something similar:

...
Left arrow icon Right arrow icon

Key benefits

  • Become proficient at traditional 2D and 3D game development
  • Build amazing interactive interfaces with Unity's UI system
  • Develop professional games with realistic animation and graphics, materials and cameras, and AI with Unity 2018

Description

With the help of the Unity 2018 Cookbook, you’ll discover how to make the most of the UI system and understand how to animate both 2D and 3D characters and game scene objects using Unity's Mecanim animation toolsets. Once you’ve got to grips with the basics, you will familiarize yourself with shaders and Shader Graphs, followed by understanding the animation features to enhance your skills in building fantastic games. In addition to this, you will discover AI and navigation techniques for nonplayer character control and later explore Unity 2018’s newly added features to improve your 2D and 3D game development skills. This book provides many Unity C# gameplay scripting techniques. By the end of this book, you'll have gained comprehensive knowledge in game development with Unity 2018.

Who is this book for?

Unity 2018 Cookbook is for you if you want to explore a wide range of Unity scripting and multimedia features and find ready-to-use solutions for many game features. This book also helps programmers explore multimedia features. It is assumed that you already know basics of Unity and have some programming knowledge of C#.

What you will learn

  • Get creative with Unity's shaders and learn to build your own shaders with the new Shader Graph tool
  • Create a text and image character dialog with the free Fungus Unity plugin
  • Explore new features integrated into Unity 2018, including TextMesh Pro and ProBuilder
  • Master Unity audio, including ducking, reverbing, and matching pitch to animation speeds
  • Work with the new Cinemachine and timeline to intelligently control camera movements
  • Improve ambiance through the use of lights and effects, including reflection and light probes
  • Create stylish user interfaces with the UI system, including power bars and clock displays

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 31, 2018
Length: 794 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788477284
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 31, 2018
Length: 794 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788477284
Vendor :
Unity Technologies
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 8,114.97
Unity 2018 Cookbook
₱3112.99
Unity 2018 By Example
₱2500.99
Unity 2018 Artificial Intelligence Cookbook
₱2500.99
Total 8,114.97 Stars icon

Table of Contents

21 Chapters
Displaying Data with Core UI Elements Chevron down icon Chevron up icon
Responding to User Events for Interactive UIs Chevron down icon Chevron up icon
Inventory UIs Chevron down icon Chevron up icon
Playing and Manipulating Sounds Chevron down icon Chevron up icon
Creating Textures, Maps, and Materials Chevron down icon Chevron up icon
Shader Graphs and Video Players Chevron down icon Chevron up icon
Using Cameras Chevron down icon Chevron up icon
Lights and Effects Chevron down icon Chevron up icon
2D Animation Chevron down icon Chevron up icon
3D Animation Chevron down icon Chevron up icon
Webserver Communication and Online Version-Control Chevron down icon Chevron up icon
Controlling and Choosing Positions Chevron down icon Chevron up icon
Navigation Meshes and Agents Chevron down icon Chevron up icon
Design Patterns Chevron down icon Chevron up icon
Editor Extensions and Immediate Mode GUI (IMGUI) Chevron down icon Chevron up icon
Working with External Resource Files and Devices Chevron down icon Chevron up icon
Working with Plain Text, XML, and JSON Text Files Chevron down icon Chevron up icon
Virtual Reality and Extra Features Chevron down icon Chevron up icon
Automated Testing Chevron down icon Chevron up icon
Bonus Chapters Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(3 Ratings)
5 star 66.7%
4 star 0%
3 star 0%
2 star 0%
1 star 33.3%
Hanseung Chae Nov 12, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It is a great book that if you are just starting Unity. Also, it has some contents valid to the old user who had never touched Unity after 5 released. There is a good solution how to handle UI stuffs and optimizing.
Amazon Verified review Amazon
Amazon Customer Oct 31, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Delivered on time, perfect
Amazon Verified review Amazon
Maury Amezcua Jan 08, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Nothing against the authors of the books but the Publisher Packt. All these "books" with this cover format are actually written specifically for a online learning database, so the pictures in the book are really small and the project files mentioned in the text aren't included with the Amazon purchase. It's like the publisher just went through each lesson and converted the pages to PDF, then turned them into books to sell on Amazon.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.