Discover how to build out the foundation for the game window. You’ll learn how to create a new window class by extending code from Jesse’s Unity 5: Advance UI course and wire up the new game window to the existing random map test code used to create the project in the previous course, Unity 5 2D RPG Movement.
- [Voiceover] Let's start building out the foundation for our game's window. We're gonna leverage some existing code inside of our random gen folder. Let's scroll down to that folder, open up the scripts folder inside of it, and open up the random map tester. This class is responsible for generating out new random maps and also adding the player to it and handling player movement. Let's scroll down to our start method. In order for this to work with the new game window we're gonna build, we're gonna need to change the way that this class starts.
Let's go ahead and cut the two lines for island tile sprites and fog of war tile sprites out of the start method and paste them into the top of the restart method. Now let's go ahead and delete this start method since we're not gonna use this anymore. When you're done with your changes hit save, and go back into your Unity project. Now inside of our advanced UI scripts folder, look for a windows folder and right click inside and create a new c sharp script called game window.
Let's open this up in MonoDevelop. Now in my advance UI course, I created a window management system that allows you to switch between different windows inside of a scene. We're gonna leverage some of that code by replacing mono behavior and instead having our game window extend the generic window class. We can also delete the start and update methods. In order for this window to work, we're gonna need a reference to the random map tester. Let's create a new private field with a type of the random map tester class and we'll call this tester.
Next we're gonna wanna override some of the base methods that exist in our generic window. We can do this easily by starting to type out override and hitting return in MonoDevelop. When you hit space, it'll give you an option for which methods you want to override. Let's start with awake. As you create a new override for awake, you'll see that MonoDevelop also gives us a base dot awake method call. This way our window will work based on whatever logic is inside of the awake call of our generic window class.
Before we call base dot awake, we're gonna wanna set up our tester class. Let's set the value of tester to equal get component, and here we'll pass in the random map tester class as the component to look for. Now below our awake, let's override our open method. Now below where we call the base dot open, let's tell our tester to restart. This'll be responsible for restarting the map and adding the player whenever we open the game window.
The next thing we wanna do is add a call that disables and enables our move camera script. This is what allows the camera to follow the player on the map, as well as letting you right click and drag the camera around. Whenever we open the game, we're gonna want to enable this script so that it works when the game is visible. We can do this by talking directly to the camera class, getting the main camera, and then calling get component and passing in a reference to the move camera class.
Now that we have a reference to this component, let's set the enabled value to equal true. The final thing we'll wanna do is override the close method. Below where we call base dot close, let's go ahead and tell the tester to shut down. We'll also wanna copy out the line from our open method where we enable the camera move script, paste it below, and set it to false. The last thing we're gonna wanna do is add the shut down method to our tester.
Let's go back into our random map tester. Here we'll create a public method that returns void called shut down. Inside of this method we're simply gonna call clear map container, and this will destroy all the contents inside of the map container. Save your code, and go back into Unity, and make sure it compiles without any problems. Now we have the basic foundation for opening and closing our game window, and enabling and disabling our game's map.
- Preparing the user interface
- Creating game and message windows
- Building a battle window
- Adding a monster and action buttons
- Creating an actor class
- Adding player and monster attack actions
- Triggering battles from the map
- Ending a battle
- Cleaning up the battle and attack systems
- Rewarding the player