我正在尝试开发自己的能够编译游戏的MonoGame编辑器。为了做到这一点,我正在尝试通过终端编译游戏以进行测试并确保有可能。在Windows上,它可以正常工作。但是,在MacOS上,我目前无法编译一个简约示例。这是我所拥有的:
我运行的要编译的命令。所有必需的dll /源文件都存在于同一目录中。编译过程本身不会导致任何错误,因此我怀疑问题是否像放错文件一样简单。不过,我确实认为我需要在此处修复某些问题:
csc /reference:MonoGame.Framework.dll /reference:OpenTK.dll /out:MyGame /optimize main.cs
执行命令(这是发生错误的位置。运行该命令后立即打印。):
mono MyGame
在运行时发生的错误:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
at ObjCRuntime.Class.Register (System.Type type) [0x00001] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at ObjCRuntime.Class.GetHandle (System.Type type) [0x00001] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Foundation.NSObject.AllocIfNeeded () [0x0001e] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x00007] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at AppKit.NSResponder..ctor (Foundation.NSObjectFlag t) [0x00000] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at AppKit.NSWindow..ctor (CoreGraphics.CGRect contentRect, AppKit.NSWindowStyle aStyle, AppKit.NSBackingStore bufferingType, System.Boolean deferCreation) [0x00000] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Microsoft.Xna.Framework.MacGameNSWindow..ctor (CoreGraphics.CGRect rect, AppKit.NSWindowStyle style, AppKit.NSBackingStore backing, System.Boolean defer) [0x00000] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.MacGamePlatform.InitializeMainWindow () [0x0001d] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.MacGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x0003c] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.GamePlatform.PlatformCreate (Microsoft.Xna.Framework.Game game) [0x00000] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.Game..ctor () [0x001fb] in <22537961847f41f881e1a9d3cb0e99a8>:0
at MyGame.TestGame..ctor () [0x00000] in <c3e40b4cec934c9cba34c73e74a28eb5>:0
at MyGame.Program.Main () [0x00000] in <c3e40b4cec934c9cba34c73e74a28eb5>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at ObjCRuntime.Class.Register (System.Type type) [0x00001] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at ObjCRuntime.Class.GetHandle (System.Type type) [0x00001] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Foundation.NSObject.AllocIfNeeded () [0x0001e] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Foundation.NSObject..ctor (Foundation.NSObjectFlag x) [0x00007] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at AppKit.NSResponder..ctor (Foundation.NSObjectFlag t) [0x00000] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at AppKit.NSWindow..ctor (CoreGraphics.CGRect contentRect, AppKit.NSWindowStyle aStyle, AppKit.NSBackingStore bufferingType, System.Boolean deferCreation) [0x00000] in <088ca15f28ed485b8ec04e5baf83b5ec>:0
at Microsoft.Xna.Framework.MacGameNSWindow..ctor (CoreGraphics.CGRect rect, AppKit.NSWindowStyle style, AppKit.NSBackingStore backing, System.Boolean defer) [0x00000] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.MacGamePlatform.InitializeMainWindow () [0x0001d] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.MacGamePlatform..ctor (Microsoft.Xna.Framework.Game game) [0x0003c] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.GamePlatform.PlatformCreate (Microsoft.Xna.Framework.Game game) [0x00000] in <22537961847f41f881e1a9d3cb0e99a8>:0
at Microsoft.Xna.Framework.Game..ctor () [0x001fb] in <22537961847f41f881e1a9d3cb0e99a8>:0
at MyGame.TestGame..ctor () [0x00000] in <c3e40b4cec934c9cba34c73e74a28eb5>:0
at MyGame.Program.Main () [0x00000] in <c3e40b4cec934c9cba34c73e74a28eb5>:0
源代码(main.cs-可能不必要,但我知道有人很可能会要求它。):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MyGame {
class Program {
[STAThread]
static void Main() {
using(var game = new TestGame()) {
game.Run();
}
}
}
}
namespace MyGame {
public class TestGame : Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;
Vector2 position;
public TestGame() {
graphics = new GraphicsDeviceManager(this);
position = new Vector2(0, 0);
}
protected override void Initialize() {
texture = new Texture2D(this.GraphicsDevice, 100, 100);
Color[] colorData = new Color[100 * 100];
for (int i = 0; i < 10000; i++) {
colorData[i] = Color.Red;
}
texture.SetData<Color>(colorData);
base.Initialize();
}
protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent() {
}
protected override void Update(GameTime gameTime) {
if(IsActive) {
if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape)) {
Exit();
}
position.X += 1;
if(position.X > this.GraphicsDevice.Viewport.Width) {
position.X = -100;
}
base.Update(gameTime);
}
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(texture,position,Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}