Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Creating an RTS Game in Unity 2023

You're reading from   Creating an RTS Game in Unity 2023 A comprehensive guide to creating your own strategy game from scratch using C#

Arrow left icon
Product type Paperback
Published in Oct 2023
Publisher Packt
ISBN-13 9781804613245
Length 548 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Cicanci Bruno Cicanci
Author Profile Icon Bruno Cicanci
Bruno Cicanci
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Part 1: Foundations of RTS Games
2. Chapter 1: Introducing Real-Time Strategy Games FREE CHAPTER 3. Chapter 2: Setting Up Unity and the Dragoncraft Project 4. Chapter 3: Getting Started with Our Level Design 5. Chapter 4: Creating the User Interface and HUD 6. Part 2: The Combat Units
7. Chapter 5: Spawning an Army of Units 8. Chapter 6: Commanding an Army of Units 9. Chapter 7: Attacking and Defending Units 10. Chapter 8: Implementing the Pathfinder 11. Part 3: The Battlefield
12. Chapter 9: Adding Enemies 13. Chapter 10: Creating an AI to Attack the Player 14. Chapter 11: Adding Enemies to the Map 15. Part 4: The Gameplay
16. Chapter 12: Balancing the Game’s Difficulty 17. Chapter 13: Producing and Gathering Resources 18. Chapter 14: Crafting Buildings and Defense Towers 19. Chapter 15: Tracking Progression and Objectives 20. Chapter 16: Exporting and Expanding Your Game 21. Index 22. Other Books You May Enjoy

Exporting Dragoncraft for desktop

We have finished developing our Dragoncraft game with lots of cool features, and now we are ready to share it so other people can play the game you developed. The Unity Engine has a lot of platform options for exporting the game; however, some of them, such as gaming consoles, require licenses and extra setup. We are going to focus on the desktop platform, which is the most common and easy to use when sharing your game.

In this section, we are going to prepare our code and our project settings to export the game for desktop, and then how to manually build and run the game from the Unity Editor using the Build Settings window.

Preparing the Editor scripts

When we export our game from the Unity Engine to run on any platform, some pieces of code might not be available to run on the given platform, and that will cause an error when we try to build the game.

In our project, we used some APIs from Unity that are available only when we are running the game in the Editor, such as UnityEditor, or using the custom menu options we created there, such as the Dragoncraft menu. To build and run the game on desktop platforms such as Windows or macOS, we need to make sure the APIs that exist only on the Editor are not included when exporting the game to desktop platforms.

Before configuring the project to export a build for desktop platforms, we need to adjust a few scripts to make sure their code will run only on the Editor. We are going to add a conditional compilation directive that will allow us to selectively include a piece of code in the compilation, using scripting symbols.

In the following code block, #if and #endif are conditional compilation directives and UNITY_EDITOR is a scripting symbol defined in the Unity Engine:

#if UNITY_EDITOR
…
#endif

Any piece of code that is between the conditional compilation directives #if and #endif will only be included in the build if the scripting symbol is defined by our code or the Unity Engine in this case.

When we are using the Unity Editor, or running the game there, the UNITY_EDITOR scripting symbol is defined by the Unity Engine so any code between the conditional compilation directives will be executed. However, when we are building and running the game on the desktop platform, for example, the piece of code between the conditional compilation directives with the UNITY_EDITOR scripting symbol is not included or executed.

Now, let’s add the #if and #endif conditional compilation directives with the UNITY_EDITOR scripting symbol in a few scripts that should only be available in the Editor:

  1. Open the LevelDataEditor script, located inside the Scripts | Editor folder.
  2. Add the #if UNITY_EDITOR line as the first line of the script, at the very beginning of the file.
  3. Add the #endif line as the last line of the script at the very end of the file.
  4. Open the EnemyDebugger script, located inside the Scripts | Editor | Debug folder, and repeat steps 2 and 3.
  5. Open the ResourceDebugger script, located inside the Scripts | Editor | Debug folder, and repeat steps 2 and 3.
  6. Open the UnitDebugger script, located inside the Scripts | Editor | Debug folder, and repeat steps 2 and 3.

All scripts that use features from the Editor are now prepared to only work in the Unity Editor and, the most important thing, not cause errors when we export a build to any other platform. Now, we are ready to export our game using the Build Settings window.

Exporting the desktop build manually

The Unity Engine has a build system that can be accessed using the Build Settings window, where we can select what scenes to include in the build, what platform we want to export the build to, and many other configurations that are specific to each platform.

Let’s export our first build for the desktop platform:

  1. Open the Build Settings window using File | Build Settings….
  2. Drag the Level01 scene from the Inspector view and drop it in the Build Settings window under Scenes In Build.
  3. Drag the GameUI scene from the Inspector view and drop it in the Build Settings window under Scenes In Build and below the Level01 scene.
  4. In the left-hand side panel named Platform, click Windows, Mac, Linux.
  5. Then, on the right-hand side, select Windows in the Target Platform property (if you are using macOS, select the macOS option instead).
  6. Click on the checkbox on the right side of the Development Build property.
  7. Click on the Build button at the bottom of the Build Settings window and select a folder where Unity will export the build. A good practice is creating a folder named Builds in the root of the project directory (not inside the Assets folder).
  8. Once Unity finishes building, a new window from your OS will be opened with all the files and executables exported for the game to run. You can double-click on the executable file and run the game to test it on your computer.

We included two scenes in the build, Level01 and GameUI. As soon as the player runs the game, the first scene in the list will be loaded as the first level of the game. It is important that, in our project, the first scene is Level01 and the second is GameUI, which will be loaded additively by the LevelManager script. The following screenshot shows the two scenes in the Build Settings window, as well as Windows as the Target Platform and the Development Build option enabled:

Figure 16.1 – The Build Settings window

Figure 16.1 – The Build Settings window

We enabled the Development Build option in the Build Settings window to tell Unity to include scripting debug symbols in our build – these are the recommended settings for testing our game because if an error happens, it will be easy to identify the root cause. In this case, the type of build is Debug, and should be used only for tests or analysis. The other type of build is Release, where the game does not have any test or debug code and is intended for the final users – the players.

The Development Build option also adds the Profiler to the build, which lets us connect the build to the Unity Editor and debug in real time. The Profiler is an amazing and essential tool for debugging the game, as well as for analyzing and identifying performance issues and optimizing the game. We are not going to see it in this book, but there are a few links in the Further reading section of this chapter with more information on how to use it.

Now that we have learned how to manually export a build from Unity, let’s create a script to automate the export process and add custom options, such as different scenes, depending on the build configuration.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Visually different images