Mastering Unity 2017 Game Development with C#(Second Edition)
上QQ阅读APP看书,第一时间看更新

Setting the scene with a skybox

Our game environment should be dark, creepy, and suspenseful. However, every new Unity scene is created with a default, procedural skybox representing a cheery daytime exterior; this needs to be changed. A skybox is ultimately a cube with a flipped normal that surround and encompass the environment. Its faces contain an environment texture which, when mapped correctly, appears seamlessly across the model, creating the look of a vast skyline surrounding the scene. The primary purpose of our skybox should be to set a base and ambient lighting. The most appropriate skybox for our usage, then, is a night skybox or, at least, a dark, stormy (and perhaps slightly alien) skybox. There are many ways to create a skybox in Unity. One method is to create a cube-map texture (six separate textures) inside the image-editing software that maps to the faces of a cube. Another method, available in Unity 5, is a procedural skybox. Using this, Unity generates a skybox from some initial creation parameters that we can set through a material.

Let's use the latter, most customizable method. To do this, open the Material folder in the Project panel and create a new material. Assign it a meaningful name; I've used matNight:

Creating a new material
Naming assets and objects appropriately is important for large projects, whether team-based or not. It's important to identify an object's type and purpose from its name, and to use a naming convention that'll alphabetically group together related assets in a useful way. For example, I prefixed all material assets with mat_ ( mat_Skybox, mat_Marble, and mat_Leather). Using this name, all materials are arranged together in the Project panel, and I can determine by the asset name that it's a material and not a texture ( _tex), mesh ( _mesh), sound effect ( _sfx), and so on.

Some may question whether prefixes are needed if folders are used. After all, can't you just name your materials anything you wanted, and simply group them together in the same folder, as opposed to using prefixes? In short, use folders to organize and group textures, but name prefixes are still important because assets from different folders can still be viewed together in a consolidate display, when running searches and filters. Also, in these cases, you'll still want to arrange search results alphabetically.

Newly created materials are configured as a Standard shader (PBR material) using the metallic-roughness workflow. This isn't necessary for a skybox, as it doesn't cast light in the same way and isn't affected by shadows and other light sources. Change the material type to Skybox | Procedural. Multiple skyboxes are available, but only a procedural skybox gives you out-of-the-box runtime control over the colors and construction of the texture.

PBR stands for Physically Based Rendering. In Unity, this corresponds to the Standard Shader. This is a special material configured to simulate real-world properties. By using the Standard Shader you can create materials within real-world limits and that react to light as you would expect. The primary purpose of PBR materials to simplify the creation of photo-realistic worlds.

Creating a Procedural sky material

Now, let's configure the material properties. By default, the skybox is generated as a daytime sky. We'll need to change that; set the Sun field to None, as we won't need one, set the Sun Size to 0, Atmospheric Thicknes to 0.43, and Exposure to 1.3; this adds volume and a horizon glow to the environment. In addition, set the sky and ground colors; these should be dark values to express a nightscape. I've chosen 021643FF for Sky Tint and 1D1A19FF for Ground:

Configuring night time skybox

The skybox can be applied to the scene in several ways. One way is to assign the skybox to a camera as a background. To do this, simply select a camera, and from the Component menu, add a Skybox component. Navigate to Component | Rendering | Skybox. However, for our purposes, we'll avoid this method. We want the background to apply to the scene, and all cameras, as opposed to a single camera:

Adding a Skybox component to a camera

Instead of applying a skybox to a camera, we'll apply it to the scene. To do that, open the Lighting window by navigating to Window | Lighting from the application menu. From the Lighting window, drag and drop the newly created skybox material to the Skybox field:

Assigning a skybox to the scene

When the skybox is applied via the Lighting window, it should appear from the Scene tab as the world background. If it doesn't appear, ensure that you enable Effects Visibility from the Scene toolbar:

Enabling skybox effect