lesson 4: Assets, Prefabs, Materials, and Packages
In this lesson we're going to look at the Assets drop-down menu. We're going to learn some key concepts and vocabulary such as Prefab, Instantiation, Materials, Textures, and Shaders. We'll revisit Packages, both importing and exporting, and learn about Playmaker's (free) bolt-on system for managing its extensibility!
Fig. 4.1 shows the Assets drop-down menu.
The Assets drop-down can be accessed by clicking on Assets along the Main Menu Bar. As with most things in Unity, there is more than one way to access the Assets menu. The alternate method can be accomplished by right clicking the right hand pane of the Project View.
Fig 4.2 shows the alternate method of accessing the Assets menu by right clicking the right hand pane of the Project View.
That will expand the menu and expose Create. Note that Create is sometimes inaccessible from the Assets drop-down menu along the Main Menu bar if you have anything other than the Assets folder selected in the Project View. As stated previously, there is typically more than one way to access options and Create is no exception. In addition to exposing it through the main menu bar drop-down, it can be summoned by right-clicking the Assets folder (and subfolders) in the left-hand panel of Project View, or by right-clicking anywhere in the right-hand panel of the Project View. And as if that wasn't enough, there's a Create button just bellow the Project View's folder tab. Note that this Create button is different from the Hierarchy View's Create button.
Fig. 4.3 demonstrates clicking the Create button at the top of the left hand pane of the Project View.
The rest of the choices in this subheading relate to coding either directly or tangentially and in keeping with the main tenant of this treatise can be safely ignored. Text Mesh Pro, however, may be covered in an upcoming lesson. XXX
Scene adds a new scene to your Assets' Scenes folder and prompts you to name it. This is similarly evoked using File>New Scene or Ctrl+N. Prefab Variant cannot be explained without an in-depth explanation of Prefabs.
Prefab is one of those all important vocabulary items and an important concept in its own right. What is a Prefab? Consider a Prefab house. A Prefab house is a house that is prefabricated; that is, it will come in sections, it will already be framed and grouped together with necessities such as electrical and plumbing already partially installed. Likewise a Prefab in Unity is a collection or grouping of assets such as meshes, materials, scripts, and other game objects that you can instantiate throughout the scene. No doubt the question has occurred to you, how does a Prefab differ from a group of assets that have been copied and pasted throughout the scene? A Prefab has some very special properties. The most ubiquitous of examples is that of a lamp post game object.
With a lamp post you might have the mesh, the material, a point light, dust motes or moths flying around, and you might even have a sound effect such as a low electrical hum. In a large city scene there may be a hundred lamp posts. What happens if you find out later that all of your lamps are too dim? If you've been copying and pasting the lamp post you'll have to go to each lamp post individually and turn up the light settings. Never mind trying to do that in one less operation, we have to find a way to do that in one hundred less operations if we ever have any hope of completing our game! That's where Prefabs and Instantiations save the day. If you've instantiated the lamp post from a Prefab, that is, copied and pasted from a Prefab as opposed to copying and pasting from a regular game object, you can select the Prefab and turn up its light and that change will propagate throughout all of the Instantiations (the pasted prefabs.) Alternately, you can select one of the Instantiations, turn up its light, click Apply at the top of the Inspector, and that change will be applied to the Prefab and then propagated. It should occur to you that a Prefab is a special type of game object or group of game objects and an instantiation is a special kind of Prefab clone. A Prefab is special because any changes to it automatically update its Instantiations. Prefabs occupy the Project View, Instantiations populate the Heirarchy View once they are added to the scene. A Prefab is most easily created by developing the Game Object or Game Object grouping in the Scene and Hierarchy Views and then dragging and dropping its textual representation from the Hierarchy View into the Project View. Let's get ahead of ourselves by importing a Package and then creating a prefab.
Check the links in the description for the download.
-[Download the section1.unityPackage from http://www.nickelcitypixels.com/noCodeDLC (or web search)]-
Click Assets>Import Package>Custom Package and locate and import the package
A folder named Scenes will now be available in the Project View. Open it and double-click the newly imported Scene
Select Don't Save when prompted; there shouldn't be any reason to save Scene Untitled at this point
The package includes a signTastySquid mesh from the upcoming game, "Yoshimi Vs. Face Punching Robots!"
Select text Main Camera from the Hierarchy select GameObject>Align View to Selected
Select the Assets folder from the left hand view of the Project View and then in the right-hand panel, right-click and Create>Folder "Prefabs"
Drag the signTastySquid text from the Hierarchy View into the Prefabs Folder in the Project View
You've created a Prefab!
Note that the signTastySquid text in the Hierarchy View has turned blue indicating a Prefab connection
Instantiate the Prefab by dragging it from the Prefabs folder into the Scene View multiple times
In the prefab folder, click on the arrow to expand the signTastySquid Prefab
Select the Point light. In the Inspector View, increase the light's Intensity
Fig. 4.4 illustrates the Scene thus far.
Note that the Instances' light intensity increases as well
Select an Instance from the Hierarchy View
Expand the GameObject's hierarchy to select its Point light
In the Inspector click on the Color parameter and change its' tint in the resulting Color window
At the top of the Inspector, click Apply
Note that the Prefab and Instances' light changes color as well
Remember to save your Scene prior to exiting, recall that Projects need not be saved (they, and changes to the editor/Assets are saved automatically)
Note that this is the only time you'll be reminded to save your Scene; in ensuing lessons you should take the initiative
There are a number of things that can be done to an Instantiation without going so far as to break the link between it and the Prefab. The transform values can, of course, be modified so that the Instantiation can be moved, rotated, and even scaled. The Instance can be renamed for the purpose of distinguish one clone from all the others. To drastically change an instantiation or to make an instantiation unique in such a way that it is no longer affected by changes to its Prefab, select Game Object>Break Prefab Instance. Doing so turns the Instance's text in the Hierarchy View black to denote that there is no longer a connection and that the GameObject is no longer an Instance. But guess what? As part of its metadata scheming, Unity remembers that this was once an Instance so you can always reconnect it to the Prefab by selecting the GameObject and clicking on Revert in the Inspector View!
4.2 Prefab Workflow
In 2018, Unity added additional ways to distinguish and interact with Prefabs. The new additions to Prefab Workflow are: Nested Prefabs, Prefab Mode, Prefab Overrides and Prefab Variants.
Previously creating a Prefab meant dragging and dropping Assets into a Scene and editing it there. Then the developer would drag the resulting edited collection back into the Assets folder and it would magically become a Prefab (this is the method demonstrated in our course). Prefab Mode allows you to edit a Prefab in a separate window, independent of the selected Scene. Prefab Mode is activated by double-clicking the Prefab Asset in Project View, or, if an instantiation exists in your currently selected Scene, by clicking the arrow to the right of the instantiation in the Hierarchy View. Changes made in Prefab mode are saved automatically (although there's an option to turn that off). Changes made to the Prefab will propagate throughout the Instantiations as you might expect. In short, Prefab Mode is yet another, but conceptually better, way to go about things.
Instantiations have been made more versatile too and are now capable of incorporating Prefab Overrides. Essentially you can now individualize Instantiations every which way without breaking the link between it and the Prefab. For example, you might select Instantiation CyborgA from the Scene, scale it up and change its Rigidbody Mass so that it looks bigger and behaves beefier. Any changes to the original Cyborg Prefab will update Instantiation CyborgA, except for the properties that were explicitly edited for CyborgA. For example, you might make the original Cyborg Prefab faster. Instantiation CyborgA will inherit the new faster speed from the original Prefab, but the overridden values of size and beefiness will not change. Additionally, changes to an Instantiation involving the addition/subtraction of GameObjects and Components also constitute an override. And again, all that's needed to activate an override is to edit the values you want overridden (in the Inspector) or add/subtract GameObjects and Components to the selected Instantiation. Consequently, you can have a whole lot going on! Mercifully, Unity will help you keep track of what has been overridden. This is facilitated by visual cues; in the Hierarchy View, Instantiations with overrides are marked with a plus symbol in a circle next to its name. In the Inspector the text of the overridden properties will become blue and bold and have a blue line placed at left. Additionally the Inspector now hosts an Overrides drop down menu. It allows you to select and compare Instances with their Prefab in a new, side-by-side window. Here you can apply overrides from the Instantiation to the original Prefab or reset overrides to their default Prefab settings. Cool!
Audio Mixer creates an Audio Mixer Asset and adds an Audio Mixer View to your editor. The Audio Mixer is essentially a virtual, um, audio mixer, as its name would suggest. It allows you to channel multiple audio signals and manipulate the dynamics, such as volume and pitch, of each track. For most beginning developers, however, it might make more sense to complete your audio edits outside of Unity.
It is important to understand Materials. A Material is a collection of texture maps (Textures) and rendering instructions (Shaders). This collection does not, however, exist as a separate file type such as a .unityPackage or .zip. Software Packages all handle materials differently, but the various implementations all have one thing in common; slots that you can fill with Textures and the ability to command which Shaders with what options should be applied. Textures are image files that give meshes surface detail. The term "texture" is often used interchangeably with "map." Diffuse Maps (aka Albedo Maps) provide color information, Normal Maps tell the engine how to light the mesh to give the illusion of additional geometry, Specular Maps define how shiny specific areas of the model should be. Often textures will contain an Alpha Channel in addition to the visible Red, Green, and Blue channels. This is often used to designate which parts of the Texture should be transparent or how shiny certain areas should be (as with Specular Maps). Unlit Textures (aka Self-Lit, aka Self-Bright) glow in the dark. There are many different types of Textures and most meshes will utilize multiple Textures in their Material. They will also use a Shader. Texture Maps define what should render where, but a Shader provides instructions as to how the material should render. A Cel-Shader (Cartoon), for example, will tell the engine to render an outline around a mesh in order to give the object a cartoony look. Instead of applying Textures to a mesh individually, the Textures are grouped together in the Inspector along with engine specific parameters such as which Shader to use, and with what options. This grouping of Textures and Shader instructions is the Material and it is the Material that is applied to the mesh. As a final note, if you're really desperate, you can apply Materials that don't have Textures and simply specify the Material be a single color. To recap, a Material isn't a file type but rather is typically a collection of Textures and Shader instructions that is applied to a mesh to give it convincing surface detail.
Fig. 4.5 shows an example Material as created in the Material Editor
A lens flare results from the scattering of light in a lens system. It sometimes produces artifacts such as glowing orbs of light. In cinema as in video games, lens flares are sometimes used (sometimes over used) for dramatic or artistic effect. In Unity, the Lens Flare Asset allows you to specify the elements of the Lens Flare in the Inspector. The Asset can then be dragged and dropped into the corresponding property slot of the light it is to be assigned to.
Render Texture allows you to project what a Unity camera sees onto a texture. One use-case would be to create a Unity camera to serve as an in-game surveillance camera. Properly configured, it will project what it sees onto a texture serving as a security monitor. You specify the Target Texture in the Camera's options rather than specifying which Camera the Render Texture will use.
Lightmap Parameters creates an Asset with editable elements that control Unity's global illumination system. Embodying these controls as an Asset offers several benefits, chief among them, the ability to reuse settings and quickly edit and apply settings for different scenes and across disparate platforms.
Custom Render Texture is much like Render Texture but with additional, inscrutable options, and in the interest of making headway we can it.
Sprites are 2D graphics objects, but they have some unique properties. Sprites can be “sliced” to enable selectivity and scaling and “atlased (combined into one sheet),” for the purposes of animation and/or optimization. The Sprites that Unity creates are meant to be placeholders for the final art assets that will be imported later.
Sprite Atlas creates an atlas game object that is cable of assimilating multiple 2D graphics. You can specify entire folders full of 2D graphics to be automatically added to the atlas. Note that the atlas settings in Unity will take precedence over whatever rules are included in any imported files.
Tile creates a Tile object, essentially allowing you to select a texture or atlas with which to be imbued the properties of a tile. Those properties allow the texture to be “painted” onto the scene and mutated in real-time according to a set of rules specified in the inspector. In addition to the tiles' appearance, its physics properties can likewise be altered on-the-fly. This improves the speed and ease with which a 2D level can be created.
In the next menu divide are various linguistic forms of the word "animate." Note that the Animators and Animations referenced here are different from the animation and animator Components you'll find in the Component section. Complicated animations, generally speaking, are not created in Unity; they are imported and then managed using an Animator Controller. Unity's Animator animation system, Mecanim, is a state machine much like Playmaker. Perhaps because of the overlapping (bad) naming conventions you've picked up on the fact that animation in Unity can be a pretty messy affair. If so, kudos. This mess will necessitate its own lesson of delineation. We will cover animations, animators, avatars, and so on, in greater detail in Section 4.
Timeline creates a Timeline Game Object and spawns a Timeline View panel. You can then add Game Objects to the Timeline for the purposes of creating rudimentary animations. These animations are mostly of the transform variety; moving things from point A to B, rotating them, etc.,... nothing too fancy.
Signals are essentially a type of keyframe that you can add to the Timeline. There are three parts to a Signal: the emitter, the asset, and the receiver. They send and receive animation events.
Unlike Materials, which can define how an object looks, Physics Materials and Physics 2D Materials can define how an object behaves "physically." For example, you can apply a Bouncy material to a sphere to make it bounce like a ball once dropped. At this stage it is advised you rely on Unity's pre-made Physics Materials as opposed to creating your own. They are applied by attaching a Collider Mesh to an object and then defining its Material parameter. Standard Physics Materials are not created by default but must be imported as part of the Physics Materials Standard Package (now part of the Characters package).
GUI Skin and Custom Font are not relevant for the purposes of this course. We'll be using a different UI scheme. XXX
Legacy opens up the ability to create a Cubemap. A cubemap is pretty much just that; a texture map laid such that it can be applied to a cube. Usually the cube surrounds (visibly or invisibly) the game environment or game object. When it surrounds the game environment, usually it's creating a skybox. When the cubemap surrounds a game object or smaller area, it's usually to inform and create reflections on a relevant game object(s). Be aware that the term “legacy” usually denotes that the technology is old and likely being phased out. It also implies that there a more current technology available for use in its place.
Create UIElements Editor Window doesn't actually create anything. It simply opens the UIElements Editor Window. Why isn't UIElements Editor Window accessed from the Main Menu Bar's Window drop-down you ask? We may never know.
Brush allows you to customize the parameters of a Brush. Such editing is done in the Inspector Panel, as per usual. Brushes, in turn, can be used to edit things such as Terrain.
Terrain Layer functions as a Material of sorts with special properties. It can be painted onto a Terrain and tiled (repeated across the terrain). It can be properly applied to a Terrain even though the Terrain may not have its UVW coordinates made apparent. Terrain Layer can be created here or from a Terrain GameObject by clicking the Brush icon in the Inspector and choosing Paint Texture from the selector. Terrain Layer concludes the Create Menu.
Show in Explorer will open up the selected asset's location in your Operating System (folder).
Open will launch a selected asset in its default application as specified in your Operating System. The Operating System's specification can be overridden by Unity in Edit>Preferences>External Tools.
If you select an art asset and then Delete, the art asset will be augmented with wub-wub dubstep, unicorns, and rainbow sparkles. I'm kidding of course. It will be deleted. Forever. As will its Scene counterparts.
The previous method of renaming things throughout Unity was confusing and seemingly buggy. Having a Rename button available here isn't really much of an improvement. Its use here is only applicable to Assets; things selected in the Project View (and not those selected in the Hierarchy View). Things selected in the Hierarchy View can be renamed at the top of the Inspector. However the most universal, quick, and convenient way to rename anything is to select what you want to rename and then right-click and select Rename from the resulting menu. A text-box will open allowing you to edit its epithet.
Copy Path enables you to select an Asset in the Project View and, you guessed it, copy its path (its location in your OS' file system). You can then paste the path into your file explorer and the folder will open right to your Asset.
Unity allows you to edit multiple Scenes simultaneously. Open Scene Additive allows you to select a Scene and add it to the Hierarchy. This is typically done for the purpose of creating levels collaboratively or streaming large, open worlds. If your game design document contains the words “large, open worlds,” I regret to inform you that your scope is wrong!
Import New Asset allows you to import individual assets such as textures, meshes, animations, sounds, and scripts. This button is used quite often.
We mentioned previously how fundamental Package is as a file type. Import Package allows you to choose from Unity's pre-made Packages. Alternatively you may browse to a folder on your hard drive that contains a Custom Package, as you did with sec1Chap4Assets.unitypackage. You can also import Packages directly from the Asset Store, one of Unity's aforementioned killer features. Playmaker is one such Package you'll need to purchase and import from the Asset Store. Let's do that now.
Fig. 4.6 Shows the Asset Store Window with Playmaker selected
Click the Asset Store View tab in the top-middle of Unity. You should already be logged in
Select the Package simply titled "Playmaker" by Hutong Games, LLC
Click the Buy button that appears on the left side of the Asset Store Window
Complete the transaction by specifying the Payment Method and filling in all pertinent information
Download/Import Playmaker using the current Asset Store dialog
Note that all previously purchased or freely accessible packages can be selected for Download/Import by first clicking the download icon in the upper left of the Asset Store View.
Hutong Games, the creators of Playmaker have (relatively) recently create a Package which in turn manages additional Packages for its Playmaker Package. Why does Playmaker need additional Packages? Playmaker's use and awesomeness has become so expansive that it needs its own management system! This management system's name is Ecosystem, and, along with Playmaker should be one of the Packages that you should import with every Project you create. It can be downloaded from tinyurl.com/playmakerEco. You can then search for, download, and find reference material for all official Playmaker add-ons without ever having to leave Unity! With Playmaker and Ecosystem installed, Ecosystem is opened by choosing Playmaker>Add-Ons>Ecosystem (or alternately, Alt+E).
It's also worth mentioning that third-party developers have created Packages that extend or otherwise interact with Unity. For example, there is a third-party Package that bridges Playmaker and NGUI, a once popular GUI creation Package. The third-party Package then allows you to control NGUI using the Playmaker State Machine!
Note that Unity and Playmaker are updated frequently. Most third-party Packages are not. This disparity can cause Project breaking bugs. I have personally experienced issues where upon updating Unity caused mission critical Packages to fail to load. Manually loading the Packages caused Unity to crash. Updating the Packages did nothing. The solution was creating a new Project and updating and importing Packages in the order of their dependency (ie, Playmaker and NGUI had to be updated and imported before the bridging third-party Package was updated and imported; otherwise Unity would crash). I then exported the Project as a Package from the malfunctioning Project and imported it into the new Project. In likewise dire instances it may be necessary to delete a malfunctioning Package's folder from your Hard Drive.
Export Package will allow you to export your Project or portions of your Package. It can be difficult to search through the Exporting package window for the assets that you want to export. Instead you can highlight assets in the various Views and then click on Export Package. If nothing is selected in the Project View, everything will be selected by default in the Export Package View!
Fig. 4.7 shows the Export Package dialogue
By checking Include dependencies Unity will automatically include anything that the creation of the asset is dependent upon. For example, if you have a lamppost Prefab selected and Include dependencies checked, Unity will include the materials and any scripts necessary to complete the Prefab. You can save entire Projects as a Package. Remember to save and backup your Projects regularly. Unity upgrades and updates will often break Projects. Save your Projects as Packages early and often with no less than triple redundancies.
Find References In Scene allows you to select an asset in the Project View and then pare the Hierarchy View text list to only those elements that reference the selected asset.
Select dependencies works in the same way as the Select dependencies option in the Exporting package window. Some assets are comprised of other assets, for example, a Material is really just a group of Textures, a Shader, and its parameters. So you can select the Material, click Select dependencies and then it will find and select the textures wherever they are in the Project folder.
Refresh functions much like a browser's refresh function.
Reimport. This is for if you've altered an asset in an editor outside of Unity and need to reimport it. This is only effective if it's been saved to the location Unity recognizes.
It should not be necessary to Reimport All. In fact, when used, it displays a warning.
Extract from Prefab. It's only active when you don't have a prefab selected, and clicking it does nothing.
Run API Updater checks for obsolete APIs and will attempt to update any old scripts and assemblies. It warns that you should backup before proceeding.
Since we won't be using UIElements, there's no need to use Update UIElements Schema. That being said, what it does is in the name. XXX
Open C# Project replaces Sync Monodevelop in name only. It won't be used for reasons aforementioned.
Side note: in Create, the Boo Scripts options has been removed even though scripts written in Boo (a variant of the Python programming language) still work!
This concludes our Asset menu lesson! There's a wealth of important information to learn and review in this lesson; how to import and export Packages, Playmaker's Ecosystem, Prefabs, Materials, Textures, and Shaders, just to name a few! In the next lesson, GameObjects!