官术网_书友最值得收藏!

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:

  1. 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.
  1. 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.
  1. Create a new scene and name it Chapter2.unity; ensure that you save it in the Scenes folder.
  1. 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.
  2. 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:
  3. Drag the three sprites into the Sprites folder.
  1. Select the catSprites image, hold Ctrl, and click on the uiElements image so that both are selected.
  1. 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.
  1. Note that the Inspector says that 2 Texture 2Ds are selected, because we have selected two images.
  2. Now select the catSprites image and open the Sprite Editor.
  3. With the Sprite Editor open, select Slice.
  4. 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.
  5. You should now see the sprite broken in to three separate regions. Hit Apply to save the changes.
  6. Now if you click on the arrow on the catSprites image in the project folder view, you should see the individual images.
  1. 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.

主站蜘蛛池模板: 大关县| 资源县| 香河县| 溆浦县| 五指山市| 阿城市| 固阳县| 黄陵县| 义马市| 临西县| 信阳市| 基隆市| 内黄县| 澄江县| 二手房| 东阳市| 余姚市| 遂川县| 邳州市| 宁国市| 仙游县| 红安县| 明溪县| 晋江市| 三门峡市| 兴安县| 彰武县| 永登县| 秀山| 新宁县| 屏东县| 淮北市| 平安县| 兴和县| 香河县| 建阳市| 上饶县| 靖江市| 常州市| 罗江县| 永仁县|