我正在尝试在OS X Mountain Lion上使用Mono中的System.Drawing来进行Unity(4.3.2)的开发。我有以下简单的C#脚本来验证它是否可行:
using UnityEngine;
using System.Drawing;
public class DrawingTest : MonoBehaviour {
void Start () {
Bitmap bitmap = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.DrawRectangle(new Pen(System.Drawing.Color.Red, 4), 0, 0, 128, 128);
bitmap.Save("/Users/username/drawing.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
我已将System.Drawing.dll文件复制到项目的Assets/Plugins文件夹中。我还复制了/Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/libgdiplus.dylib文件到Assets/Plugins中。这样可以在Monodevelop中顺利构建(CMD-B)所有内容。然而,在尝试在Unity编辑器中运行项目时,我收到了以下错误消息:
DllNotFoundException: libgdiplus.dylib
System.Drawing.GDIPlus..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Drawing.GDIPlus
System.Drawing.Bitmap..ctor (Int32 width, Int32 height, PixelFormat format)
(wrapper remoting-invoke-with-check) System.Drawing.Bitmap:.ctor (int,int,System.Drawing.Imaging.PixelFormat)
DrawingTest.Start () (at Assets/DrawingTest.cs:9)
我还添加了以下这行代码:
<dllmap dll="gdiplus.dll" target="libgdiplus.dylib" os="osx"/>
为了让Unity能够找到要加载的DLL,我尝试编辑 /Applications/Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/config 文件。同时,我也将项目的Player Settings设置为使用“.NET 2.0”兼容级别(而不是子集)。即使我构建了该项目并运行独立可执行文件,问题仍然存在。
如果我尝试使用Mono.Cairo(自然使用不同的DLL),也会出现同样的问题。我的问题是,如何在Mac上的Unity中使用System.Drawing或者其他默认未启用的程序集?我还想最终将该应用程序部署给最终用户,因此,如果独立构建需要使用不同的技巧,则非常感激。