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
NGUI for Unity
NGUI for Unity

NGUI for Unity: The NGUI plugin for Unity makes user interfaces so much more efficient and attractive. Learn all about it in this step-by-step tutorial that includes lots of practical exercises, including creating a fun 2D game.

eBook
€8.99 €22.99
Paperback
€28.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

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

NGUI for Unity

Chapter 2. Creating Widgets

In this chapter, we will create our first sprite widget and understand how it works. Then we will create one sample of each important widget template, and analyze their corresponding parameters so that you know how to create and configure them.

At the end of this chapter, we will have a functional main menu with most of NGUI's widgets.

Creating our first widget


We will create our first sprite widget to display our main menu's background window. In order to do that easily, NGUI has a Widget Wizard with a few templates for us.

Widget Wizard

The Widget Wizard can be opened by navigating to NGUI | Open | Widget Wizard. It will look like the following screenshot:

As you can see in the previous screenshot, to create a widget, you require Atlas and Font to be configured. As said in Chapter 1, Getting Started with NGUI, an atlas is a large texture containing the sprites that you need to create your UI. For the rest of this chapter, we will use the default atlas named SciFi Atlas , which is included in the plugin.

Selecting an atlas

Let's select our default SciFi atlas, which contains the necessary sprites, as follows:

  1. In the Project view, navigate to Assets | NGUI | Examples | Atlases | SciFi.

  2. Drag-and-drop the prefab SciFi Atlas.prefab in the Atlas field.

  3. Drag-and-drop the prefab SciFi Font – Header.prefab in the Font field.

We have...

Sprites


We have created our first sprite and understood how to move, rotate, and change its dimensions. We actually scaled it up massively compared to its original size. But this 15 x 15 sprite has something special.

If you select the Sprite (Dark) GameObject, you will see it has four dotted lines inside the Preview window in the Inspector view. This means it's a sliced sprite:

Sliced sprites

A sliced sprite is an image divided in nine sections, making it resizable while conserving its corners' proportions. Sliced sprites may be scaled as you wish—they still look beautiful.

Since Sprite (Dark) is a 9-slice sprite, we must tell our UISprite component to treat it as such. Perform the following steps to do so:

  1. Select the Sprite (Dark) GameObject.

  2. Click on the drop-down menu next to its Sprite Type field.

  3. Select Sliced.

Notice how the sprite looks good now—it's not stretched anymore!

Note

The Fill Center parameter allows you to only show the sprite's borders and leave only transparency inside.

Even though...

Labels


Labels are used to display text on the screen with a specific font as shown in the following screenshot:

Let's create a label widget with the following steps:

  1. Select the Panel GameObject.

  2. Navigate to NGUI | Open | Widget Wizard.

  3. Select the Label template.

  4. We have already selected a font; it will be used for this label.

  5. Click on the Add To button.

A new label has been added to the panel and placed at the center of the screen.

Parameters

With Label selected, the UILabel parameters are shown in the Inspector view. They are as follows:

  • Text: This is a large textbox that lets you type the text to be displayed.

  • Overflow: This offers four different behaviors for the widget when the text is larger than the label's Dimensions. The four behaviors are as follows:

    • Shrink Content: This shrinks the text in order to fit

    • Clamp Content: This ensures overflow will never happen

    • Resize Freely: This resizes to display all the content and overflow

    • Resize Height: This resizes height only—column style

  • Encoding: This...

Buttons


With NGUI, buttons are easy to create and configure.

Let's create our first one by performing the following steps:

  1. Select the Panel GameObject.

  2. Create a new child with Alt + Shift + N and perform the following steps:

    1. Rename it as Buttons. It will be our buttons container.

  3. Navigate to NGUI | Open | Widget Wizard and perform the following steps:

    1. Select the Button Template.

    2. For the Background field, select the sprite named Button.

  4. With the Buttons GameObject selected, click on the Add To button.

A button has just been created and centered on the screen. If you look at the Hierarchy view, you will see that a button is composed of a container GameObject named Button and two children: a Background sprite and Label. That's how NGUI works; templates are simply assembled components and widgets. If you wanted to, you could build a button from scratch using the right components on empty GameObjects.

Click on the Play button. You can see that hover and click are already set! Turn off the Play mode, select...

Text input


Now we will learn how to add a text input to create a nickname box. Perform the following steps to do so:

  1. Select the Panel GameObject and create a new child with Alt + Shift + N. Then rename this new child as Nickname. It will be our nickname box container.

  2. Navigate to NGUI | Open | Widget Wizard.

    1. From the Project view, navigate to NGUI | Examples | Atlases | SciFi.

    2. Drag the SciFi Font – Normal prefab into the Font field.

    3. Select the Input template.

    4. Set Sprite (Dark) as the Background parameter.

  3. With the Nickname GameObject selected, click on the Add To button.

A new widget named Input has been added to the scene.

Parameters

An Input GameObject has been created. Let's look at its Inspector parameters:

  • Input Label: This is the text label that is to be used for this input.

  • Inactive Color: This is the text color while the text is not selected.

  • Active Color: This is the text color while the text is being edited.

  • Default Text: This is either Blank or with the label's default text. Blank will...

Slider


Now let's add a volume slider for the user to move and select his/her volume level.

A slider template is available, enabling you to adjust parameters easily by sliding a thumb along a bar. Perform the following steps to create a volume slider:

  1. Select the Panel GameObject and create a new child with Alt + Shift + N.

  2. Rename that new child as Volume. It will be our volume settings container.

  3. Navigate to NGUI | Open | Widget Wizard and perform the following steps:

    1. Select the Slider template.

    2. Set the Dark Sprite to Empty.

    3. Set the Light Sprite to Full.

    4. Set Highlight to Thumb.

  4. With the Volume GameObject selected, click on the Add To button.

Parameters

A slider has been created. It has 6 parameters as follows:

  • Value: This is the slider's current value, which is between 0 and 1.

  • Steps: This is the number of steps to completely fill or empty the slider.

  • Direction: This is the slider's fill direction, either Horizontal or Vertical.

  • Foreground: This is the sprite used to fill the slider.

  • Thumb: This is...

Toggle


Now that we have a volume slider, let's add an enable/disable sound checkbox, which will turn down the volume to 0 and hide our volume slider.

First, create a toggle widget as follows:

  1. Select the Panel GameObject and create a new child with Alt + Shift + N.

  2. Rename that new child as Sound. It will be our sound toggle container.

  3. Navigate to NGUI | Open | Widget Wizard and perform the following steps:

    1. Select Toggle as Template.

    2. Select the Dark Sprite as Background.

    3. Select the X Sprite as Checkmark.

    4. With the Sound container selected, click on the Add To button.

A checkbox with a label has just been created as shown in the following screenshot:

Parameters

Select our new Toggle GameObject. Let's look at the UIToggle's Inspector parameters:

  • Group: This is the toggle's group. Toggles of the same group will act as radio buttons; only one of them can be checked at once.

  • Start State: This defines in which state the toggle will be at the start.

  • Animation: This is the animation that will play when the checkbox...

Popup list


We will now learn how to create the popup list, see its parameters, and create a difficulty selector for our game.

  1. Select the Panel GameObject and create a new child with Alt + Shift + N.

  2. Rename that new child as Difficulty. It's our difficulty box container.

  3. Navigate to NGUI | Open | Widget Wizard and perform the following steps:

    1. Select Popup List as Template

    2. Select Dark as Foreground.

    3. Select Dark as Background.

    4. Select the Highlight sprite as Highlight.

  4. With our Difficulty GameObject selected, click on the Add To button.

Parameters

A Popup List GameObject has just been created. Let's look at its parameters:

  • Atlas: This is the atlas used for the popup list's sprites.

  • Font: This is the font used for the popup list's options.

  • Text Label: This is the label to update when Popup List changes selection.

  • Options: This is the list of options that will pop up—one per line.

  • Default: This is the option selected at start.

  • Position: You may force the list of options to appear Above or Below the Popup...

Summary


In this chapter, we have learned how to create and configure most of NGUI's widgets—sprites, labels, buttons, text inputs, sliders, toggles, and popup lists.

We now have a main menu with interactive elements. We also used NGUI's notification event system to change variables in code and register the user's choices.

We used UIAnchors and UIStretch components to properly position our widgets—we just have to move each box's background sprite to move the entire element. This is much more effective than having to move each GameObject manually! You should have a main menu that looks like the following screenshot:

Ok, now it is time to enhance our UI experience and make this better. Let's move on to Chapter 3, Enhancing your UI.

Left arrow icon Right arrow icon

What you will learn

  • Download and import NGUI
  • Construct a basic user interface: buttons, sprites, text inputs, lists, sliders, and more
  • Enhance your UI using clipping, draggable panels, content alignment, animations, and localization
  • Use C# with NGUI to save options, implement volume modification, launch animations, and change variables
  • Build a scrollable viewport with a draganddrop system
  • Create tooltips and notifications
  • Add your sprites and fonts to customize your main menu
  • Move elements through code and handle collisions to create a game
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 24, 2014
Length: 182 pages
Edition :
Language : English
ISBN-13 : 9781783558667
Vendor :
Unity Technologies
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Latvia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Publication date : Jan 24, 2014
Length: 182 pages
Edition :
Language : English
ISBN-13 : 9781783558667
Vendor :
Unity Technologies
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 108.97
Unity 4.x Game Development by Example: Beginner's Guide
€37.99
NGUI for Unity
€28.99
Unity Character Animation with Mecanim
€41.99
Total 108.97 Stars icon
Banner background image

Table of Contents

7 Chapters
Getting Started with NGUI Chevron down icon Chevron up icon
Creating Widgets Chevron down icon Chevron up icon
Enhancing your UI Chevron down icon Chevron up icon
C# with NGUI Chevron down icon Chevron up icon
Building a Scrollable Viewport Chevron down icon Chevron up icon
Atlas and Font Customization Chevron down icon Chevron up icon
Creating a Game with NGUI Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9
(14 Ratings)
5 star 42.9%
4 star 21.4%
3 star 28.6%
2 star 0%
1 star 7.1%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Feb 12, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Full Disclosure - I was one of the technical editors for this book.NGUI for Unity provides a step-by-step guide to help you get started using NGUI in Unity. The author explains how each component works and demonstrates proper use of the most commonly used NGUI widgets. The author also walks you through the creation of a 2D game, complete with menu screen and drag-drop elements, as you progress through the book.
Amazon Verified review Amazon
Frida Feb 20, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Im new to NGUI, complete beginner ,The book take you step by step , shows you how to create UI , explain how to use the component with good examples , Widgets , how to create new atlas.Ngui is a great UI system , you can buy it on the Asset store and it's decently wort it.The book is easy to read, very clear , very organized , easy to follow .Now, i understand how to use NGUIi recommend this book book .excellent book !!!Frida
Amazon Verified review Amazon
John Grden Feb 09, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
After interviewing with a few unity3D shops,I was hit with the same question every single time: "do you have NGUI experience?" - to which I replied "no, but I'm sure I can pick it up". They didn't share my optimism :) Enter in NGUI for Unity!What I love about e-books these days is their size and how concise they can be, and this book delivers not only on both counts, but sets you in motion with a very real world example project that covers a great portion of the capabilities of NGUI. Everything from the wizards to widgets to creating atlas' and fonts is covered. The book isn't exhaustive in its explanations but IS thorough in pointing out the features and properties of all the components it covers - which makes it easy to follow along with in the examples.The author gets right to the meat of the matter immediately walking you step by step through the sample project. It helps to have Unity3D experience and you'll have to understand the basics of how unity works with prefabs and code as this book is targeting unity devs looking for a solid UI framework. It IS written in a very practical manner so even a complete beginner could get something out of it.Now, in thinking back about my response to potential clients about just "picking NGUI up", I see how deep the framework really is. It's much more than just a set of UI components, and this book really brought me up to speed in a very short amount if time - excellent work!
Amazon Verified review Amazon
Xavier Valade Mar 23, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
NGUI is an amazing plugin that makes creating UI and 2D games really easy. This book is THE book to have if you want to discover and master NGUI.It’s perfect for everyone, no matter what their Unity skills are. Beginners can follow clear step by step explanations while professionals can take it to the next level with the advanced features, the complete overview of the different parameters and the many tips.
Amazon Verified review Amazon
Joselyn O'Connor Feb 09, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Finished reading NGUI for Unity a few days ago. Since, I've been skimming through it using it as a reference. I absolutely appreciated that the author was brief and concise in his examples, it made them a most valuable resource while working. It was very easy to read and has given me a much better understanding of NGUI. The resources that are provided with the book are also very well organised, and make for an excellent starting point while tinkering. I definitely recommend this book!
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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela