First up I am a bit of a noob when it comes to project templates etc.
With Xamarin now being free I thought I would make a small game for android and iOS. I want to avoid having to separate solutions for each platform. I would like to avoid having to code for one platform the copy paste for the other.
From what I understand you are supposed to have one solution with multiple projects. One project for the main chunk of the code, the code that stays the same for all platforms. And then separate projects for each platform which then reference the main project.
The problem I have is this. I have tried referencing a project created with one of the monogame templates (I tried several) but it does not work. Lets say Project A is the main project and I want Project B to reference it. I right click Project B -> Add -> Reference -> Project A. So far so good, no errors or anything. But I can not actually using any of the code from Project A in Project B. The other way around works fine.
For Project A:
I have tried using Monogame Windows OpenGL template, Monogame Cross Platform Desktop template.
Maybe you’re forgetting to actually put code/references in that are needed to make monogame work on Android
Put all of the references from “C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Android” into your Android project and in the Main Activity change it form inheriting whatever it currently does to AndroidGameActivity and remove all the code in the OnCreate void other than base.OnCreate(bundle); and add the following lines bellow:
var g = new Game1(); SetContentView((View)g.Services.GetService(typeof(View))); g.Run();
so it should like like in whole:
`using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Microsoft.Xna.Framework;
using PongClone;
namespace App1 {
[Activity(Label = “App1”, MainLauncher = true, Icon = “@drawable/icon”)]
public class MainActivity : AndroidGameActivity {
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
var g = new Game1();
SetContentView((View)g.Services.GetService(typeof(View)));
g.Run();
}
}
If you could upload an example that would be greatly appreciated, but don’t stress. It’s not like this project will be the next super hit on mobile or anything I can just create the android app first and port to iOS later if I feel like it.
But I am kinda interested in finding out what I’m doing wrong or if something is wrong with my install. So if you could upload an example that would be great, but as I said don’t stress
Yes you are correct. You will need a separate project for each platform, reference the correct version of MonoGame and add your game code to each project. The best way to do it is put your code in a another location and add the files as links.
What you are asking for is MonoGame bundled in a PCL, which yeah would be awesome. In this scenario, there would be a yourGame.PCL project with all game logic and reference to MonoGame.PCL, and each platform just references that project. There’s still a lot of work to get there.