harian untung99play.xyz

untung99play.xyz: Introduction to Unity UI Part 1


Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian untung99play.xyz dengan judul untung99play.xyz: Introduction to Unity UI Part 1 yang telah tayang di untung99play.xyz terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99play.xyz, Terimakasih.

Update February 2019: This tutorial was updated to Unity 2018.3 by Ben MacKinnon. Original post by Kirill Muzykov.

You will love getting gooey with Unity’s new GUI!

Do you remember the old Unity UI system? It required you to write all your GUI code in OnGUI. It was slow, inefficient and programmer-centric, going against the visual nature of Unity. Many developers chose instead to use third party tools such as NGUI.

Thankfully, Unity Technologies listened to community feedback and worked out a new system, released in Unity 4.6.

In this three-part series, you’ll explore Unity’s new UI system by adding an interactive UI to a game called Rocket Mouse.

To satisfy users’ cravings for an engaging UI, you’ll also learn to:

  • Animate buttons
  • Create a settings dialog that slides into a scene
  • Use more GUI controls like Text, Slider, Panel and Mask

Getting Started

This tutorial is aimed at those familiar with the Unity Editor. Go check out our Introduction to Unity series if this is your first rodeo. The great news is that as this tutorial focuses on the UI system, it is 95% script free!

Note: Keep in mind that this tutorial does not cover the creation of the game itself. The goal here is to provide an introduction to Unity’s UI system and familiarize you with its components.

You’ll need some images for backgrounds, buttons and other UI elements, as well as a few fonts for the labels and buttons. Don’t worry, you won’t have to draw anything yourself or scour the web for the right assets. I’ve prepared a special package that has everything you need. You’re welcome. :]

To get the package, and the starter project files, click on the Download Materials button at the top or bottom of this tutorial.

This is all you need!

Creating MenuScene

Open the Introduction to Unity UI Part 1 Starter project in Unity.

The RocketMouse game is already set up, and all the assets for it are in its own folder. You’ll be working out of the RW folder, which contains a Scenes folder. Now, you’ll create a new scene to work with. From the menu bar, Select File ‣ New Scene to create a new empty scene.

It’s best to save the scene right away. Open the Save Scenes dialog by choosing File ‣ Save Scenes. Then, enter MenuScene as the scene name and save it to the RW / Scenes folder, right next to the RocketMouse scene.

Importing Images and Fonts

First on your to do list is to add assets to the scene, so unpack the UI Assets package in your file explorer. There you’ll find two folders: Menu and Fonts.

Note: The Menu folder contains background images, buttons, icons and other game art. The game art is provided by Game Art Guppy, where you can find a lot of other game art for your practice games. In addition, two fonts from DaFont. are in the Fonts folder. You can thank Rodrigo Fuenzalida for the Titan One font and Draghia Cornel for the DCC Dreamer font.

It’s nice to keep a tidy folder structure, so in the Project window, create a new folder inside RW named UI.

From your file explorer, drag the Menu and Fonts folders into the UI folder in the Project window:

Once the assets are in the project, check their import settings. Make sure all the assets in UI / Menu are set to the Sprite (2D and UI) texture setting.

Woo-hoo! You’ve finished the setup and you’re ready to create your first UI element using the Unity UI system.

Adding Your First UI Element

The first element you’ll make is the background image for the menu scene.

From the top bar, select GameObject ‣ UI ‣ Image. This adds an object named Image to the scene. You should see it in the Hierarchy as a child of Canvas. All elements must be placed on a Canvas. If you don’t have one, Unity will create one for you automatically.

Select the Image and set its position to (X:0, Y:0). You’ve just discovered the Rect Transform:

Note: The Rect Transform is the UI equivalent to Transform. It positions, rotates and scales UI elements within a Canvas. You’ll use it often in this tutorial.

You’ll set the correct position and size in a moment. Right now, there are three things to be aware of. In the Hierarchy, you’ll see three new objects in the scene:

The Image is a non-interactive control that displays a sprite, with many options for customization.

For instance, you can apply color to the image, assign a material to it, control how much of the image displays or even animate it on the screen using a clockwise wipe.

The Canvas is the root object for all your UI elements. Remember, Unity creates this for you when you add your first UI element. It has multiple properties that allow you to control how your UI renders.

The EventSystem processes and routes input events to objects within a scene. It’s also responsible for managing raycasting. As with the Canvas, The UI requires the Event System, so Unity automatically adds it.

Setting Up the Menu Background Image

The first thing to do is rename your image. In the Hierarchy, select Image and rename it to Background.

Next, open RW ‣ UI ‣ Menu in the Project window and find the menu_background image. Drag it to the Source Image field of the Image component in Background in the Inspector:

Now you have a background image instead of the default white image. However, it’s not great! It’s too small and the aspect ratio is incorrect.

To fix this, find the Set Native Size button in the Inspector and click it to set the size to 1136 x 640.

Now it looks like a proper background.

Uh-oh. There’s still one problem.

Shrink your Scene view, and you’ll see that the white rectangle of the Canvas covers only part of the image. If you switch to the Game view, you’ll see only a part of the background image, as if the camera is too close to the image to capture it completely.

Note: The original game design was for iPhones with 3.5- and 4-inch displays. This is why all the game art supports 1136 x 640 and 960 x 640 resolutions. You’ll soon see how the UI can adapt to different game resolutions.

You’ll tackle this issue by using a Canvas Scaler.

Using the Canvas Scaler

You’ll use the Canvas Scaler to adjust the background image display.

First, you need to know that the display is not the result of a bug. From Unity’s point of view, you have the Game view — or viewport — set to such a small size that it displays only the portion of the image that fits within the Game view.

If you were to run the game on a device with a large enough resolution or stretch the Game view to fit the whole image, you would see the entire background image.

Although Unity’s settings make sense in most scenarios, there are times when you need different behavior. An example is when you have a small monitor that doesn’t fit your target device’s resolution.

Additionally, many games support only one resolution. Designers use this reference resolution to dictate sizes, positions and other data. When you develop a game based on a single reference resolution, make sure to enter the designer’s specifications without additional calculations so that the user sees everything as intended.

If you’ve ever ignored your designer’s directions, you know there’s a price to pay. The user experience and varying resolutions are important, but try to keep your designer happy, too. :]

Canvas Scaler to the rescue! By default, every Canvas includes Canvas Scaler.

Select Canvas in the Hierarchy, and in the Inspector, you should see the Canvas Scaler component:

The Canvas Scaler has three scale modes:

  • Constant Pixel Size: Makes UI elements retain the same pixel size, regardless of the screen size. This is the default value of the Canvas.
  • Scale With Screen Size: Sizes and positions UI elements according to a referenced resolution. If the current resolution is larger than the referenced resolution, the Canvas will maintain the reference resolution, while scaling up the elements to match the target resolution.
  • Constant Physical Size: Positions of the UI elements are specified in physical units such as millimeters or points. This requires the correct reporting of the screen DPI.

Change the component mode to Scale With Screen Size and set its Reference Resolution to (X:1136, Y:640). Also, slide the Match Width or Height all the way to the right, or simply enter 1 in the input field.

After making those changes, you’ll see the full background image, even in a small Game view window.

Change the Game view resolution to see how your game might look in a different resolution, for example, on iPhone Wide 480×320. You’ll notice it still looks good!

Note: If you don’t see any of the iPhone options, chances are you’re building for a different platform. From the menu bar, select File ‣ Build Settings. In the build settings dialog underneath the platform settings, make sure to select iOS.

Unity will reprocess all your assets, and it may take a while. At the end, you should have access to the various iOS screen sizes.

Now switch to the Scene view, and you’ll see the Canvas’s size doesn’t change when you resize the Scene view. The side edges of the screen are neatly cropped while the central part is fully visible. This is the result of setting Match Width or Height to 1. It works perfectly for your target resolutions.

These designs are old! Modern phone screens are much wider in Landscape mode. Switch to iPhoneX Landscape mode and we have a whole new problem.

If you change the Match Width or Height value back to zero, it works for iPhone 5 and iPhoneX, but appears letterboxed on iPhone 4.

Fortunately, there are other solutions for this. While the Canvas Scaler has two other scaling modes, Expand and Shrink, there’s another useful component that we can use specifically for background images.

Put the Canvas Scaler back to Match Width or Height with the value 1, and set the Game view to iPhoneX. It should like the image above again.

Now, select the Background and add an Aspect Ratio Fitter component. It will automatically set the Aspect Ratio value to the current ratio of the image’s Rect Transform. Switch the Aspect Mode to Envelope Parent. The image will automatically resize to once again fill the whole screen.

Phew, that was a lot of work. And we’ve only added the background image! But getting the Canvas Scaler and background set up in the proper way is going to make everything easier going forward. After doing this a couple of times ,you’ll find the setup so fast and easy that you’ll barely have time to blink before you’re done.

What about the buttons? What happens when they’re too close to the left or right edge of the screen? You don’t want to crop or hide them.

Fortunately, Unity has a feature that will help you sidestep this rookie mistake. You’ll learn about it soon.

Adding a Header Image

Before moving on to buttons and other UI controls, you’ll add the header image. For this exercise, you’ll use a non-fullscreen image to demonstrate a few other important concepts of Unity’s new UI system.

Open the Scene view, and from the top bar, select GameObject ‣ UI ‣ Image. This will add another image as a child of Canvas:

Note: If you can’t see the image in the Scene view, set its Pos X and Pos Y properties to 0 to center it.

Now, turn that white rectangle into an actual image by following these steps:

  1. Select Image in the Hierarchy and rename it to Header.
  2. Open the Menu folder in the Project window and search for the header_label image.
  3. Drag the image to the Source Image field on the Inspector.
  4. Click Set Native Size in the Inspector.

Now, you’ll work with the Rect Transform component to position the image.

Rect Transform, Anchors, Pivot and You

If you’ve worked with Unity before or completed other Unity tutorials on this website, you may have had some exposure to the Transform component. If not, that’s fine. Transform is a tool that can position, rotate and scale objects in a scene. Here’s what it looks like:

You’ll see the Transform component when you select any type of non-UI GameObject in your Hierarchy view. However, if you select any UI element, for example, Header, you’ll see a different component named Rect Transform.

As you can see, Transform and Rect Transform look different. Additionally, the Rect Transform can change the way it looks, depending on its anchor settings. For example, it can look like this:

Here, instead of Pos X, Pos Y, Width and Height, you have Left, Top, Right and Bottom.

Are you wondering about the anchor setting that changes the look of Rect Transform so dramatically? Keep reading!

Anchors

Setting anchors is a simple, elegant and powerful way to control the position and size of your UI elements relative to their parent. It’s especially handy when you have to resize the parents.

When you set anchors, you specify several positions in the parent, usually one in each corner of the parent’s UI element Rect. When this happens, your UI element will try to maintain a uniform distance to the anchor points, forcing it to move or resize along with its parent.

To see different Anchors Presets, select Header in the Hierarchy and click on the rectangle above the Anchors field in the Rect Transform component.

After clicking, you’ll see various Anchors Presets. These are the most common settings for anchors, but you can customize them. You can also select different horizontal and vertical behavior for your UI element.

This will all make more sense once you work with it. If you look at the next image, which has the background image disabled, you’ll be able to see the Canvas size changes a bit better.

As you can see, the anchors settings control how your UI element adapts to screen size changes.

The four triangular handles representing anchors resemble a flower. Here’s how it looks with anchors set to the top-center preset:

Before you start experimenting with different settings, read through the next section. It’ll help you understand anchors a little better, and you’ll get more out of your experimentation.

Custom Anchors

You can manually move anchors to a custom position. The presets are just for your convenience.

Note: You might find yourself in a situation where the translation gizmo covers the anchor icon, making it impossible to select the anchor.

This is where another Unity UI tool comes in handy. If you are familiar with Unity, you’ll know that the keyboard shortcuts Q, W, E and R toggle the Hand, Move, Rotate and Scale tools respectively. With the new UI system, the RectTool was added under the shortcut T. With the RectTool selected, the Gizmo changes to a 2D scale/move box that acts as you would expect from any graphics editing software.


Now you can grab the anchors and move them around.

See how the image moves to the right when you resize the Canvas? It moves only a little in relation to the right edge of the Canvas because these anchors are set to 25% width of the Canvas.

Splitting Anchors

You can split anchors to make them stretch a UI Element horizontally, vertically or both.

Note: You’re not actually resizing the Canvas when dragging one if its edges. In fact, you can’t resize the Canvas this way.

Look for the word Preview next to the cursor when you try to resize it. Use this technique to experiment and see how your UI elements adapt to different screen sizes.


Rect Transform Depends on the Current Anchors Setting

Depending on the anchors setting, the Rect Transform provides different ways to control the size and position of your UI element.

If you set anchors to a single point without stretching, you’ll see the Pos X, Pos Y, Width and Height properties.

However, if you set anchors in a way that stretches your UI element, you’ll get Left and Right instead of Pos X and Width if you set it to stretch horizontally. You’ll get Top and Bottom instead of Pos Y and Height if you set it to stretch vertically.

In this screenshot, Header’s Anchors are set to middle-stretch. This means the image stays in the middle of the Canvas vertically and stretches horizontally.

Pivot

The final property to discuss in the Rect Transform component is Pivot.

The pivot is the point around which all transformations are made. In other words, if you change your UI element position, you also change the pivot point position. If you rotate your UI element, it’ll rotate around that point.

The pivot uses normalized coordinates. This means that it goes from 0 to 1 for both height and width, where (0, 0) is the bottom left corner and (1, 1) is the top right corner.

Note: You can also set Pivot outside the UI Element bounds. In this case, Pivot will be outside the (0, 0) – (1, 1) range. This can be useful. For example, you might want to rotate your object around some point in the scene. To alter the pivot, you must make sure the Pivot/Center button is toggled to Pivot like so: