Android version and cross-platform development

Note: This is an automatic translation from Russian. It looks good enough, but there may be inaccuracies. We are sorry for the inconvenience.

File paths

In an Android project, all game resources, such as pictures and sounds, must be placed in the Assets folder. After that, in the code, they will be available via relative paths.

For example, let's create a folder called sounds in the Assets directory and put the file forest.mp3 there. Now you can play this sound in the game code:


Note that Android is a Unix-like system, and the file paths use a slash instead of a backslash. In a Windows project, the same line of code would look like this:


To make your code run on both platforms without having to make changes for each of them, we recommend using the Path class from the System.IO namespace to generate file paths. So the same line of code would look like this:

game.Audio.PlaySound(Path.Combine("sounds", "forest.mp3"));

This code will now work on both Windows and Android without having to make any changes to it.

Image paths

In previous article We told you that due to the peculiarities of the component used in Windows version , the absolute path to the image files must be specified in the HTML code. We used Environment.CurrentDirectory for this. However, the same component in the Android version accepts relative paths without any problems.

Create an images folder in the Assets directory and place the forest.jpg file there. Now we can add this image to the location:

Content = "<img src='images/forest.jpg'>";

However, this code will not work on a Windows project. We'll have to rewrite the paths to images from project to project, on Windows using absolute paths, and on Android - relative ones. Since there can be hundreds of images in the game, such a situation is meaningless.

To solve this problem, we suggest the following approach. In the main game class, create a static property like this:

public static string PathPrefix { get => Environment.CurrentDirectory; }

Now, in a Windows project, the path to the image will look like this:

Content = $"<img src='{Path.Combine(MyGame.PathPrefix, "images", "forest.jpg")}'>";

Note that we are using the Path.Combine method from the previous section.

Now, to make the paths to all images work in the Android project, in this project just change the PathPrefix property so that it returns an empty string.

public static string PathPrefix { get => ""; }

Thus, in order for the paths to images to work correctly in Windows and Android projects, you will need to change just one line of code.

Methods IO.YesNoQuestion and IO.Prompt

Let's say that at the beginning of the game we want to know the player's name. Then the method that handles the corresponding action on the location might look like this:

        private void GetName(Game game)
            string name = game.IO.Prompt("Enter your name:");

When this method is triggered, the player will be shown a dialog box in which he can use the text input field. However, due to the fact that in the Android operating system, dialog boxes do not block the main program flow, this method must be made asynchronous and explicitly wait for the IO.Prompt method to execute. Thus, in an Android project, the same method will look like this:

        private async void GetName(Game game)
            string name = await game.IO.Prompt("Enter your name:");

Unfortunately, such changes will need to be made for each such method.