Closed HellGate94 closed 7 years ago
All of the Ultraviolet libraries are available on NuGet. You can use the Ultraviolet Game (Platform)
packages to create skeleton projects for each of the supported platforms.
In order to share code between the different platforms, I use Shared Projects; just create a Shared Project, move all of your non-platform-specific code into it, and then add a reference to it in all of your platform-specific projects.
One small complication is that if you're supporting Android, you may need to slightly modify the declaration of your UltravioletApplication
class using preprocessor macros, like I do here.
Let me know if you run into difficulty getting anything to work.
the Ultraviolet Game (Platform) did work quite well for one platform at a time but not so much with multiple. i could get it to work with the desktop project, however the android project cant seem to resolve the ultraviolet packages. most likely related to the Game.cs containing the game code now but the android one wanting it inside MainActivity.cs and i cant seem to find the project setting to change that.
would be really nice to update the documentation with a step by step way to set up a project with shared game code (and content) for all the platforms. i see how it should be in the end from the samples but i would like to know how to get there. especially since i never done any cross platform programming with c# before.
Improved documentation is on the list for the 2.0 (previously 1.4) release. It's a matter of finding the time to write (and maintain) it, unfortunately.
Here's a walkthrough of how I would do it...
Start from a completely empty solution. For demonstration purposes, I'll call mine CrossPlatformGame
.
Add a new project to the solution using the "Console Application" project template. I'll call this one CrossPlatformGame.Desktop
. Usually I'd go into the project properties and change the application's output type from "Console Application" to "Windows Application" in order to get rid of the console window, but this isn't strictly necessary.
Add another new project to the solution using the "Blank App (Android)" project template. I'll call this one CrossPlatformGame.Android
.
If you can't find the "Blank App (Android)" project template, that probably means you don't have Xamarin installed. Xamarin is required to run Ultraviolet on Android and iOS. You can install it by running the Visual Studio Installer and adding the "Mobile development with .NET" module to your Visual Studio 2017 installation.
Your project must be a Xamarin.Android project, or NuGet won't be able to add the Android versions of the Ultraviolet libraries. I'm guessing this is what you were running into.
Add a third project to the solution using the "Shared Project" project template. I'll call this one CrossPlatformGame.Shared
.
Manually add a reference to the CrossPlatformGame.Shared
project to both of your platform-specific projects.
All code which is not specific to a particular platform needs to go inside of the Shared Project. When you add a reference to a Shared Project, it sort of "mirrors" all of the code files inside of it. It's like you had copies of the shared files inside of all of the individual platform projects.
Using NuGet, add the "Ultraviolet Game (Desktop)" package to your desktop project, and the "Ultraviolet Game (Android)" package to your Android project. This will add all of the necessary dependencies as references.
Copy Game.cs
from your desktop project into the Shared Project. Delete the original Game.cs
as well as the MainActivity.cs
file in the Android project; you won't be needing them. In my case the Game
class was declared inside of the CrossPlatformGame.Desktop
namespace, so I changed it to CrossPlatformGame
, since we'll be using it from both projects.
The above steps will give you your basic project structure, but if you try to build this now, the Android project will complain that it has no idea what UltravioletApplication
is supposed to be. Due to how Android works, your main game class needs to inherit from UltravioletActivity
instead, and needs a couple of extra annotations. So let's modify Game.cs
a bit so that it will build as an UltravioletApplication
on the desktop and an UltravioletActivity
on Android.
This line:
public class Game : UltravioletApplication
Needs to be changed to this:
#if __ANDROID__
[Android.App.Activity(Label = "CrossPlatformGame", MainLauncher = true, ConfigurationChanges =
Android.Content.PM.ConfigChanges.Orientation |
Android.Content.PM.ConfigChanges.ScreenSize |
Android.Content.PM.ConfigChanges.KeyboardHidden)]
public class Game : UltravioletActivity
#else
public class Game : UltravioletApplication
#endif
The __ANDROID__
symbol is defined automatically by Xamarin for your Android project.
At this point, everything should build without errors, and you ought to be able to deploy to an Android device using the Xamarin tools. For more information on that, I'd suggest looking through Xamarin's documentation, since that isn't unique to Ultraviolet.
You might also want to read up on how Shared Projects work generally.
thats what i did and thanks to your great write up i figured out the problem. in the example it was using #if ANDROID
instead of #if __ANDROID__
with that change im able to get it running
also its great to hear version 2.0 (1.4) is on its way since thats one of the key milestones im waiting for and im currently trying to get more familiar with the framework
Yeah, that was my mistake, sorry! Ultraviolet's internal projects define an ANDROID symbol in their project files in addition to the one provided by Xamarin, which I forgot about.
Glad you got it working.
since the new versions no longer come with an installer that adds templates and the old one does not support vs2017, whats the right way to set up a cross platform project?