What Makes a Unity Project?

Một phần của tài liệu Game Development for iOS with Unity3D 2012 (Trang 76 - 79)

4. Basics of the Unity Editor 59

4.1 What Makes a Unity Project?

In this chapter, we’re going to start out by looking in detail at the parts that make up a Unity project. Knowing what goes into a project will help you to understand the inter- face, as well as manage everything when we reach Chapter 6 and start making an actual game. Chapter 6 isn’t that far away now, but try not to skip ahead just yet!

4.1.1 Scenes

The easiest way to imagine a scene would be to think of it in terms of a scene within a play. It contains everything to tell the story for a single scene, such as actors or props.

GameObjects (your game elements) may be brought in from external sources, such as from disks or the Internet; however, the scene is a place where the game exists.

There is no set way to use scenes. The way in which you use them depends on your preferences and sometimes on the project requirements. Some games will have a scene per level; some may have all of the menu screens in a single scene and the game play in another; others may even have a single scene with everything all in it or everything instantiated dynamically.

4.1.2 GameObjects

GameObjects are, in essence, empty objects to which all parts that go to make your game elements are attached. A GameObject has a transform used for setting its rota- tion, scale, and position. A GameObject may also contain components, such as scripts or components that define physics, audio, or camera properties, and more. If we refer

back to the analogy of the scene as a theater stage, the GameObjects may be thought of as the actors, props, or anything else that will make up the content of the play itself.

4.1.3 Materials and Shaders

Materials allow you to adjust properties and assign assets to shaders. A shader contains everything Unity needs in order for it to render something on the screen, and a shader defines which properties or assets are used when a mesh is rendered to the screen. The language Unity uses for coding shaders is called ShaderLab, and it is similar in style to CgFX and Direct3D Effects .FX languages.

Unity ships with over 30 shaders at no extra cost. There are shaders available for transparencies, reflections, and all kinds of particle effects. Have a look at the shader drop-down menu on any material to see the different options Unity provides you with.

4.1.4 Models

In a Unity game, its models or meshes are rendered with a renderer. When you import the mesh and drag it into the scene, the renderer component (a mesh renderer) is au- tomatically added to the GameObject. The renderer displays the mesh at the GameOb- ject’s position and its appearance is controlled through the renderer’s materials.

4.1.5 Transforms

Every object in a scene has a transform, used to store and manipulate object properties such as position, rotation, and scale. If a transform is parented to another transform in the Hierarchy View, it will be affected by the parent’s transform properties. This means that you could, for example, have several objects affected by the scale factor or rotation of its parent object(s).

Be aware that modifying the scale of a transform via code in real time (as your game is running) is not recommended, as it can negatively impact performance. When you modify the transform scale, it can mean that the mesh information needs to be sent to the GPU and the CPU multiple times. In most cases, the difference is negligible, but it is certainly bad practice to work this way.

4.1.6 Scripts

Unity supports three different languages for scripting: Boo, C#, and UnityScript. Uni- tyScript is a custom language with ECMAScript-inspired syntax and is the language usually referred to by Unity users and in the documentation as JavaScript. Even though UnityScript is not technically JavaScript, it is very similar, and most JavaScript syntax can be applied without issue.

Having such flexibility in its supported scripting methods has no doubt helped Uni- ty reach such a wide range of users, making it easy to transfer existing skills. Reading through the official Unity forums or chatting in IRC, it is not too difficult to find argu- ments over which language is better; but I say ignore all that and go your own route.

Each language has its pros and cons. There are even converters available on the Asset Store to convert between C# and JavaScript.

It is entirely possible to use a mix of scripts from C#, UnityScript, and Boo, al- though I would recommend trying to stick to one because of issues with compilation

4.1. What Makes a Unity Project? 61

order and complications that arise when you need one to talk to the other. Unity 3.5 did introduce a fully customizable compilation order system, but I prefer to let Unity take care of all that and stick to a single language.

Unity scripting is built on an open-sourced .NET implementation called Mono.1 4.1.7 Sounds

The engine supports a number of audio formats to use for your game sounds. It is rec- ommended that you bring in all of your audio as uncompressed .wav format files and allow Unity to take care of the compression. On iOS, you will need to use compressed audio as much as possible to avoid filling up the memory unnecessarily.

The biggest drawback to using compressed audio on an iOS device is that the de- compression process may eat up valuable CPU cycles, resulting in performance deg- radation. For incidental sound effects such as laser blasts or impact sounds, choose uncompressed audio to avoid slowdown.

Finding the balance between resources (CPU and memory) and quality is never an easy task. Ensuring that your game runs smoothly on iOS may sometimes mean that you have to compromise on the audio quality here and there. You and your audio engi- neer should be prepared for this.

4.1.8 Textures

Textures are images that wrap around your 3D objects, and Unity is extremely flex- ible in the formats that it supports and how they can be manipulated once inside the engine.

4.1.9 Unity Packages

A Unity package is a collection of files and components (scripts, etc.) wrapped up into a single file with the extension .unitypackage. A .unitypackage file could contain anything from scripts to materials to sounds to 3D models—anything that we can use in Unity.

You can make your own unity packages to distribute or import into other projects, and all of the purchases made from the Asset Store will be delivered as packages wrapped as .unitypackage files.

We will be looking at some of these fantastic freebies in more detail in Chapter 6 when we build out our first Unity iOS game. Unity and Unity iOS gives you a whole bunch of free code examples and things to get you started like character control rigs, control schemes for iOS, scripts, and more, all wrapped neatly into .unitypackage files and ready to import into your project.

Notice that when you create a new project, the editor brings up all of the available unitypackages provided by Unity so that you can start a new project with those assets already in place and automatically imported.

4.1.10 Prefabs

A prefab is a type of asset made from a GameObject. Prefabs are reusable; they can be used in any number of scenes, multiple times per scene. When you add a prefab

1 http://www.mono-project.com

to a scene, you create an instance of it, but it continues to be linked to the original prefab object. Changes to the main prefab will effect those changes to all instances in all scenes.

Prefabs are commonly used for interactive objects such as characters, pickups, or vehicles. We usually create a complex set of GameObjects in the scene, such as a car rig with suspension, wheels, collision body, etc., and then create a prefab of it and delete the original objects from the scene. Whenever that type of object is needed in the game, the prefab is instantiated into the scene. A good example as to why you might use a prefab in this way would be a racing game where each scene contains an environment for cars to race around. Rather than have to copy all of the cars into each scene, we instantiate cars as needed. That way, to change all cars we only need to change the prefab, alleviating any need to update all scenes that have cars in them whenever the car needs to change.

To create a prefab, the easiest way is simply to drag the root GameObject from the Hierarchy window down to into the Project window. You can also use the menu Assets –>Create–>Prefab.

Một phần của tài liệu Game Development for iOS with Unity3D 2012 (Trang 76 - 79)

Tải bản đầy đủ (PDF)

(278 trang)