+ All Categories
Home > Documents > Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways...

Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways...

Date post: 05-Aug-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
14
Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015 Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected] Prefab Basics Introduction Daggerfall Tools for Unity 1.3 debuted a basic prefab system to help developers expand procedural objects. This tutorial explains the new procedural container prefabs and demonstrates ways they can be useful to customise your environments. Note: The prefab system will continue to evolve in future updates. The implementation in Daggerfall Tools for Unity 1.3 is just a starting point. Getting Started Please create a new project and scene by following the Getting Started tutorial. To summarise: Note: You must be running Unity 5.0.1 or later. Create a New Project and import Custom Package “Daggerfall Tools for Unity.unitypackage”. Empty default scene and add DaggerfallUnity and Player (or PlayerAdvanced) prefabs. Prepare Lighting configuration and set player Rendering Path to Deferred. Set Quality settings to Fantastic. Set Arena2 Path of your local Daggerfall installation. Create Run input binding and optionally invert mouse look. What’s a Prefab Anyway? A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects) that has been preconfigured with a set of components and properties intended to be reused over and over again. Unlike simple duplication where each object is an individual, prefabs maintain a link back to their parent template allowing you to quickly push changes to all prefabs in the scene. If you edit a prefab directly from Hierarchy then changes are made to that instance only, just like a normal GameObject. However, if you edit a prefab in the Project view those changes will be applied to all linked prefabs in the scene (linked prefabs have blue text in Hierarchy). This is useful not only for creating templates of complex objects, but also for rapidly changing every instance of that object in the scene. When editing from Hierarchy you can also revert to prefab or apply changes to the parent prefab. If you’d like to read more about prefabs, the next step is the Unity manual. http://docs.unity3d.com/Manual/Prefabs.html
Transcript
Page 1: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Prefab Basics

Introduction

Daggerfall Tools for Unity 1.3 debuted a basic prefab system to help developers expand procedural

objects. This tutorial explains the new procedural container prefabs and demonstrates ways they

can be useful to customise your environments.

Note: The prefab system will continue to evolve in future updates. The implementation in Daggerfall

Tools for Unity 1.3 is just a starting point.

Getting Started

Please create a new project and scene by following the Getting Started tutorial. To summarise:

Note: You must be running Unity 5.0.1 or later.

Create a New Project and import Custom Package “Daggerfall Tools for Unity.unitypackage”.

Empty default scene and add DaggerfallUnity and Player (or PlayerAdvanced) prefabs.

Prepare Lighting configuration and set player Rendering Path to Deferred.

Set Quality settings to Fantastic.

Set Arena2 Path of your local Daggerfall installation.

Create Run input binding and optionally invert mouse look.

What’s a Prefab Anyway?

A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects) that has been

preconfigured with a set of components and properties intended to be reused over and over again.

Unlike simple duplication where each object is an individual, prefabs maintain a link back to their

parent template allowing you to quickly push changes to all prefabs in the scene.

If you edit a prefab directly from Hierarchy then changes are made to that instance only, just like a

normal GameObject. However, if you edit a prefab in the Project view those changes will be applied

to all linked prefabs in the scene (linked prefabs have blue text in Hierarchy). This is useful not only

for creating templates of complex objects, but also for rapidly changing every instance of that object

in the scene. When editing from Hierarchy you can also revert to prefab or apply changes to the

parent prefab. If you’d like to read more about prefabs, the next step is the Unity manual.

http://docs.unity3d.com/Manual/Prefabs.html

Page 2: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Prefabs in Daggerfall Tools for Unity

You’ve already seen prefabs in Daggerfall Tools for Unity – we use the DaggerfallUnity prefab every

time we create a new scene. This and any prefabs in Particles, Player, and World folders are just

standard Unity prefabs. We don’t need to cover them much here as they are fairly self-explanatory.

Prefabs in the Scene folder are a different story however. Unlike regular prefabs that are complete

representations of a GameObject, the Scene prefabs are considered both a template and procedural

container. Besides acting as a standard prefab template, these objects are also merged with

procedural data at runtime. Let’s use dungeon doors as an example.

In the Project view navigate into Prefabs/Scene and select the DaggerfallActionDoor [Dungeon]

prefab. Looking at Inspector this is what we see.

There are a few components here already. We don’t need to go through them one-by-one as you’ve

already played with dungeon doors back in the Dungeon Basics tutorial. What is conspicuous here

Page 3: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

are the components missing from this prefab, such as a MeshFilter and MeshRenderer. When do

they get setup?

The short story is they are added by scene builders at runtime or when importing a dungeon into

your scene. Daggerfall is notable for its procedural generation of environments, a stark difference to

the typical Unity workflow. Rather than a prefab for every different type of door, a single prefab

container is used to combine both fixed and procedural data.

So when constructing a dungeon scene, the procedural builder will follow this general process for

every door.

1. Instantiate empty DaggerfallActionDoor [Dungeon] prefab.

2. Procedurally load mesh data from Daggerfall’s files based on the door mesh at this location

(a single dungeon can use many different types of doors, of many different sizes).

3. Configure BoxCollider to fit dimensions of mesh.

4. Configure any other properties as needed.

Once everything is in the scene, the resultant DaggerfallActionDoor [Dungeon] will look something

like below.

Page 4: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

This is what we mean when saying the Scene prefabs are both template and container. They not only

provide a starting template, they are built-on by procedural layouts. Sometimes complex hierarchies

of objects will be added. This solution is unique to a game world like Daggerfall’s which relies heavily

on procedural generation over manual placement of assets.

That’s not to say you can’t create assets manually. For example, there’s nothing stopping you from

manually creating your own custom dungeon blocks and mixing them in with Daggerfall’s fixed

palette of dungeon blocks. We’re going to create something along these lines a bit later in this

tutorial.

Starting Small

To demonstrate the flexibility of the prefab system, let’s do something easy to begin with.

The first thing we’ll do is duplicate one of the existing prefabs, rather than edit those shipped with

Daggerfall Tools for Unity. Go into the Prefabs/Scene folder in Project and select the DaggerfallLight

[Dungeon] prefab.

Duplicate this prefab using menu Edit > Duplicate (or Ctrl+D in Windows). This creates a copy of our

prefab called DaggerfallLight [Dungeon] 1. Rename it to something like CustomDaggerfallLight

[Dungeon].

Next, select DaggerfallUnity in Hierarchy and locate the light prefab settings in Inspector.

Change the Dungeon Lights prefab to your CustomDaggerfallLight [Dungeon] prefab by dragging

and dropping from Project into the appropriate field in Inspector. The above settings should now

resemble the following.

With that change made, go ahead and import the dungeon Daggerfall/Privateer’s Hold just like we

did in the Dungeon Basics tutorial. If you click on any light in the scene (shown by a lightbulb gizmo)

then you will see our custom prefab has been instantiated for every dungeon light with the prefab

link intact.

Page 5: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Now setup your Player or PlayerAdvanced transform to position 12.75, 1, 41.8. This places your

player at the dungeon start.

Hit Play and check your basic dungeon looks alright. When you’re done, hit Play again to return to

edit mode.

Next we’ll do something with that custom light. In your Project view select the custom light prefab

we created earlier. It should still be in the Prefabs/Scene folder. You can also zoom straight to that

prefab by selecting any light in the scene then clicking Select on the prefab buttons in Inspector.

With your custom light prefab now selected, change the Color to RGB 255, 0, 0 (click Color field to

edit). This changes all of our lights in the scene to red. Hit Play to test it out.

So that’s a little creepy, but we can make it even more so. Select your custom light prefab in

Prefabs/Scene again. Now in Inspector click Add Component and add the Daggerfall Audio Source

component. We’re going to place a custom sound on every light with the following properties.

Page 6: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Preset is the behaviour of your Daggerfall Audio Source. The Loop If Player near setting will

loop the selected sound effect only when player is inside the radius of effect. This helps

reduce the load on audio system to only what the player is close enough to hear.

Index is the sound effect we want to play from Daggerfall’s sound library. This is the sound

of people talking in the background.

Sound Finder can be used to locate sound effects from Daggerfall’s library of sounds. You

can’t use this from a non-instantiated prefab however, so just ignore for now.

Hit Play and go stand near some lights. You should hear the sound of many people talking, the

volume of which depends on your distance from the light source. Considering you may be affected

by several lights at any time, you may hear these voices at anything from a whisper to a din.

Note: If you want to explore the dungeon fully, swap out your Player for PlayerAdvanced which is

completely setup as per Dungeon Basics with weapons, etc.

You aren’t limited to just adding components from Daggerfall Tools for Unity either. You can extend

these prefabs however you like. For example, the below video sets light intensity in the entire capital

city of Daggerfall based on the amplitude of playing music. Your prefabs can be as useful or as silly as

you like.

https://youtu.be/odhTciw71V4

Stepping Up

Now that we’ve seen the basics of setting up a custom prefab, let’s step it up a notch and make a

custom dungeon of sorts. This will involve a little scripting but should be easy enough to follow even

if you’re not overly familiar with programming.

First of all, let’s start with an observation. Tidy up your Hierarchy by contracting the block nodes like

below. Then click on DaggerfallBlock [S0000999.RDB].

If you zoom out (or press F in Scene view) you will notice that S0000999.RDB is the central block of

Privateer’s Hold, and it’s capped on all sides (north, south, east, and west) with border blocks.

Page 7: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Above: The central block of Privateer’s Hold S0000999.RDB. The ‘S’ means Special. B=Border, W=Wet, M=Mausoleum,

N=Normal. Special blocks are typically used for quest areas and other custom content, and are not guaranteed to tile with

other Border and Normal blocks. Border blocks seal a dungeon so it’s not open to the Void.

Notice how the Transform of DaggerfallBlock [S0000999.RDB] is at 0, 0, 0. This is because

S0000999.RDB is the root block. All other blocks are offset from this central block. You can read

more about dungeon layouts on the below UESP page.

http://www.uesp.net/wiki/Daggerfall:Dungeons

Right-click DaggerfallBlock [S0000999.RDB] in Hierarchy and select Delete. This will remove it from

the scene, leaving a huge gap and your player hovering over the abyss. Don’t worry, we’re going to

move the player and do something cool to fill in that gap.

Select your Player or PlayerAdvanced in Hierarchy and set their Transform as below.

Page 8: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

This positions our player just outside the root block area in one of the border blocks. If you go into

Play mode now, you should be looking out a doorway into the Void. Watch your step, this dungeon

crawl is becoming stranger by the minute.

Now let’s do something about that gap in our dungeon layout. Select your DaggerfallUnity object in

Hierarchy and import block N0000016.RDB.

This will fill in our block with another area, entirely changing the nature of our dungeon. Hit Play and

go exploring. Don’t forget to hit Play again when you’re done to return to edit mode.

Now we’re going to get creative. If you haven’t already, create another folder in Project called

MyScenes and save your scene into this folder. Always remember to save your work as you go, just

in case something happens.

Select DaggerfallBlock [N0000016.RDB] and disable it by clearing the enable check box in Inspector.

This will leave the prefab in your scene Hierarchy, but keep it inactive for now (notice how the object

is dimmed).

Page 9: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Now import the block N0000042.RDB and disable it just like we did above. Repeat this process until

you have the following disabled blocks, all at position 0, 0, 0.

N0000016.RDB

N0000042.RDB

N0000024.RDB

N0000025.RDB

N0000026.RDB

N0000066.RDB

Your Hierarchy should resemble the following, with all 6 new blocks disabled. It doesn’t matter what

order they appear in your Hierarchy.

What we’re going to do next is enable a random block every time you start the game, resulting in a

variety of possible dungeons.

This obviously isn’t the most efficient way to do this, but it will require minimum scripting and

demonstrate the interchangeable nature of dungeon blocks. It also shows that it’s possible to mix

your own custom dungeon layouts with Daggerfall’s procedural content. You just need to ensure

your hand-made block prefabs respect the same dimensions and door locations that Daggerfall uses.

Click on menu GameObject > Create Empty and rename this new object to Block Enabler. Reset it’s

Transform if you like, but it’s not important for what follows.

With Block Enabler selected in Hierarchy click Add Component in Inspector. Add a New Script and

call it EnableRandomBlock. Ensure C Sharp is selected for the language then click Create and Add.

Page 10: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Your Block Enabler GameObject should look like the below in Inspector with Enable Random Block

(Script) component now attached.

Next, double click the EnableRandomBlock script field highlighted above to open it in your preferred

code editor.

Note: Setting up your code editor is outside the scope of this tutorial, but it’s done from menu Edit >

Preferences then External Tools tab. For many new Unity users, this will be set to MonoDevelop

(built-in). This tutorial assumes you have MonoDevelop, but Visual Studio with UnityVS is a

commonly preferred setup.

With EnableRandomBlock.cs script is open in your code editor, delete all existing code then copy and

paste the code below into your script. When you’re done, save your script and return to the Unity

Editor.

Page 11: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

using UnityEngine; using System.Collections; using DaggerfallWorkshop; public class EnableRandomBlock : MonoBehaviour { // This is an array of blocks to populate in editor public DaggerfallRDBBlock[] Blocks; void Start() { // Cannot continue if Blocks array null or empty if (Blocks == null || Blocks.Length == 0) return; // Choose a random index within range // The max value of Random.Range() is exclusive // which means the range is from 0 to Blocks.Length-1 int index = Random.Range(0, Blocks.Length); // Enable the block GameObject if (Blocks[index]) { Blocks[index].gameObject.SetActive(true); } } }

As this is a basics tutorial, we won’t cover this script in much detail. Essentially it’s just a few lines of

code to enable a random block from an array we provide. Hopefully the comments will help explain

what’s going on.

If you started the game now, there would still be a gaping hole in the middle of your dungeon. We

need to fill up the Blocks array with the disabled blocks we created earlier.

With Block Enabler selected in Hierarchy your script component in Inspector should now have a

Blocks array available. This is shown because our script has a public field of this name. You may need

to click the little arrow to the left of Blocks to expand the highlighted field.

Change the Size value to 6. This is the number of blocks we created earlier. Now drag and drop each

of those disabled blocks we created earlier from Hierarchy into the numbered element fields in

Inspector.

Page 12: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Note: It’s OK if your blocks are in a different order than shown. Just try to avoid any duplicate blocks

in the array elements.

Now hit Play to start the game and a random block will be set active. Every time you start and stop

the game, a random block will be selected creating a slightly different dungeon each time. While this

is not the exact approach Daggerfall uses, it should give you a good understanding of how the blocks

fit together.

Above: Block N0000026.RDB was selected randomly from our list, creating a semi-random dungeon layout. Keep in mind

that pseudo-random numbers are “clumpy” and you might get the same layout multiple plays in a row.

We didn’t really use the DaggerfallBlock [Dungeon] prefab much in this section of the tutorial,

instead we just instantiated them directly from the Importer as usual. But it was worth going over

the blocks and how they fit together, because future versions of Daggerfall Tools for Unity will

Page 13: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

expand prefabs towards being “self-assembling”, letting you construct standalone scenes from raw

prefabs alone. Let’s have a look at a prefab that’s already self-assembling.

Custom Enemies

The DaggerfallEnemy [Demo] prefab in Prefabs/Scene is one such self-assembling prefab, with

editor scripts capable of instantiating any of Daggerfall’s enemies from thin air. Let’s take a look by

creating a custom enemy.

Note: The [Demo] suffix is due to enemy AI script originating from the DaggerfallWorkshop.Demo

namespace. This namespace is not a core part of Daggerfall Tools for Unity, but a collection of scripts

demonstrating how certain tasks can be accomplished.

First, locate your Player or PlayerAdvanced object in the scene. The quickest way to do this is select

your player in Hierarchy then press F (with mouse over Scene) to find the player in the Scene view.

Navigate your Project view to Prefabs/Scene then drag and drop a DaggerfallEnemy [Demo] prefab

directly into Scene, off to one side of the player.

Select DaggerfallEnemy [Demo] in Hierarchy then use Inspector to select what Enemy Type you

would like to instantiate. This is set to Skeletal Warrior by default, but feel free to change it to

something else.

Once you’ve selected an enemy type, click the Apply Enemy Type button to set this as your enemy.

The enemy will probably be half way into the ground, so adjust their Y position manually or click the

handy Align To Ground button to do this automatically.

When you’re happy with the new enemy, click the Play button and prepare to fight! If you’d like a

more docile enemy, set the Enemy Reaction to Passive and click the Apply Enemy Type button

again.

Page 14: Prefab Basics · This tutorial explains the new procedural container prefabs and demonstrates ways they ... A prefab in Unity3D is a special type of GameObject (or hierarchy of GameObjects)

Daggerfall Tools for Unity Tutorial Prefab Basics V1.3.31 [Unity 5.0.1] | 06/08/2015

Devblog dfworkshop.net Forums forums.dfworkshop.net Source github.com/Interkarma/daggerfall-unity YouTube youtube.com/daggerfallworkshop Twitter twitter.com/gav_clayton Email [email protected]

Above: He’s most definitely not happy to see you.

Conclusion

This concludes the Prefabs Basics tutorial. With a bit of experimentation and clever coding, there are

a myriad of ways you can override the default behaviours of objects in your own creations.

Future versions of Daggerfall Tools for Unity will expand on the prefab system to help you create

larger and more ambitious projects based on the Daggerfall assets.

If you create something cool (no matter how weird and wonderful) let me know and I’ll post about it

on www.dfworkshop.net and @gav_clayton.


Recommended