I'm trying to use System.Drawing from Mono in Unity (4.3.2) on OS X Mountain Lion. I have the following simple C# script to verify it works:
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);
}
}
I have copied the System.Drawing.dll to my Assets/Plugins folder in the project. I also copied /Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/libgdiplus.dylib to Assets/Plugins. This makes everything build (CMD-B) in Monodevelop just fine. However, when I try to run the project in Unity editor, I get this error message:
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)
I have also added the following line:
<dllmap dll="gdiplus.dll" target="libgdiplus.dylib" os="osx"/>
to /Applications/Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/config to try to force Unity to figure out what DLL to load. I've also set the Player Settings for the project to use ".NET 2.0" Api Compatibility Level (instead of the subset). The problem persists also if I build the project and run the standalone executable.
The same problems happen if I try to use Mono.Cairo (with different dlls, naturally). So my question is, how can I use System.Drawing or any not-enabled-by-default assembly in Unity on Mac? I would also like to eventually deploy the application to end-users, so if the standalone build requires different hacks, they would be very much appreciated.