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
Free Learning
Arrow right icon
Unity 5.x Cookbook
Unity 5.x Cookbook

Unity 5.x Cookbook: More than 100 solutions to build amazing 2D and 3D games with Unity

eBook
R$80 R$271.99
Paperback
R$339.99
Subscription
Free Trial
Renews at R$50p/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 5.x Cookbook

Chapter 2. Inventory GUIs

In this chapter, we will cover the following topics:

  • Creating a simple 2D mini-game – SpaceGirl
  • Displaying single object pickups with carrying and not-carrying text
  • Displaying single object pickups with carrying and not-carrying icons
  • Displaying multiple pickups of the same object with text totals
  • Displaying multiple pickups of the same object with multiple status icons
  • Revealing icons for multiple object pickups by changing the size of a tiled image
  • Displaying multiple pickups of different objects as a list of text via a dynamic List<> of PickUp objects
  • Displaying multiple pickups of different objects as text totals via a dynamic Dictionary<> of PickUp objects and "enum" pickup types
  • Generalizing multiple icon displays using UI Grid Layout Groups (with scrollbars!)

Introduction

Many games involve the player collecting items or choosing from a selection of items. Examples could be collecting keys to open doors, collecting ammo for weapons, choosing from a collection of spells to cast, and so on.

The recipes in this chapter offer a range of solutions for displaying to the player whether they are carrying an item or not, if they are allowed more than one of an item, and how many they have.

The big picture

The two parts of software design for implementing inventories relate to, first, how we choose to represent the data about inventory items (that is, the data types and structures to store the data) and, secondly, how we choose to display information about inventory items to the player (the UI: User Interface).

Also, whilst not strictly inventory items, player properties such as lives left, health, or time remaining can also be designed around the same concepts that we present in this chapter.

We need to first think about the nature of different inventory...

Creating a simple 2D mini-game – SpaceGirl

This recipe presents the steps to create the 2DSpaceGirl mini-game, on which all the recipes of this chapter are based.

Getting ready

For this recipe, we have prepared the images you need in a folder named Sprites in the 1362_02_01 folder. We have also provided the completed game as a Unity package in this folder named Simple2DGame_SpaceGirl.

How to do it...

To create the simple 2D mini-game Space Girl follow these steps:

  1. Create a new, empty 2D project.
  2. Import supplied folder Sprites into your project.
  3. Convert each sprite image to be of type Sprite (2D and UI). To do this, select the sprite in the Project panel, then, in the Inspector, change choose Sprite (2D and UI) from the drop-down menu Texture Type, and click on the Apply button, as shown in the following screenshot:
    How to do it...
  4. Set the Unity Player screen size to 800 x 600: choose the Edit | Project Settings | Player menu, then for option Resolution and Presentation uncheck Default is Full Screen, and...

Displaying single object pickups with carrying and not-carrying text

Often the simplest inventory situation is to display text to tell players if they are carrying a single item (or not).

Getting ready

This recipe assumes that you are starting with the project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter. So, either make a copy of that project or do the following:

  1. Create a new, empty 2D project.
  2. Import the Simple2Dgame_SpaceGirl package.
  3. Open scene Scene1 (in the Scenes folder).
  4. Set the Unity Player screen size to 800 x 600 (see the previous recipe for how to do this) and select this resolution in the Game panel the drop-down menu.
  5. Convert each sprite image to be of type Sprite (2D and UI). In the Inspector, choose Sprite (2D and UI) from drop-down menu Texture Type, and click on the Apply button.

For this recipe, we have prepared the font you need in a folder named Fonts in the 1362_02_02 folder.

How to do it...

To display text to inform the user about the status of carrying...

Displaying single object pickups with carrying and not-carrying icons

Graphic icons are an effective way to inform the player that they are carrying an item. In this recipe, if no star is being carried, a grey-filled icon in a blocked-off circle is displayed; then, after the star has been picked up, a yellow-filled icon is displayed, as shown in the following screenshot.

Displaying single object pickups with carrying and not-carrying icons

In many cases, icons are clearer (they don't require reading and thinking about) and can also be smaller onscreen than text messages for indicating player status and inventory items.

Getting ready

This recipe assumes that you are starting with the project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter.

How to do it...

To toggle carrying and not-carrying icons for a single object pickup, follow these steps:

  1. Start with a new copy of the mini-game Simple2Dgame_SpaceGirl.
  2. In the Hierarchy panel, add a new UI Image object (Create | UI | Image). Rename it Image-star-icon.
  3. Select Image-star-icon in the Hierarchy...

Displaying multiple pickups of the same object with text totals

When several items of the same type have been picked up, often the simplest way to convey what is being carried to the user is to display a text message showing the numeric total of each item type being carried, as shown in the following screenshot. In this recipe, the total number of stars collected is displayed using a UI Text object.

Displaying multiple pickups of the same object with text totals

Getting ready

This recipe assumes you are starting with project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter. The font you need can be found in folder 1362_02_02.

How to do it...

To display inventory total text for multiple pickups of same type of object, follow these steps:

  1. Start with a new copy of the mini-game Simple2Dgame_SpaceGirl.
  2. Add a UI Text object (Create | UI | Text). Rename it Text-carrying-star. Change its text to stars = 0.
  3. Import the provided Fonts folder into your project.
  4. In the Inspector panel, set the font of Text-carrying-star to Xolonium-Bold (folder Fonts...

Displaying multiple pickups of the same object with multiple status icons

If there is a small, fixed total number of an item to be collected rather than text totals, an alternative effective UI approach is to display placeholder icons (empty or greyed out pictures) to show the user how many of the item remain to be collected, and each time an item is picked up, a placeholder icon is replaced by a full color collected icon.

In this recipe, we use grey-filled star icons as the placeholders and yellow-filled star icons to indicate each collected star, as shown in the following screenshot.

Since our UI code is getting a little more complicated, this recipe will implement the MVC design pattern to separate the view code from the core player logic (as introduced at the end of recipe Displaying single object pickups with carrying and not-carrying text).

Displaying multiple pickups of the same object with multiple status icons

Getting ready

This recipe assumes that you are starting with the project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter.

How to...

Introduction


Many games involve the player collecting items or choosing from a selection of items. Examples could be collecting keys to open doors, collecting ammo for weapons, choosing from a collection of spells to cast, and so on.

The recipes in this chapter offer a range of solutions for displaying to the player whether they are carrying an item or not, if they are allowed more than one of an item, and how many they have.

The big picture

The two parts of software design for implementing inventories relate to, first, how we choose to represent the data about inventory items (that is, the data types and structures to store the data) and, secondly, how we choose to display information about inventory items to the player (the UI: User Interface).

Also, whilst not strictly inventory items, player properties such as lives left, health, or time remaining can also be designed around the same concepts that we present in this chapter.

We need to first think about the nature of different inventory...

Creating a simple 2D mini-game – SpaceGirl


This recipe presents the steps to create the 2DSpaceGirl mini-game, on which all the recipes of this chapter are based.

Getting ready

For this recipe, we have prepared the images you need in a folder named Sprites in the 1362_02_01 folder. We have also provided the completed game as a Unity package in this folder named Simple2DGame_SpaceGirl.

How to do it...

To create the simple 2D mini-game Space Girl follow these steps:

  1. Create a new, empty 2D project.

  2. Import supplied folder Sprites into your project.

  3. Convert each sprite image to be of type Sprite (2D and UI). To do this, select the sprite in the Project panel, then, in the Inspector, change choose Sprite (2D and UI) from the drop-down menu Texture Type, and click on the Apply button, as shown in the following screenshot:

  4. Set the Unity Player screen size to 800 x 600: choose the Edit | Project Settings | Player menu, then for option Resolution and Presentation uncheck Default is Full Screen, and set the...

Displaying single object pickups with carrying and not-carrying text


Often the simplest inventory situation is to display text to tell players if they are carrying a single item (or not).

Getting ready

This recipe assumes that you are starting with the project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter. So, either make a copy of that project or do the following:

  1. Create a new, empty 2D project.

  2. Import the Simple2Dgame_SpaceGirl package.

  3. Open scene Scene1 (in the Scenes folder).

  4. Set the Unity Player screen size to 800 x 600 (see the previous recipe for how to do this) and select this resolution in the Game panel the drop-down menu.

  5. Convert each sprite image to be of type Sprite (2D and UI). In the Inspector, choose Sprite (2D and UI) from drop-down menu Texture Type, and click on the Apply button.

For this recipe, we have prepared the font you need in a folder named Fonts in the 1362_02_02 folder.

How to do it...

To display text to inform the user about the status of carrying...

Displaying single object pickups with carrying and not-carrying icons


Graphic icons are an effective way to inform the player that they are carrying an item. In this recipe, if no star is being carried, a grey-filled icon in a blocked-off circle is displayed; then, after the star has been picked up, a yellow-filled icon is displayed, as shown in the following screenshot.

In many cases, icons are clearer (they don't require reading and thinking about) and can also be smaller onscreen than text messages for indicating player status and inventory items.

Getting ready

This recipe assumes that you are starting with the project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter.

How to do it...

To toggle carrying and not-carrying icons for a single object pickup, follow these steps:

  1. Start with a new copy of the mini-game Simple2Dgame_SpaceGirl.

  2. In the Hierarchy panel, add a new UI Image object (Create | UI | Image). Rename it Image-star-icon.

  3. Select Image-star-icon in the Hierarchy panel...

Displaying multiple pickups of the same object with text totals


When several items of the same type have been picked up, often the simplest way to convey what is being carried to the user is to display a text message showing the numeric total of each item type being carried, as shown in the following screenshot. In this recipe, the total number of stars collected is displayed using a UI Text object.

Getting ready

This recipe assumes you are starting with project Simple2Dgame_SpaceGirl setup from the first recipe in this chapter. The font you need can be found in folder 1362_02_02.

How to do it...

To display inventory total text for multiple pickups of same type of object, follow these steps:

  1. Start with a new copy of the mini-game Simple2Dgame_SpaceGirl.

  2. Add a UI Text object (Create | UI | Text). Rename it Text-carrying-star. Change its text to stars = 0.

  3. Import the provided Fonts folder into your project.

  4. In the Inspector panel, set the font of Text-carrying-star to Xolonium-Bold (folder Fonts)...

Left arrow icon Right arrow icon

Key benefits

  • Built on the solid foundation of the popular Unity 4.x Cookbook, the recipes in this edition have been completely updated for Unity 5
  • Features recipes for both 2D and 3D games
  • Provides you with techniques for the new features of Unity 5, including the new UI system, 2D game development, new Standard Shaders, and the new Audio Mixer

Description

Unity 5 is a flexible and intuitive multiplatform game engine that is becoming the industry's de facto standard. Learn to craft your own 2D and 3D computer games by working through core concepts such as animation, audio, shaders, GUI, lights, cameras, and scripting to create your own games with one of the most important and popular engines in the industry. Completely re-written to cover the new features of Unity 5, this book is a great resource for all Unity game developers, from those who have recently started using Unity right up to game development experts. The first half of the book focuses on core concepts of 2D game design while the second half focuses on developing 3D game development skills. In the first half, you will discover the new GUI system, the new Audio Mixer, external files, and animating 2D characters in 2D game development. As you progress further, you will familiarize yourself with the new Standard Shaders, the Mecanim system, Cameras, and the new Lighting features to hone your skills towards building 3D games to perfection. Finally, you will learn non-player character control and explore Unity 5's extra features to enhance your 3D game development skills.

Who is this book for?

This book is for anyone who wants to explore a wide range of Unity scripting and multimedia features, and find ready-to-use solutions for many game features. Programmers can explore multimedia features, and multimedia developers can try their hand at scripting. *From intermediate to advanced users, from artists to coders, this book is for you, and everyone on your team! *It is intended for everyone who has the basics of using Unity, and a little programming knowledge in C#.

What you will learn

  • Immerse players with great audio, utilizing Unity 5 s audio features including the new Audio Mixer, ambient sound with Reverb Zones, dynamic soundtracks with Snapshots, and balanced audio via Ducking
  • Create better materials with Unity s new, physically-based, Standard Shader
  • Measure and control time, including pausing the game, displaying clocks and countdown timers, and even implementing “bullet timeâ€? effects
  • Improve ambiance through the use of lights and effects such as reflection and light probes
  • Create stylish user interfaces with the new UI system, including power-bars, clock displays, and an extensible inventory system
  • Save and load text and media assets from local or remote sources, publish your game via Unity Cloud, and communicate with websites and their databases to create online scoreboards
  • Discover advanced techniques, including the publisher-subscriber and state patterns, performance bottleneck identification, and methods to maximize game performance and frame rates
  • Control 2D and 3D character movement, and use NavMeshAgents to write NPC and enemy behaviors such as seek, flee, flock, and waypoint path following

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 05, 2015
Length: 570 pages
Edition : 1st
Language : English
ISBN-13 : 9781784391362
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 : Oct 05, 2015
Length: 570 pages
Edition : 1st
Language : English
ISBN-13 : 9781784391362
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 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
R$500 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 R$25 each
Feature tick icon Exclusive print discounts
R$800 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 R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 919.97
Unity 5.x Cookbook
R$339.99
Unity UI Cookbook
R$306.99
Unity 5  Game Optimization
R$272.99
Total R$ 919.97 Stars icon
Banner background image

Table of Contents

13 Chapters
1. Core UI – Messages, Menus, Scores, and Timers Chevron down icon Chevron up icon
2. Inventory GUIs Chevron down icon Chevron up icon
3. 2D Animation Chevron down icon Chevron up icon
4. Creating Maps and Materials Chevron down icon Chevron up icon
5. Using Cameras Chevron down icon Chevron up icon
6. Lights and Effects Chevron down icon Chevron up icon
7. Controlling 3D Animations Chevron down icon Chevron up icon
8. Positions, Movement and Navigation for Character GameObjects Chevron down icon Chevron up icon
9. Playing and Manipulating Sounds Chevron down icon Chevron up icon
10. Working with External Resource Files and Devices Chevron down icon Chevron up icon
11. Improving Games with Extra Features and Optimization Chevron down icon Chevron up icon
12. Editor Extensions Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.8
(17 Ratings)
5 star 88.2%
4 star 5.9%
3 star 0%
2 star 5.9%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




real7a May 06, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's a good practical book on Unity game development and in the real world nowadays practice goes to production is very fast :) I've started to read it half a Year ago and started with several games and VR projects very fast. So the review is partial and delayed and anyway :)In short it's strongly recommended to jump start and get some vision of the landscape of Unity development.The book is organized as a cookbook as titled "Unity 5.x Cookbook" and it's the series of tutorial lessons actually. Some of lessons are easy and kind of artificial and they are used to show core concepts and lead to real useful tasks.I liked the tutorial of map radar for the shooter game. I made it and even submitted errata that was accepted.There are a lot of more useful lessons there. If You have time to learn it's very good addition to Unity tutorials and official Unity documentation.
Amazon Verified review Amazon
Nicole.Garrow Jan 02, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very helpful in game making!
Amazon Verified review Amazon
ruben Dec 26, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Unity 5.x Cookbook By Ruben Oliva RamosI liked this book, it has a big deal that I could understand the objective of the book, I understood all the chapters, The book has a new content that we can develep our own development.This is an excelent book, because covers my needs in game programming.Ruben
Amazon Verified review Amazon
Tim Crothers Dec 23, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I love that this book is jam packed with practical and useful examples. All of the example code, images and other files are a free download. Seems to me that just finding one or two of the recipes useful would justify the cost of the book. In my case I was able to adapt several of them for inclusion in my projects and gained a ton of value.
Amazon Verified review Amazon
Adam Maksym May 29, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Helpful Info..
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.