- Unity 2020 By Example
- Robert Wells
- 800字
- 2025-02-28 16:26:47
Importing assets
Most projects you undertake in Unity will require external resources, and this project is no different. You will often need to import and configure sound, images, 3D models, and even other people's code, and this process will become second nature to you as you create the projects in the book.
Important note
As a reminder, when we import assets, we are making a copy of the original. During the import process, Unity will create a corresponding meta file for each asset. Please do not remove or edit this file yourself as it includes necessary import settings. For information on the import process, see Chapter 1, Exploring the Fundamentals of Unity, and Unity's online documentation: https://docs.unity3d.com/Manual/ImportingAssets.html.
In this section, we will go through how to import and configure textures and audio.
Importing textures
Let's start with textures for the player spaceship, enemy spaceships, and star-field background. Drag and drop textures from Windows Explorer or Finder to the Unity Project panel in the Textures folder. Unity imports and configures the textures automatically:

Figure 3.3 – Importing texture assets for the spaceship, enemies, star-field background, and ammo
Tip
The use of the provided assets is optional. You can create your own if you prefer. Just drag and drop your textures in place of the included assets, and you can still follow along with the tutorial.
By default, Unity imports image files as regular textures for use on 3D objects, and it assumes that their pixel dimensions are a power-2 size (4, 8, 16, 32, 64, 128, 256, and so on). If the size is not one of these, then Unity will up-scale or down-scale the texture to the nearest valid size. This is not appropriate behavior, however, for a 2D top-down space shooter game in which imported textures should appear at their native (imported) size without any scaling or automatic adjustment. To fix this, perform the following steps:
- Select all the imported textures.
- In Inspector, change the Texture Type from Default to Sprite (2D and UI).
- Click on Apply to update the settings, and the textures will retain their imported dimensions.
- Remove the checkmark from the Generate Mip Maps box if it is enabled:

Figure 3.4 – Changing the texture type for imported textures
Disabling generating Mip Maps will prevent Unity from automatically downgrading the quality of textures based on their distance from the camera in the scene. This ensures that your textures retain their highest quality. More information on 2D texture settings and Mip Maps can be found at the online Unity documentation: http://docs.unity3d.com/Manual/class-TextureImporter.html.
Important note
Later in the chapter, we will drag and drop the textures to the scene, which will add them as sprite objects. You must drag and drop them from the Project panel to the Hierarchy panel, not to the scene view. When you do this, the texture will be added as a sprite object in the scene. We'll make frequent use of this feature as we create spaceship objects.
With the textures successfully imported, we can move on to importing the audio files.
Importing audio
Music and audio effects are important. They add an extra level of immersion and, on occasion, are a core part of the gameplay experience (think Cadence of Hyrule on the Nintendo Switch). With that in mind, it's important that you familiarize yourself with the process of importing and configuring audio files.
Important note
The assets used in this section can be found in the Chapter03/End/Assets/Audio folder and were downloaded from OpenGameArt.org.
To import the audio, drag and drop the files from the folder to the Project panel. Unity will import and configure the assets. You test the audio from within the Unity Editor by pressing Play on the preview toolbar from the Inspector, as shown in Figure 3.5:

Figure 3.5 – Previewing audio from the object Inspector
As with texture files, Unity imports audio files using a set of default parameters. These parameters are typically suitable for short sound effects such as footsteps, gunshots, and explosions. However, for longer tracks such as music, they can be problematic, causing long level-loading times. To fix this, select the music track in the Project panel and, from the Load Type drop-down box, select the Streaming option, as shown in Figure 3.5. This option ensures that the music track is streamed as opposed to loaded wholly in memory at level startup.
Important note
In future projects, we won't go step by step through importing assets. However, you can always refer back to this section, and any differences will be noted.
And that's it! Both the textures and audio files required for this project are ready to use in our game. Now it is time to start creating our own assets, beginning with the player object.