Sunday, December 7, 2014

Unity How To: Loading Assets at Runtime

This is the first installment of a new series in which I intend to create tutorials on various aspects of using the Unity game engine. These are intended to be aimed at those who know the basics of the engine but don't yet have extensive knowledge of it. I hope that these are particularly of use to artists or designers who are getting their feet wet with scripting in the game engine. If there are topics that you would like me to cover in a future post, I encourage you to share them in the comments section. Now, without further adieu:

Loading Assets at Run Time

What use is this?

In Unity, most assets are assigned while working in the scene view. An example is when a texture is assigned to an object or an asset is dropped into a slot on a script in the inspector. This is how Unity determines which assets it has to have loaded at any given time and determines which assets are included in a build. However, there are times when loading assets into the inspector can be tedious or inappropriate.

For example, if a single character has many 50 outfits that are implemented as alternate textures, then loading all 50 textures into the inspector becomes tedious and a waste of memory, as Unity will load all 50 textures into memory. An alternative approach is to load the outfit at the start of the game and then unload the rest of the textures. This allows the game to reclaim unused memory and run more smoothly.

A Simple Example:

In this example, we will create a simple scene that changes the texture of an object when the scene starts. There are a few items that are neccessary to set up in advance. Only assets that exist in the Resources folder can be loaded at run time. Simply create a new folder at the top level of the Assets folder and name it "Resources", as shown here. In that example, the material "Blue" can be accessed while the assets in the "SampleProject" folder would be inaccessible.


Now, we need to add an object to our scene to place the material on. Create a blank scene and add a cube to the scene. Position it so that it is visible to the camera. It should look similar to this. I have elected to add a Red material to the cube to emphasize the transition, but this is not necessary.


Now, we need to create the script below and add it to the game object we added to the scene:


The basic syntax for loading an asset from the resources folder is shown in line 11. That syntax is "Resources.Load<Type>(Asset_Path)". In the Type location, the type of asset is specified. In this example we are loading in a Material, but this can be changed to accommodate other types such as a Game Object in the case of loading a prefab. The "Asset_Path" is a string that holds the location of the asset relative to the Resources folder. I have elected to create a string variable in line 6 which holds the location of the asset as a matter of good practice, but this can be hard coded.

Now, simply drag and drop the LoadResources.cs script onto the cube object that was created earlier, and press play. When the simulation is run, the "Blue" material should be accessed and assigned to the cube.

Talking to the Player (Part II)

In my last post, I talked a little about creating a teachable moment in a video game though breaking the 4th wall to address the player directly. However, there is another more subtle and arguably more effective way to cause a player to question his beliefs. Ask the player a question that they cannot answer.

Now, there is a lot to say on this topic and I cannot summarize it better than the good folks over at Extra Credits (link below), so I encourage you to listen to what they have to say. However, what I intend to explore here isn't what the player learns from the choice itself. I'm interested in exploring the mechanics of presenting that choice effectively.



I'm going to use an example that recently forced me to pause the game and think in just such a fashion. The example comes from Dragon Age: Origins. Be warned, there will be spoilers.

Still with me? Good. The set-up for this scenario involves the dwarves and their centuries long battle against the darkspawn. Originally, there were 11 great dwarven kingdoms connected by a large network of underground tunnels known as the Deep Roads. The Darkspawn also live underground and have been constantly attacking the dwarven kingdoms through the Deep Roads. The dwarves have steadily been pushed back; only 2 of the original 11 kingdoms remain.

This is where the Golems come into play. Centuries ago, a dwarven inventor figured out how to create Golems, soldiers of living stone. For a time the dwarves used the golems to hold the line and even push back the darkspawn, but the artifact used to create the golems was lost.

In the game's storyline, the player eventually goes into the Deep Roads and finds the lost artifact that was used to create the golems. However, when they do they are met by the spirit of the dwarf who originally created that artifact. He reveals that the reason he sealed it is because the secret of the golems is that creating a golem requires the sacrifice of a living person – their soul is imprisoned in the golem and used to power it. At first, he only used volunteers, but then criminals were used and eventually the dwarven king started ordering his political enemies be used. He then begs the player to help him destroy the artifact rather than allowing it to enslave more souls. The player is then faced with a choice of recovering the artifact or destroying it for good.

The choice is re-enforced within the game's mechanics. Preserving the artifact allows the player to add Golems to the army they are trying to assemble and makes them usable during the game's final battle, but one of the characters in your party feels strongly enough about the evil of the artifact that they will leave the party and fight you.

The way the choice is framed is also significant. The choice is either-or ; there is no middle ground option because the player cannot both destroy and preserve the artifact. This prevents the player from ducking the decision. It is also important that neither decision is presented as the clearly superior choice – both of them will require sacrifice.

Furthermore, the decision is treated seriously by the game and carry consequences. There are no snide comments to the player to make light of that decision.


All of these elements are significant in crafting a decision that forces introspection from the player.

Talking to the Player (Part 1)

Recently, something happened that hasn't happened in some time. I was playing a game and the story presented me with a difficult choice. I thought about it briefly, then stepped back from the keyboard and thought about it some more. It made me question my beliefs. When I came back and made the choice, I felt like I'd also learned something about myself. Presenting this kind of choice to the player is one way that gaming can move beyond simply being viewed as entertainment and become respected as an art form.

I have seen games make attempts at creating these teachable moments in two main strategies. In the first, the game at some point breaks the 4th wall and talks directly to the player. In the second, the game creates a moment where the player is faced with a choice that prompts their own introspection. Both strategies have a set of criteria that has to be effective for the message to resonate, and I will explore some of the mechanics necessary for creating effective teachable moments. Be warned, this post contains spoilers.

In the first strategy, in which the game will break the 4th wall and talk directly to the player, I can think of no better recent example of doing this well than Spec Ops: The Line. Now, much has already been written about this game, both its message and its plot. What doesn't get mentioned much is that the set-up for that final sequence in which the hero is called out for his actions. For that call-out to be effective, the player needs to feel agency over their actions up to that point.

This feeling of agency is reinforced through the very mechanics of how the player's actions are presented. For example, there is a time in the game when the player's squad is surrounded by a hostile crowd. These people aren't soldiers, but they greatly outnumber the player and they're angry. As they slowly close in, the player has only his 2 squadmates and his gun. Many players report at that point that they start firing into the crowd, killing the civilians. As soon as they start firing, the crowd flees. Mechanically, no prompt was given and the player wasn't forced to kill them. In fact, if the player stays calm and fires into the air, the crowd will still flee. The player chose to fire into the crowd – chose to take that action in a way that makes them feel like they own it. This makes the call-out at the end all the more effective – because the situation is constructed in a way to make the player feel like they had agency for the very actions that are being called out.

An example of this being done poorly is the recent Far Cry 4. If you're not interested in spoilers then you should stop reading now.

Still with me? Good.

The player takes the role of a person returning to their native land (which is embroiled in a civil war) to spread their mother's ashes. The player then gets embroiled in the civil war. At the beginning of the game, the player meets and escapes from Pagan Min, the country's dictator. As the storyline progresses, the player is forced to choose between rebel factions and whether or not to kill main characters. Much later in the game, the player confronts Pagan Min again. The Min will call out the player for his actions, even going so far to point out that the player doesn't even have a motivation for what hes doing.

Yet, this attempt at calling out the player is hampered by the game itself. For instance, during their first meeting, Pagan Min tells the player that he has cleared his calendar so him and the player can “tear stuff up”. There is an optional ending where the player doesn't escape from Pagan Min and instead he takes you directly to the place you wanted to place your mother's ashes by helicopter. After you have done so, then he allows you to depart the country by helicopter. This allows you to complete the game in under 30 minutes without ever once picking up a gun. As the screen turns to black, however, Min makes one final statement: “Do you feel better now? Get that out of your system? Good. And maybe now we can finally shoot some goddam guns.” He isn't speaking this line in character - he has no reason to. He is speaking to the player, asking them if they're done with their adventure in nonviolence and can we please get back to the gun play.

That style of self-aware humor is sprinkled throughout the game and interferes with the game's attempts to question the actions of the player. The player is being continually reminded that they're playing a game, which encourages the player not to take the events too seriously and instead lay the blame for their action's on the game's narrative and their protagonist instead of taking ownership of their actions.

This isn't to say that Far Cry was a terrible game or the Spec Ops was the best game ever. However, Spec Ops' combination of taking itself more seriously and giving the player as much agency over their decisions as possible made their message calling out the player's actions much more effective. If one wishes to call out a player directly, these are good lessons to learn.