Book Image

Unity 5.x Cookbook

Book Image

Unity 5.x Cookbook

Overview of this book

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.
Table of Contents (20 chapters)
Unity 5.x Cookbook
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Displaying a "Hello World" UI text message


The first traditional problem to be solved with a new computing technology is often to display the Hello World message. In this recipe, you'll learn to create a simple UI Text object with this message, in large white text with a selected font, and in the center of the screen.

Getting ready

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

How to do it...

To display a Hello World text message, follow these steps:

  1. Create a new Unity 2D project.

  2. Import the provided Fonts folder.

  3. In the Hierarchy panel, add a UI | Text GameObject to the scene – choose menu: GameObject | UI | Text. Name this GameObject Text-hello.

    Note

    Alternatively, use the Create menu immediately below the Hierarchy tab, choosing menu: Create | UI | Text.

  4. Ensure that your new Text-hello GameObject is selected in the Hierarchy panel. Now, in the Inspector, ensure the following properties are set:

    • Text set to read Hello World

    • Font set to Xolonium-Bold

    • Font size as per your requirements (large—this depends on your screen—try 50 or 100)

    • Alignment set to horizontal and vertical center

    • Horizontal and Vertical Overflow set to Overflow

    • Color set to white

    The following screenshot shows the Inspector panel with these settings:

  5. Now, in the Rect Transform, click on the Anchor Presets square icon, which should result in several rows and columns of preset position squares appearing. Hold down SHIFT and ALT and click on the center one (row middle and column center).

  6. Your Hello World text will now appear, centered nicely in the Game panel.

How it works...

You have added a new Text-hello GameObject to a scene. A parent Canvas and UI EventSystem will also have been automatically created.

You set the text content and presentation properties, and use the Rect Transform anchor presets to ensure that whatever way the screen is resized, the text will stay horizontally and vertically centered.

There's more...

Here are some more details that you don't want to miss.

Styling substrings with Rich Text

Each separate UI Text component can have its own color, size, boldness styling, and so on. However, if you wish to quickly add some highlighting style to a part of a string to be displayed to the user, the following are examples of some of the HTML-style markups that are available without the need to create separate UI Text objects:

  • Embolden text with the "b" markup: I am <b>bold</b>

  • Italicize text with the "i" markup: I am <i>italic</i>

  • Set the text color with hex values or a color name: I am <color=green>green text</color>, but I am <color=#FF0000>red</color>

    Note

    Learn more from the Unity online manual Rich Text page at: http://docs.unity3d.com/Manual/StyledText.html.