Unity New GUI Tutorial – Part I

Source: Internet
Author: User

Original address: Http://www.raywenderlich.com/78675/unity-new-gui-part-1
The Sharoth translation group: Proofreading: Glory.

There's no denying that unity is a great game engine, and in order to be able to meet the needs of independent developers, but also to provide high-quality AAA engine features, not surprisingly, unity now occupies the mainstream position

Unfortunately, every magician has his Achilles heel, and for unity its Achilles heel is the old GUI system.

The old GUI is a frustrating system that requires you to write all of your GUI code in Ongui, and Unity's user interface-centric nature is in contradiction to every project-centric programmer.

Now there are third-party tools like Ngui to solve this problem, but it's frustrating to have to spend more money on the area containing these basic third-party packages.

Well, in unity Technology, the small partners listen, after two years of development, they have provided a solution to eliminate all the pain, With the release of Unity4.6, you are now fully accessible to a new GUI system, and in this tutorial you will learn all about the new system, now.

In these three sections, you can use the Unity 2D in our original tutorial to do a game like a jet backpack.

Note: If you haven't finished the original tutorial, it doesn't matter, but it doesn't affect you looking back. If you look at the beginning of the tutorial, when you see the end of the tutorial, you will have a great functional game, not just a simple button scene project. And you will see how to migrate from the old GUI system to the new GUI system, which is priceless.

While you are doing this tutorial, you will be adding a classic menu interface to the game.

However, to master the new GUI system, just adding a few buttons is not enough, which requires adding something interesting to satisfy the user's desire for a seductive UI to animate a button to create a settings dialog that can be slid into the scene using more new GUI controls like text, Slider, Panel , Mask and so on if you want to seize the vanguard, download run this final project to see what you need to learn in this tutorial to start now

At the beginning you need some background images, buttons, and other UI elements, as well as the fonts for the buttons on the labels and text, for which you don't have to draw anything yourself or surf the Internet to find them, because I've prepared a package that contains everything you need.

You can download this package here Rocketmouse_gui_assets

This package contains background images, buttons, icons, or other game elements, which are made by my dear wife Svetlana.

You'll also find two font dafont.com here, thanks Rodrigo Fuenzalida (Titan one font) and Draghia Cornel (DCC Dreamer Font)

Mention that you will add a new scene to the project Rocket mouse in the previous tutorial, so download the final version of the project before you begin Rocketmouse_final

That's all you need, all right, besides the love song, are you listening to Beatles songs? create a button scene

Use the link above to download and unzip the project Rocket Mouse project. Select File\open Project ... To open the Unity project, locate the project's root directory.

Note: In addition, open this file in the project Assets\scenes and double-click the scene rocketmouse.unity to open unity.

After the project is loaded, you will see a scene you are familiar with or the scene is new to you-depending on whether you have completed the original tutorial. Whatever the situation, calm down and keep looking down.

Now create a new project and you'll spend a lot of time creating a new scene. In the menu, choose File\new Scene an empty new scenario

It is best to go directly to save this scene and choose File\save scene to open the Save Scene dialog box. Then, enter Menuscene as the name of the scene and save it to the Scenes file, placed underneath the rocketmouse scene

Take a look at the project to make sure there are two scenes inside.

Note: If you encounter any problems in opening a project, creating or saving a scene, or adding a resource file, browse through the original tutorial rocket Mouse tutorial. There is a step-by-step tutorial-There are pictures in GIF format that are not included in this tutorial, stick to dry principles Import Pictures and fonts

First you need to add resources to the scene in your to-do list, so unpack the resource bundle and you'll find two files: Menu and Fonts.

Select the two files and drag them into the assets file for your unity project.

Take a few seconds to process the files. Then you should see the file Fonts and menu in the Project browser, as shown below

Oh, whoa! Now that you've done the preparatory work, you can create your first UI element in the new GUI system. add your first UI element

The first element created in the scene is a background picture.

Select Gameobejct/ui/image in the menu. This adds an Image to the scene. You will see it inherit from canvas. All elements must be on canvas, so if you haven't, unity will provide you with a

To make sure you can see this picture in the scene view, select Image in hierarchy and set its X and Y position to 0.

Note: I have been able to hear these questions, "What is the position of X", "What is the position conversion component, what are they similar to?"
As you conduct this tutorial, you will find the answers to these questions. It is easy to learn from different objects, so that you can observe different settings behavior.

A property like a width or position x does not require any explanation.
In a moment you will set the correct position and dispel it. Now, there's another interesting thing. In hierarchy, you'll find three new objects in the scene.

1. Image
2. Canvas

3. EventSystem

Image is a non-interactive controller that displays sprite texture and offers many options to adjust.

For example, you can set the color of a picture or add footage to it. You can even control how many pictures are displayed. Animation like the clock rotation also brings vitality to it.

Canvas is a superclass of all UI elements, as previously shown, and is created automatically when you add your first UI element. It's also a lot of properties that allow you to control the rendering of your UI, and you'll see part of it in this tutorial.

EventSystem is responsible for handling input, mapping, and events in the scene, which requires the UI to be operational, so that's why it automatically joins in. set Menu Background

Now it's time to go back to the picture and the first thing to do is rename the image to Img_background.

Next, open the Menu file in the project browser to find the Menu_background picture. Drag it to the source Image area.

Now you have replaced the original white picture with a background image. However, it does not look like a suitable background disk because it is too small and the aspect ratio is incorrect
To fix this problem, find the Set Native size button in inspector and click it to set its size to 577 X

Now it's a picture of the right background.

However, there are a few questions:

1. The real size of the picture is 1136 x 640, just enough to cover 4 inches in full screen.

2. To narrow down your scene view, you will see that the canvas (white rectangle) only covers the part of the picture, and if you switch to the game view, you will see that it covers only part of the background image, as if the camera is too close to the picture to capture all of it.

Note: Originally this game was designed for iphone 3.5 inch and 4 inches which is why all game designs are supported by 1136 x 640 and 960 x 640. But then you'll see how the UI fits in different games.

The first question is the default import setting, and in the next chapter you will fix it. With this you will solve your second problem reference Resolution Component. Modify image Import Settings

In the project you need to write a slightly larger picture, in order to prevent unity from narrowing them down.

Open the menu file in the Project browser and select the Menu_background picture. In Inspector, select the Default tab, which is the maximum size of 2048, click Apply.

Now select Img_background in hierarchy, and then click Set Native Size again. The width and height should be the right thing, 1136 and 640.

Did you see it? It's really easy to solve and solve the next!

When you have a picture of the right background, it's time to adjust the way it appears.

First, the kind of display is not a bug. In the game view, you set the smaller size to 550x310, so it only shows part of the picture to fit the game view.

If you want to run the game on a device or simply stretch your game view to fit the entire image, you will see the entire background image.

While this unity is useful for most scenarios, it doesn't work when you need a different action. It's like you have a small monitor that doesn't fit into a high-resolution device.

In addition, many games support only one resolution.

Designers use this solution to determine size, location and other data, so when you develop the game, you need to make sure that the design is specific to avoid unnecessary calculations so that users can see exactly the right interface in anticipation.

If you've overlooked the design decisions, obviously you know that it's going to cost a lot, the user experience and the variety of solutions is important and you have to keep your designers happy

Fortunately, a special component comes in for an emergency. This component is called reference Resolution, and you need to add it to the canvas component.

Select Canvas in the hierarchy, and then click Add Component. Enter Reference Resolution to find the required component and click it when it is displayed to the list.

While adding this component, set his resolution to 1136 X 640. In addition, slide all the way to the appropriate width, the latter simply enter 1

After this change, you will immediately see all the background images in a small game view window in time.

Change the resolution of the game view to see how your games look in different resolutions, such as iPhone3.5 inches, fully adapted

Now that I switch to the scene view, you will see that the size of the canvas does not change when you re-size the scene view.

As you can see, the edges of the screen are cropped and the middle part is completely visible. This is the result of setting the width height to 1 (fitting high).

That's how you manage your designer's decision about the background and the user's potential resolution.

But what about these buttons? What happens when they are very close to the left or right side of the screen. , of course you don't want to cut or hide them.

Fortunately, Unity has a feature that will help you solve this rookie-level error and you'll learn it right away. Add title Picture

Adding a background image may be a bit of a fee, mainly because you set up the initial interface, and after a few times you will find that the installation is quick and the blink of an eye

Open the scene interface and select the menu Gameobject\ui\image, which will add a white picture somewhere in the view, and the bottom is

Note: To put the picture at the end, just set his x and Y properties to 0.

Now, turn this white area into a real picture.

1. Select image in hierarchy and rename it to Img_header

2. Open the menu file in Project Explorer to search for Header_label pictures

3. Drag the picture into the resource file

4. Click Set Native Size

As you can see, it's very easy to add another picture. Now you need to deal with the position, which takes you to the next exercise, using the Rect transform component coordinate transformation, Anchor point, rotate with center

If you've used unity before, or just finished this tutorial some Unity tutorials on this website, for the transform component you might have some understanding

If not, it doesn't matter, in the scene he is a simple tool for placing positions, rotating, scaling objects, and it looks like the bottom

In your hierarchy, currently you check gameobject any one of the types is, you will see the transform component

However, if you select a UI element, such as Img_header, you will see a different area transform component name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.