简体   繁体   中英

Unity and System.Drawing on OS X

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.

Install the Homebrew package for GDI+ on macOS:

brew install mono-libgdiplus

Update: I have searched for the same problem, and found another answer, I tried and that works for me:

You can update the config file /Applications/Unity/Hub/Editor/XXX/Unity.app/Contents/MonoBleedingEdge/etc/mono/config with the following line: < dllmap dll="gdiplus" target="/Library/Frameworks/Mono.framework/Versions/XXX/lib/libgdiplus.dylib" / > < dllmap dll="gdiplus.dll" target="/Library/Frameworks/Mono.framework/Versions/XXX/lib/libgdiplus.dylib" / > (XXX is your Unity version)

Based on your comment I can recommend two things :

  1. Have a lookhere . This way you could create all your shapes in 3d which would also let you visualize them in the scene. If I'm not mistaken Vectrosity (an awesome Unity plugin) does it this way. Making a bitmap in memory and transferring coordinates from 3d space to 2d should be relatively straightforward.

  2. You can just draw pixels directly to a Texture2d object (using SetPixel ) in memory by doing the math yourself. Unity has Rect shape and you could create others to suit your needs. Here are some links that should help.

Polygon Contains

Procedural Primitives

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM