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
Conferences
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.

Arrow left icon
Profile Icon Charles Bernardoff (EURO)
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (14 Ratings)
Paperback Jan 2014 182 pages Edition
eBook
$9.99 $22.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Charles Bernardoff (EURO)
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.9 (14 Ratings)
Paperback Jan 2014 182 pages Edition
eBook
$9.99 $22.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $22.99
Paperback
$38.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

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

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 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 : 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
$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 $ 142.97
Unity 4.x Game Development by Example: Beginner's Guide
$48.99
NGUI for Unity
$38.99
Unity Character Animation with Mecanim
$54.99
Total $ 142.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 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.