首页 > 解决方案 > Android MonoGame 游戏无法在手机上启动

问题描述

我正在学习适用于 Android 的 Xamarin 和 MonoGame。我有一个问题。在Android 9游戏模拟器上正常启动。在真手机上,游戏无法启动。我在装有 Android 10 和 Android 11 的手机上进行测试。我设置了所有 Android API 版本,但这无济于事。 Android 11 手机 Android 10 手机

文件“Game1.cs”的代码:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace AndroidTest
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;

        private SpriteFont font;
        private Vector2 position;

        private float x, y;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            position = new Vector2(10, 10);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            font = this.Content.Load<SpriteFont>("File");

            // TODO: use this.Content to load your game content here
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            TouchCollection tc = TouchPanel.GetState();
            foreach (TouchLocation tl in tc)
            {
                position = tl.Position;
                x = tl.Position.X;
                y = tl.Position.Y;
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();
            _spriteBatch.DrawString(font, "This is test!", position, Color.GreenYellow);
            _spriteBatch.DrawString(font, string.Format("X:{0} Y:{1}", x, y), new Vector2(10, 1635), Color.Beige);
            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

文件“AndroidManifest.xml”的代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AndroidTest.AndroidTest" android:versionCode="1" android:versionName="1.1" android:installLocation="preferExternal">
    <uses-sdk android:minSdkVersion="30" android:targetSdkVersion="30" />
    <uses-permission android:name="android.permission.SET_ORIENTATION" />
    <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
    <application android:label="AndroidTest"></application>
</manifest>

文件“Activity1.cs”的代码

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using Microsoft.Xna.Framework;

namespace AndroidTest
{
    [Activity(
        Label = "@string/app_name",
        MainLauncher = true,
        Icon = "@drawable/icon",
        AlwaysRetainTaskState = true,
        LaunchMode = LaunchMode.SingleInstance,
        ScreenOrientation = ScreenOrientation.FullUser,
        ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize
    )]
    public class Activity1 : AndroidGameActivity
    {
        private Game1 _game;
        private View _view;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            _game = new Game1();
            _view = _game.Services.GetService(typeof(View)) as View;

            SetContentView(_view);
            _game.Run();
        }
    }
}

标签: c#androidxamarinmonogame

解决方案


推荐阅读