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
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
Paperback Aug 2018 794 pages 3rd Edition
eBook
$32.99 $47.99
Paperback
$60.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Matt Smith Profile Icon Francisco Queiroz
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7 (3 Ratings)
Paperback Aug 2018 794 pages 3rd Edition
eBook
$32.99 $47.99
Paperback
$60.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$32.99 $47.99
Paperback
$60.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781788471909
Vendor :
Unity Technologies
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 31, 2018
Length: 794 pages
Edition : 3rd
Language : English
ISBN-13 : 9781788471909
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 $5 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 $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 158.97
Unity 2018 Cookbook
$60.99
Unity 2018 By Example
$48.99
Unity 2018 Artificial Intelligence Cookbook
$48.99
Total $ 158.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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.