Join Dan Violet Sagmiller for an in-depth discussion in this video Unit prefab, part of Unity 5: Build a Real-Time Strategy Game.
- [Voiceover] Now we can add our first unit. I've selected a hovering drone. The additional content can be found in the Resources folder of the download. Under Resources, you'll find folders Content 1, 2, and 3. We're gonna go into Content 1. And then I'm going to select the Models and Textures folders that are inside of there, and drag them into the root Assets folder in Unity. So now, we have a Textures and a Models folder. Let's go into the Models folder. Inside of here we'll find our drone unit model. I'm going to drag it into the scene, it doesn't really matter where.
Now, I'm gonna double click on it from the Hierarchy. There's a couple things wrong with it. One, is its rotation is not at a good default, let's set these to zero. The second thing is it has an animation controller which we're not using animations, so I'm going to remove this. Next, I wanna give this drone some color, so I'm going into the Textures folder, and I'm gonna drag this material over on top of it. Now, we have a textured drone.
Now, I need to create a prefab out of this. So, I'm going into the Assets folder and I'm gonna create a new folder called "Prefabs". I'll drag the Drone Unit into the Prefabs folder, and then, I can delete it from my scene. I'm going to zoom back out a little bit. Now, from the manager, we have our Rts Manager, and in it, it has Starting Units. I'm going to drag this Drone Unit prefab into both sets of Starting Units, so both Player 1 and Player 2 will have them.
So now, we can see both Player 1 and 2 have a Drone Unit in their Starting Units. Now, we need the code to support this. I'm going to edit the Rts Manager. In order to get to all the Starting Units, we're gonna have to go through each of the players, so that requires two foreach statements, "foreach ( var p in Players)". Then we need "foreach "(var u in P.StartingUnits)".
Now, I need to create a new instance of each one of these units in the Starting Units. To do that I'll say, "GameObject. Instantiate", and the original is going to be "u", then I need to know where to put this, which will be "p.Location.position". And then, I need to know what its rotation will be, which is going to be "p.Location.rotation".
Save the code, now return to Unity. We'll press play, and as we can see there is a starting unit for this player. And if I'm going to scroll up to the other player's section, we can see that it has a drone unit as well. Right here.
- Adding player positions and start info
- Loading prebuilt levels
- Adding camera moves
- Adding a unit
- Supporting navigation
- Adding unit owners and player objects
- Importing and setting up a HUD map
- Adding unit info and credits
- Creating HUD actions
- Building a command base
- Adding artificial intelligence
- Adding battle actions