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:
- Open the
LevelDataEditor
script, located inside the Scripts | Editor folder. - Add the
#if UNITY_EDITOR
line as the first line of the script, at the very beginning of the file. - Add the
#endif
line as the last line of the script at the very end of the file. - Open the
EnemyDebugger
script, located inside the Scripts | Editor | Debug folder, and repeat steps 2 and 3. - Open the
ResourceDebugger
script, located inside the Scripts | Editor | Debug folder, and repeat steps 2 and 3. - 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:
- Open the Build Settings window using File | Build Settings….
- Drag the Level01 scene from the Inspector view and drop it in the Build Settings window under Scenes In Build.
- 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.
- In the left-hand side panel named Platform, click Windows, Mac, Linux.
- Then, on the right-hand side, select Windows in the Target Platform property (if you are using macOS, select the macOS option instead).
- Click on the checkbox on the right side of the Development Build property.
- 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 theAssets
folder). - 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
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.