Switching between Monogame and UIKit

2019-04-28 22:08发布

问题:

I've been searching and searching but can't seem to find a solution that works for what I'm trying to do, and I'm almost at the point where I have to ask if it is even possible.

I'm using Xamarin Studio to develop an iOS app. I have a few different screens set up as UIViewControllers and they are working well. The crux of the app, however, is a game and I want to use Monogame as I've used it before and enjoyed working with it.

Basically, I have to switch from a UIViewController to the Game class. I am able to do this by simply creating a new Game object and calling Run(), but I can not figure out how to get out of the Game when I need to and return control back to the UIViewController.

Has anybody done this in an app? I've tried everything I can possibly think of, but nothing seems to do the trick. Is it even possible? Or will I need to redo it so that Monogame handles everything, even all of the other stuff in the app that isn't part of the actual game?

Thanks!

回答1:

There are a couple hacky ways I feel this will work without modifying MonoGame.

1st option:

  1. When your app starts, start your Game class as you would a normal MonoGame game
  2. To display a controller present it modally over top of the game
  3. To return to the game, dismiss your modal controller

So for example, to show a controller:

var gameController = game.Services.GetService(typeof(UIViewController)) as UIViewController;
gameController.PresentViewController(new YourController(), true, null);

Then you can just call DismissViewController to hide it. I've used this for implementing Game Center stuff.

Sadly, you will have to modify how MonoGame works if you want to show your MonoGame game after a UIKit controller.

Another option is to basically add a UIView on top of your MonoGame's view. Something like this:

var gameController = game.Services.GetService(typeof(UIViewController)) as UIViewController;
var view = new YourView();
gameController.View.AddSubview(view);

You'll need to add some logic to pause the game if you take this route. I've used this option before for iAd, etc. You could also do this with other controllers' views, like your tab controller example above.