Game development on .NET

Basics of Unity

dotGame
Basics of Unity
5 (100%) 1 vote
(Video Transcript)
Hi guys, welcome to .GAME. In this episode, we’re gonna take a look at the basics of using Unity including going over the UI. Looking at the concepts, the core concepts that are involved in making a game. And then we’re gonna dive into setting up our scene and the scene’s going to be use for several future videos. So we’re gonna build off of this over the course of some time. We’ll also take a very brief look at some of the scripting, more of like an introductory to get you going. So you can start to play around with Unity and some of the game logic that you can build off of it. So to get started, we can take a look at this project that I have set up. So I’ve already gone ahead and created some of the scene that we’ll be working with. And we’re gonna build a little bit off of this scene and we’ll talk about some of the components that are tied to it. So don’t worry too much about the fact that some of it’s already set up. Taking a step back, if you take a look at Unity and this is an engine that you can go and get online, we’ve got several kind of panels that are set aside.

So this top left one is known as the hierarchy. And this is basically a list of all of the objects that are sitting in your scene. So if we take a look, we have this well that’s sitting right here, If I just click on it. You can see this environment node dropped down and we have the well listed. So you can see that it’s just literally a list of those game objects. If you look down, we have this Project tab. This is basically a list of all of the assets that you have in your project file. So it might not be everything that’s sitting in the scene but it’s a list of stuff that you are going to be working with in one way or another within your game. I’ve already pre-populated this with everything that we’re going to be working with in this episode. In the center, here we have the console. Now this is going to output any error messages, any informational messages, things like that. That you would typically see when scripting or really if you happen to just run into any errors.

There’s also the Animation tab, which we’re gonna not really look at too much in this episode, but probably the next one. This is where you can set up various types of animations. And then we also have the Animator Controller, which again we’re really not gonna look at in this one. But this will let you kind of fine tune what animations would trigger when. So you can kind of set up parameters and builds logic that basically dictates when and how an animation would look. Over on the right-hand side we have the Inspector. Now the Inspector is basically a list of components that are tied to a specific game object. Now I know that that can sound a little bit confusing. So we’ll explain what a game object is and what a component is momentarily. But for now the key thing to just hone in on is this is where all of the components are that make up a particular game object. And then the last tab that we currently see or I’m sorry, not the last tab but another tab that we see is the Navigation area.

Now we’re gonna touch too much on this in this episode, but the next episode actually going to focus on player navigation. So we’ll actually dive pretty deeply into this particular tab. So the last two that I wanted to go over is the scene view which is this tab here. And this is where basically you can go in and you can set up where your objects are, your game objects are in the scene as rendered or viewable by the camera. And then the third or sorry, I don’t know why I keep adding tabs, but the next tab is the Game Op tab. And this is actually the view of the camera. So, this is where we currently have the camera positioned in the scene and this is what the players would actually look at. Up at the top, there’s play, pause and then next. So if we were to hit play, nothing is really gonna happen cuz we don’t have any actual game logic built into the scene. But we can see as soon as Unity decides to go, there it goes. But this is ultimately again what the player would see, you can’t actually do anything yet, but we’ll start to build that game logic out.

All right, so kind of jumping into what a game object is and then a quick overview of component. So the best way to think about a game object is it’s basically an object that gets defined by its components. So, what I mean by that is you can actually right-click on your hierarchy and do a create empty. What I’ll do is I will create an empty game object. If we hop into our scene view, we can actually see this sitting in world space. There’s nothing really tied to it. We can just kind of see the the x, y and z axis. But nothing else because there’s nothing that’s really set to render this. This is quite literally an empty game object. If we look over at our components, there’s the name game object. And then it has this component called the transform. Now every game object has this and what it does is it defines its position, rotation and scale as it relates to the game world. So in this case, it has a position of 48, 9 and really negative 0.44. We can adjust the position to be 0, 0, 0 and we can see that it kind of moved over to where the well is.

Which is basically our game world coordinates of 0, 0, 0. The rotation is also set to 0, 0, 0, if we were to rotate it you really wouldn’t notice much, because again there’s not much in the way of visibility. Same with scale, scale is 1, 1 and 1. We’ll take a look at these properties and what they actually mean a little bit later. So this, like I said, is an empty game object. And those are pretty useful for various reasons. So for example, if I click on this environment game object, you’ll notice it’s got a name. I’ve changed the name from game object to environment, but it really only has the position, the rotation and the scale. And at some point I guess when I created this, I messed up the positioning, usually I try to keep that at zero. But that’s gonna shift everything over, so I’m not really gonna mess with that right now. But underneath it, you can see it’s actually got quite a bit of stuff. So I’ve actually defined the environment’s game object purpose as basically being the parent container for all of these little objects.

Really honestly just because I didn’t want to stare at a ginormous list in my hierarchy. I wanted things to be a little bit more organized. However, that does let me do a little bit more fine control. So for example, if I wanted for some reason to shift all of this stuff over. Instead of clicking on each individual item and just moving it, right now I have the ground and I’m just shifting it over. If I wanted to move everything, I can click on the parent and move that instead. So there’s a lot of practical purposes also for doing the hierarchy like I’ve done. And that stuff will become a little bit more obvious as we start to progress through it. So if we take a look at one more game object, again talk about the component notions. Remember I said the camera was up here kind of looking roughly down here you can see in this view or if we pop over to the game view, you can kind of see that it’s here. Really though, what a camera is is it’s a game object that’s essentially empty that has a series of components that define its purpose in the game world as the camera.

So this particular component right here is one of them along with the other ones. We’re not gonna dive into each individual component right away, cuz that would just take quite some time. Instead, we’re gonna slowly explore what the different components are through each of the episodes and we’ll build off of them some. So taking a step back, I’m gonna go ahead and delete this empty game object. When actually I’m sorry, I jumped ahead. Let me add that back in. You can actually view a list of all the possible components and start to play around with them in this Add Component button on the Inspector. So if I click that and I remove what I have been typing in at some point and time, you can see quite a large list. Various components that can be added. So we’ve got things anywhere from mesh renders and filters which will make a little more sense as we go to physics to audio. All sorts of stuff that’ll kind of help you get going with defining again a game object’s purpose in the game.

All right, so that’s a brief overview of the UI and some of the concepts behind it. We’ll talk about scripting in a bit, but for now let’s go ahead and build up our scene. So what we’re gonna do over the course of several episodes is we’re gonna kinda slowly build off of or build up an RPG style game. Honestly, I’m not entirely sure how far we’ll take it. But I do have some plans in mind. I’ve been dying to build up an inventory system and merchant stuff. So I think we’ll probably end up doing some of that. Before we dive into it, we need to actually get some models out there and build out the scene that we’ll be working with to start. So what I have here is under this folder of models, I’ve got a few things. In particular, I have this wizard or witch. I guess I called her two different things. I was calling her a wizard and then everyone else kept calling her a witch because of her hat. But this is her. So it’s a model that I just started working on. So she actually does not have any animations or anything tied to her yet.

But that’s fine for what we’re gonna be doing. If you drag her and drop her out just into the environment. Either you can drag straight into the scene or you could drop in the hierarchy and that would put it at zero zero. I dropped her in the scene. But if we click, we can see the position and that position, rotation and scale have altered. If we set it again to zero, zero, which we really don’t want, but that’ll put her right about here. Let’s move her a little bit forward. This ensures she’s at least sitting on the ground and is positioned properly. You can see she has an animator component on that. I started to work on the animations and I rigged her out, but she actually isn’t ready to go. So we’re gonna just remove that component cuz there’s no point in having it just yet. So we have our witch. And we’re gonna rename this to be the player. This is going to be the character that we’ll control. And then what we wanna do is the first video series that we’re gonna end up doing is gonna have the player is going to be able to interact with a storekeeper.

We’re gonna have something pop up. We’ll be able to do some shopping and particularly they’re gonna buy this cute little hat that I also made, sort of a little wizard hat right here. And then we’ll end up putting that on their head and showing it in their inventory, as well. So for now let’s go ahead and build our storekeepers. So we only really have the one model that we’re going to be working with. But we are going to retexture, recolor that model to make him look like a different person. So what you can do is click on Player, and then if you hit Ctrl+D, that’s going to duplicate that object. So, if we move this, we’ll notice that we now have two. My word of advice is, be careful with that, cuz you get into the habit of hitting Ctrl+D in other places. And in Windows that will delete your files, which I’ve learned the hard way not to do. So you have to try and break that habit, if you end up relying on it a little too much. So we’re gonna rename this to be Merchant.

And then I’m actually gonna position, [INAUDIBLE] cave merchant person here. So I’ll put her here. When you rotate, you have a couple of options. So we can use these to kind of move. Obviously, you can set these parameters. You can also hover, and it switches to, I don’t know how well you can see that, but it kind of switches to two little arrows. And what you can do is you can kind of drag those. So you can see her pivoting just a hair. We’ll pull it back a bit, actually I’m gonna want her, oops not on the z, y. We’re gonna rotate her on the y, I’ll make her face, that’s about 130. I like whole numbers, that just makes working with things a lot easier. -28, so we’ll just round everything. And if you hold down, so if you actually, when you go to move, you hold down the Ctrl button and then drag it to the side. It’s actually gonna snap it to the grid, which can also be quite useful. So the other thing is, again, she still kind of looks similar, so we want to change that up.

And the way that we can do that is we can work with what’s known as the material that’s on this particular object. So if we expand her hierarchy. So again it’s very similar in that this top one is just a container object to organize her. Underneath it we have body, and that’s basically the mesh that’s tied to her. And then we can see that there are these things right here. The skin, the dress base, the boots, the gem, the dress trim, and the hair. What these are, are these are actually materials. And what a material is, is a definition of how a surface renders. And I know that’s kind of vague. And it’s vague in the sense that you can define it in quite a different way, several different ways that kind of make it up. So the first is a shader. And what a shader is, is it’s basically a script that calculates the color of each individual pixel. Unity itself comes with several shaders that are available. The standard being kind of the most commonly used one, but you can start to write your own shaders.

And that’s actually an area that I do wanna dive into as well. So, hopefully, we’ll have some tutorial videos that will teach a little more about writing up the shaders. And then the third thing that kind of we’ll make up a material is known as a texture. In this case, I actually don’t have any textures tied to it. I’d actually originally gone down that route, and I wanted to make it so it was easier to switch the coloring of the model without having to redo the textures. So instead of doing that, I actually just have a color that’s being applied. So real quick I’m gonna kinda back it up a bit. And so you can kind of hopefully see this a little more. I don’t think I can make much, I don’t think it’s gonna let me make much space unfortunately. You know what, we’ll do this, I’ll kind of move this person. So I want to show you something that’s kind of key when working with materials. Which is why we have two different players and we have basically two different objects.

Here the materials are actually shared. So what I mean by that is, if I click this merchant and adjust the scale, and I adjust it to 2. You notice that the player didn’t change. That’s because they are two different instances of the game object. However, if I were to click Body and then click on the hair, the skin. Let’s do the hair, so it’s a little more obvious. And then I click on the hair. If I were to change it to something a little bit crazy like hot pink, you’ll see that it’s actually changing on both models. That’s because both of these are sharing the same material. Now, we can fix this relatively easy by just adjusting what’s on this merchant. So if we go ahead and collapse all of these models and we go under this prefab folder. I’ll talk about what a prefab is in a second, for right now we’ll skip over that, so I’m gonna demonstrate it. And just add a new folder called merchant. What I can do is hop into Models, go under Wizard, and these are a list of all of the materials that we just looked at.

So if I were to keep the hot pink hair, or really just change it here, we’ll see that it applies to the model. What we can do is we can duplicate these materials and then change just the ones that we would want changed for our merchant. So, with this folder selected I’m gonna hit ctrl+D. And that’s gonna create a Materials1 folder and I’m just gonna drag it under this Merchant folder. And I’m gonna rename it, be just Materials and then I’ll click it. So what we’re gonna do is we’re actually gonna change her outfit altogether. So under the Boots, if we click this color, we’ll highlight it. And we’ll do, Whoops, I’m actually on it, it didn’t wanna close on, I don’t know why it wasn’t picking that up. 584632, so we’re gonna go for a bit of a brown color on her boots. So the reason it hasn’t changed is because this new material that we just duplicated isn’t actually on the model. They’re still sharing the original one. So if I drag this up, you actually see it happens on the grass.

But if I drag it straight to the boots, it’ll change the color of the boots. And then if we look at the model, it’ll change. Where are you, boots? It’ll change the one that’s there. So that one has a kind of our brown one and then this player, as you can see, has the original one. So we’re able to kind of separate out the coloring. And you can see this as lending itself really well to, if you’re creating the game and you want the player to be able to pick any hair color or pick a series of hair colors, or kind of really whatever. What you can do is tap into the material color and use that to just change it. So you don’t necessarily even have to make different materials, if the one that you have on the actual model is the one that you’re okay with changing. So I’m just going to go through and I’m going to change a couple of these, 828. And then we’ll go and we’ll drag them on to that. So the base of the dress, it’s going to be, Give me a darker brown. And I want this trim to be a green, so you can of course color pick.

But at least I looked up my colors ahead of time, and wrote them down. And then I think I forgot to. I feel like that’s everything and everything. Hem, hair, hats, the hat. So actually I want to give her a hat, so I’m going to color this. And I’d actually like the hat to match the dress’s face. We’ll do that. So, there’s our merchant. Now this merchant is gonna be swanky. That’s the word I’m looking for. So we’re going to give him a hat. And this hat is, we’re gonna child it. So when I talked about the difference of like the hierarchies and it being apparent. What you can do is you can child the objects underneath them, again, so they move with them. Now we want this hat to move with the merchant no matter where we put it. So if I just drag that game object down underneath it, it’ll put the hat as a child under it. So if I were to move, they’re gonna move together. Which is exactly what we want except for we also want the hat on their body. So we move it to 0,0.

That’s gonna position it at the feet. We’ll drag it up and pushing it ever so lightly on the head. Like that, a little off, it’s okay, oop. I think that should be good. [INAUDIBLE] I can see a little bit of her hair so let me just move it a bit up. Okay, so that looks relatively good. So a few shortcut keys that are quite handy. So when you’re moving around, right click, to move forward and back you just use the arrow keys. Position this way to move a little bit faster, hold down Shift while you use the arrow keys. Now that’s going to move you around the environment. To zoom in on an object, push F and that’ll hone in on whatever game object that you have selected. Okay, so the last thing is, of course, we need to change the hat’s material to be our brown. There we go, and I don’t like the hat rotating that particular direction. So let’s see, I think I can do, oops, jeez. Why it’s not [INAUDIBLE] always do that. Actually, well, probably maybe should be in the back.

Okay, so that’s our merchant. Now, I mentioned that we were gonna talk a little but about what a prefab is. So I’ve gone ahead and I’ve created the folder already and I’ve set these under it. But we haven’t made any actual prefabs. Or really we haven’t turned the merchant, I guess I should say, into the prefab. What a prefab is, is it’s a way to kinda save. Lack of a better way to put it, a configuration of the game object. So, for example, we have this witch, or I’m sorry, this merchant. Let’s say we wanna use this merchant in several scenes. This exact same one, that’s colored the exact same way. And the easiest way to do that would be to create basically a saved kinda version, or configuration, of how we’ve set this game object up. Now, to do that, what you can do is you can actually just select your game object and then drag it into really your prefab’s folder. You want to try and keep that relatively organized and then let it go. And that’ll actually create a prefabbed instance of it.

And what that does is that also creates a link between this object that I have in my scene and my prefab object. So if I wanted to say make a change that would apply to all of the game objects, I can really just do it on this prefab level, and that’ll apply down. So, again, we’ll take a look at the scale because that’s kinda the easiest thing. If I change the scale here, again, not on what’s in actually my hierarchy, but on this prefab, you can see that it actually affected my game object. So let’s actually pull out a couple more of these, so we’ve got two here, and join this back to one. So the prefab itself is set to kind of default, and if we click on the merchant. So, again, we create, we click on the instance of the prefab out here, and we altered that. Only that instance was adjusted, not our other version of it. Now what you can do, so if we’re working with this prefab over here, and we decide to make a bunch of changes. And I’m like, I love that change, and I want that to apply to everyone.

You can still do it from this view, and you can do it by clicking this Apply button right here. You can also revert your changes back by clicking this button right here. So if we wanted to apply it, I’d hit that and you can see that it basically applied all those settings to the prefab. Which then pushed it down to our other version or other instance of that game object. So bringing it back a bit we’ll, again, move that to one. We’re gonna get rid of our second merchant, don’t care about her. That’s really all we need to do for the scene setup. You can see kinda see that’s how we dragged the objects out. It’s pretty self explanatory, the only thing I wanted to talk about was really how I created the ground that you could sit at. So it’s actually quite easy to do. Unity has a series of game object types that you can initialize, or add into it on your own. So, if you right click in the hierarchy you have a bunch of options. One is Create Empty so that’s kind of what I was doing a little bit earlier when I was just creating blank theme objects.

They also have pre setup ones. So we can create a Cube, a Sphere, Capsule, all sorts of stuff. Some of which we’ll end up talking about really soon. There’s also 2D objects, so you can create a sprite, we’ll look at that, I think, in the next video. And the other thing is it has a series of UI. And at some point probably two or three videos from now, we’ll be taking a look at setting up some UI stuff. So we’ll dive pretty deep into this as we set up the screens for purchasing things. So, again, kinda stepping back and looking at how this grass was made. Really all I did was I created a 3D object, a plane. So you can kinda see it’s actually up high. It’s really not very big but there it is and it’s at a strange location. So if we do 0, 0, it’s gonna, again, put it in the well cuz that’s where our 0, 0 is. So we’ll just move it up since we’re gonna take a look at it. You notice that it actually provided a default material in here. And it’s one that we really can’t mess with.

In order to change how this looks or the coloring of it, we need to create our own material. And that’s what I did already, so that’s how I have this grass one. I just had created a materials folder. I right-clicked, and I did Create. And then somewhere in here, there it is, I clicked Material. And that’ll just create a blank one, name it whatever you want, Grass02. And then, again, it’s the same stuff that we kinda looked at where you can set the coloring. So we’ll have some red grass and you can drag it into the object. And if we look at our plane, we can see that it’s no longer the default and now it’s Grass02. So pretty simple, and really what I did, so you can see mine’s quite a bit larger. It was just that I changed the scale to be 40, 40 in this one. Our new plane object is sitting at 1, 1, 1. So really, just to adjust it, it’s really the same thing. I’m gonna delete this out, all right, so that’s a quick and dirty overview of setting up a scene. The last thing that I wanted to take a look at this video is just kind of the basics of getting started with creating a script and doing that.

Actually really what we’re gonna do, what we’re gonna create is not anything we’re gonna keep. I’m gonna create an empty game object that we’re gonna delete out. And we’re just gonna call this TestObject. And really, it doesn’t matter where it’s positioning this. I’m gonna set it to 0, 0. So this game object is not gonna visually represent anything in the game. But it is going to act as basically our test object, or test script holder. Really, we actually would call this test manager if we wanted. So I find creating an empty game object with scripts tied to it to be particularly useful really when you’re creating a game manager. So for us, when we do multiplayer games we tend to have a game manager that gets added in that handles the overall scene logic. Well, not just for multiplayer games, but really any game we end up going down that route in some manner. Okay, so to create a script, you have several different options. So you can do Add Component > Add New Script.

And then pick your language and type in your name, and it’ll add the script in. I believe it will just add it into your base folder. I prefer a little bit more organization. So you can also right-click on wherever it is that you want it. Go to Create and then do C# scripts. And then lastly, if you’re using Visual Studio you can create the script straight out of Visual Studio. For now, I’m just going to create it here, so we’ll just call this test. To open up Visual Studio and begin scripting. Again, you have a couple of options. We can just double click this script and then that’ll actually cause your IDE, either Visual Studio or Unity, it also comes with MonoDevelop, to automatically open. Alternately you can click Assets at the top and then you can go to Open C# Project. It doesn’t really matter which, we’ll just double click. It should take a couple of seconds for Unity to generate the Visual Studio projects and there we have it. So this is our script, and then it puts in a couple things to start.

I believe this is actually something that you can go and edit in the template. I read about it quite some time ago, but I never got around to actually doing it. Which is quite funny because it would save me enormous amounts of time if I did. But you can see, when you create through Unity, or if you were to do it in Visual Studio using the Unity Project Explorer. It’ll basically set up your script so that you’re automatically inheriting from something called MonoBehavior. And then it sets up the usings to be more appropriate with what you would typically do for your average script. So MonoBehavior is actually required when you wanna put a script on a game object. And this is actually going to open up the ability, I guess for lack of a better way to put it, to work with various event functions such as Start and Update. We’ll actually take a look at a couple more called Awake, OnEnable and then OnDisable. There’s many more that you can actually work with. In fact, you’re in Visual Studio, you can right-click or Ctrl+Shift+M.

To implement MonoBehavior. And what this does is it actually provides a list of all of the MonoBehavior that you can work with. When you’re getting started with Unity, this is incredibly helpful. I still, every once in a while, encounter something that I either forgot wasn’t there or I just never really worked with when working with the mono events. And even when you start to get into the networking, that actually opens up a whole other set of stuff that you end up working with. So this is actually very, very handy. What you can do, so you can actually go through and check which ones you wanna work with. So like I said, we’re gonna work with Awake(), we’re gonna work with OnEnable(). And then we’re gonna work with OnDisable. We need these three to get added in. I assumed that would remember what I had clicked, but it didn’t. That’s no problem, the other way that you can do it is you can do Quick MonoBehaviors, and this’ll add it in. So you do OnEnable, and you can do cases too.

So it has the notion of camel casing for even quickerness or quicker typing, I guess. Quickerness, I don’t think that’s a word, but it is now. We’ll type in our other one that we want and then kind of a stickler for stuff to be organized [LAUGH] a certain way. I don’t know why that’s not, I guess I accidentally copied a blank lane there, there we go. Okay, so I’m gonna organize these, personally, when I’m working with scripts, I like my Awakes and my Starts to be at the top along with really Update. And then after that, I attempt to get a little bit more chaotic, before this, I actually wanna organize these in basically the order of execution that we’re gonna see. And I’ll demonstrate this, but we’re gonna see at what point Awake, OnEnable, Start, Update, and OnDisable actually run when you’re working with code. In order to do this, the easiest thing is to have it write something out, On the console. So you remember looking at Unity, it said that when you attach stuff and you’re working with it, your errors that you would maybe normally see in your IDE will actually show up here.

So you can play your game and see as errors are happening, which is very useful. You also can do debugging and all sorts of stuff as well. So if we look at the script, we have Awake. Awake really is the only one that has anything right now, and that’s okay for the time being. And then we’ve kind of got our stubs, if you will, for the rest. Now, if we pop over and we hit play, we’re gonna see nothing is going to happen. And that’s because our script is not actually attached to a game object, which means it’s not actually running. We haven’t created an instance of that script. So if we click on our TaskManager, you can either drag the script over, or you can do Add Component and then search for the script and add it that way. By adding it to an active game object in the scene, that’s gonna cause our script to run. So when I push play, now there’s a little caveat to that, and we’ll look at that in a second. So if I push play, we can see that the Awake log that we had written in is here, and that’s great, that’s exactly what we would expect to see.

Now, if we take a look, I’m just gonna copy, then we do OnEnable. I’m gonna add OnEnable and Start. And again, we’ll kind of look at these, and when they true here momentarily. So right now, we have code for Awake, OnEnable, and Start. Everything else is not executing anything that we can see. So if I push play, we’re gonna see all three of these go. So we saw OnAwake happen, OnEnable, and Start. These are initialization methods. And what that means is each of them actually trigger at different points in time when the script is running. So, Awake is called, as you can see, before Start and before Enabled, and it always runs when a game object is instantiated, regardless on if it’s actually disabled. So what I mean by that is if I uncheck this check box next to the word Test, I’m actually disabling the script by default. So that means when the game goes to run and that game object gets put in it, I’ve said script, you’re not enabled, don’t do anything. What actually happens is Awake will run, but we see that OnEnable and Start do not run.

So that kinda comes back to Awake will basically always run when your game object is initialized. Actually, with it still running, we can see this. So the nice thing is when you’re working with Unity and you’re playing your game, you can actually manipulate in the Inspector various properties and things, and turn your scripts on. And technically, we could change the sizing of our person if we wanted to, to kinda see how things behave in the world. And then when you stop playing the game, it’ll actually revert almost all of the settings back. I think it’s materials will not actually revert back, that’ll save it, if I recall correctly. Okay so looking at Test, if I click this check box again to enable it, we see that OnEnable ran and Start ran. So, again, it has to do with when you check it. Now if I uncheck it and check it again, we can see OnEnable ran again. The reason is, we’re triggering that OnEnable event every time the script gets enabled, but we see that Start only ran.

So Start only ran the one time. Now Start is similar to Awake in that it only runs once, but it only runs when it’s actually been enabled. So you can hone in your stuff on Awake for things that you want to run kinda regardless on if the script itself is actually enabled. And then Start would be your logic that you needed to run at the beginning only when your script is enabled. I tend to populate the OnEnabled logic to be stuff that I want to run every single time I’ve toggled my script. So the last one that I wanna look at before we dive into Update is OnDisabled. So this is basically the counterpart to OnEnabled. It’s going to run when we uncheck this box to disable it. So I’m gonna unplay this, I’m gonna OnDisable, changes that we have this coming in. Now I’ll play it again. So if we take a look, again, we have Awake. We’ll go ahead and check this. We see OnEnable and Start. If I uncheck this, we’re gonna see OnDisable trigger again, and then we’ll be able to toggle back and forth between them.

So, again, this is a good place to put game logic that you want to have happen before the script has been completely disabled. So, the last kind of basic area that we’re gonna look at is Update. Now before I jump into it, it is worth noting that this is not the only place for you to put your game logic. There are two other ones called FixUpdate and LateUpdate, not gonna dive too much into each one of those in this video. But it is worth calling out that they do exist, and they do have purposes within the game logic. But as you’re getting going in first using Unity, it’s okay to kinda hone in on just the Update method for your game logic. Now what this method is, is it’s something that’s actually to trigger it in a sort of loop, and it’s going to run once per frame. And that does mean that it is solely frame rate dependent. So if you happen to be pulling very few frames and you have game logic sitting in Update, you’re going to see that happen versus maybe only in a game that’s got 60 frames per second.

So when you’re playing around with physics, a really good spot for you to do that type of stuff is actually in FixedUpdate. Which again I’m gonna stay away from, but just keep that in mind, and if you have a need to go and work with it, go take a look at that. And down underneath this video in the descriptions, I’ll have some reference areas for you to go and browse and then, of course, on the GitHub page where you can go and download all these project files, there’ll also be a couple references for you to go. And take a look at what each of those methods are, and then there’s also an order of execution we’re gonna dive into it. So coming back to the Update method, we can do to kind of, again, illustrate this, and we’re gonna put a debug log, and we’ll be able to see this happen a lot more frequently than the others. So, again, our script is disabled. We’re gonna enable it and we can see, if we push pause, we can actually see that Update has run quite a bit of times. So this is great really, again, for any game logic that you want to happen per frame.

So let’s say you wanna monitor to see if somebody’s pushing a button to move the character around. Update is a great method to do something like that in, because it’s going to check pretty frequently. Now on the flip side, you wanna be very careful about your game logic, so that you’re not doing a constant compute that maybe doesn’t need to happen there, but needs to happen a little bit more infrequently. So putting stuff in Update can be very useful, but it also can be very dangerous on the performance of your game, so you wanna try and pick where you go carefully, I guess, in that sense. So, that about wraps up we’re gonna be covering for this Unity beginner video. In the next video, we’re gonna dive more deeply into the navigation aspect, so we’re actually going to build out kind of the point and click style where you have a character selected. You click somewhere on the environment, it’s gonna navigate to it. So we’ll build more off of this scene and dive a little bit more into coding and then explore the navigation and path finding features that Unity has to offer.

So thanks and see you next time.

 

Read the video

In this episode we will take a look at the basics of using Unity while setting up the scene that we will be working with for the next several episodes. We will discuss the basics of the UI, general terms such as "game object" and  "component" and give a brief introduction into materials. We’ll wrap up by taking a quick look at the Awake(), OnEnable(), OnDisable(), Start() and Update() methods.

Resources

1. GitHub Repository
2. Unity Manual – Execution Order of Event Functions


dotGame

dotGame is a blog dedicated to game development with a primary focus on .NET technology. The episodes will focus on tutorials, tips and tricks and interviews. All tutorial project files can be found on the .GAME GitHub repository. Have a topic you'd like to see covered? Great! You can request a topic by opening an issue on the GitHub repository or by emailing dotgame@microsoft.com. General feedback can also be directed to dotgame@microsoft.com.

    Comments to Basics of Unity

    • Really enjoyed watching it, looking forward to the upcoming episodes.Since this channel is related to game development in .NET, I would like to also leave the message, already asked by quite a few on User Voice, that it would be nice to be able to have a UWP projection of DirectX, instead of having to go down to C++ and make use of COM to use it.

      Moondevil January 6, 2017 1:06 pm Reply
    • Thanks Stacey, really enjoyed this video. Nice pace and explained well.Would be nice to have some intro/outro music. I’m sure the Ch9 studio can help with that ;)I’m still not clear on the use of Prefabs. To me, it just looks like Materials and Models placed under a different folder. The transform for the model is shared but not much else. The materials don’t seem to be any different as they are shared either way.Are you going to go over how to import models/materials/textures? For that matter, how to create a new project from scratch? You do have some other Unity videos so I’ll take a look and see if this is covered there.It would also be nice if you gave us some projects to work on to enhance what you just showed, then go over how to do it in the next video.Great work and thanks again.

      geeyef January 6, 2017 10:24 pm Reply
    • // thank you for taking the time to make an entire series and sharing the knowledge

      stahtufiang January 8, 2017 5:15 am Reply
    • @Moondevil: Thanks! I’ve passed the information along to the team. I recommend voting for the item on User Voice if you have not already. @geeyef: I find that the best way to think of a Prefab is as a template. You can use the template to create instances of the game object within the game (known as instantiating). I’ll be showing a more practical usage of this in one of the upcoming videos. I am also thinking I might do a short tips/trick video that goes over it a bit more in depth. I hadn’t planned on doing a video on how to import models/materials/textures or creating a new project, but I will add that to my list. Feel free to venture over to the GitHub repo and open an issue for any other topics that you’d like to see! I also love the idea of giving challenges to expand on topics covered. I did that for the Super Space Shooter series, which I’ve heard from a few folks was quite helpful. I’ll look at how I can incorporate that in some of the upcoming episodes.@stahtufiang: Thank you for the kind words! 🙂  

      StaceyHaffner January 9, 2017 9:53 am Reply
    • I am a C# developer and have never used Unity (or monogame etc) before and find this is just the right level to peak my interest in Unity. Unity now downloaded. I look forward to future episodes. I did wonder while watching this episode how to create new 3d models yourself (not from the asset store).. I presume you will cover this in future episodes?.

      leee January 17, 2017 1:03 am Reply
    • I would love to port my Unity game to Windows. Currently, it shows ads from AdMob. Is there a way to show ads in a Unity game on Windows?

      JDL January 20, 2017 12:13 pm Reply
    • I’m new to Unity and this episode was extremely helpful in covering the basics. I will definitely watch the rest of the series. Thank you so much for doing this.

      YoniS January 27, 2017 8:21 pm Reply
    • Hey, I really found this helpful. But how did you made those models!

      Anas10 February 2, 2017 5:40 am Reply

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Loading...
    1Code.Blog - Your #1 Code Blog