- Mastering UI Development with Unity
- Ashley Godbold
- 531字
- 2021-08-27 18:43:15
Examples
Now let's jump into some examples! We'll be creating a layout for a basic heads-up-display (HUD) and a background image that stretches with the screen and scales at multiple resolutions.
Before we begin setting up our UI, let's set up our project and bring in the art assets we will need.
We'll begin by setting up our project:
- Create a new Unity Project and name it Master Unity UI Project. Create it in the 2D mode.
We're selecting 2D Mode because it will make importing our UI sprites a lot easier. When in 2D Mode, all images import as Sprite (2D and UI) images rather than Texture images, as they do in 3D Mode. You can change to 3D Mode at any time by navigating to Edit | Project Settings | Editor and changing Mode to 3D.
- Create three new folders named Scenes, Scripts, and Sprites:
You don't need the Editor folder, but, if you'd like it, it was created following the steps in the Method 1: PlayerPrefs.DeleteAll() section of Chapter 1, Designing User Interfaces.
- Create a new scene and name it Chapter2.unity; ensure that you save it in the Scenes folder.
- We’ll be using art assets that I've modified from free art assets found at the following:
https://opengameart.org/content/free-game-gui
Chapter2/Sprites folder, you'll find the catSprites.png, pinkBackground.png, and uiElements.png images. - We'll be importing the sprites to the project and slicing the sprite sheets into their separate image regions. If you already know how to slice sprite sheets, do so now for the catSprites image and the uiElements image and proceed to the Laying out the Basic HUD section. If you are not familiar with the process, follow these steps:
- Drag the three sprites into the Sprites folder.

- Select the catSprites image, hold Ctrl, and click on the uiElements image so that both are selected.

- Now, in the Inspector, select Multiple for Sprite Mode. Then hit Apply. This will cause both the catSprites and the uiElements images to be considered a sprite sheet.

- Note that the Inspector says that 2 Texture 2Ds are selected, because we have selected two images.
- Now select the catSprites image and open the Sprite Editor.
- With the Sprite Editor open, select Slice.
- Now change the slice properties so that the Slice Type is Automatic and the sprite Pivot is applied to the Bottom. Once done, hit Slice.
- You should now see the sprite broken in to three separate regions. Hit Apply to save the changes.
- Now if you click on the arrow on the catSprites image in the project folder view, you should see the individual images.
- Complete steps 10–14 for the uiElements image, but make the pivot point in the Center.
Now that we have our project and sprites set up, we can begin with the UI examples.
推薦閱讀
- Learning Python Web Penetration Testing
- 程序員面試白皮書
- Android Application Development Cookbook(Second Edition)
- Python高級編程
- 區(qū)塊鏈:以太坊DApp開發(fā)實戰(zhàn)
- Java程序設(shè)計與實踐教程(第2版)
- 算法訓(xùn)練營:提高篇(全彩版)
- ASP.NET開發(fā)與應(yīng)用教程
- HTML 5與CSS 3權(quán)威指南(第3版·上冊)
- Vue.js光速入門及企業(yè)項目開發(fā)實戰(zhàn)
- Arduino機器人系統(tǒng)設(shè)計及開發(fā)
- 現(xiàn)代CPU性能分析與優(yōu)化
- PHP項目開發(fā)全程實錄(第4版)
- 百萬在線:大型游戲服務(wù)端開發(fā)
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)