简体   繁体   English

OS X 上的 Unity 和 System.Drawing

[英]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.我正在尝试在 OS X Mountain Lion 上使用 Unity (4.3.2) 中 Mono 的 System.Drawing。 I have the following simple C# script to verify it works:我有以下简单的 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);
    }

}

I have copied the System.Drawing.dll to my Assets/Plugins folder in the project.我已将 System.Drawing.dll 复制到项目中的 Assets/Plugins 文件夹中。 I also copied /Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/libgdiplus.dylib to Assets/Plugins.我还将 /Applications/Unity/Unity.app/Contents/Frameworks/Mono/lib/libgdiplus.dylib 复制到 Assets/Plugins。 This makes everything build (CMD-B) in Monodevelop just fine.这使得 Monodevelop 中的所有构建(CMD-B)都很好。 However, when I try to run the project in Unity editor, I get this error message:但是,当我尝试在 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)

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.到 /Applications/Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/config 尝试强制 Unity 找出要加载的 DLL。 I've also set the Player Settings for the project to use ".NET 2.0" Api Compatibility Level (instead of the subset).我还将项目的播放器设置设置为使用“.NET 2.0”Api 兼容级别(而不是子集)。 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).如果我尝试使用 Mono.Cairo(自然而然地使用不同的 dll),也会发生同样的问题。 So my question is, how can I use System.Drawing or any not-enabled-by-default assembly in Unity on Mac?所以我的问题是,如何在 Mac 上的 Unity 中使用 System.Drawing 或任何未默认启用的程序集? 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.我还想最终将应用程序部署到最终用户,因此如果独立构建需要不同的 hack,他们将非常感激。

Install the Homebrew package for GDI+ on macOS:在 macOS 上安装 GDI+ 的 Homebrew 包:

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)您可以使用以下行更新配置文件 /Applications/Unity/Hub/Editor/XXX/Unity.app/Contents/MonoBleedingEdge/etc/mono/config:< 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 是您的 Unity 版本)

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.通过这种方式,您可以在 3d 中创建所有形状,这也可以让您在场景中可视化它们。 If I'm not mistaken Vectrosity (an awesome Unity plugin) does it this way.如果我没记错的话, Vectrosity (一个很棒的 Unity 插件)就是这样做的。 Making a bitmap in memory and transferring coordinates from 3d space to 2d should be relatively straightforward.在内存中制作位图并将坐标从 3d 空间传输到 2d 应该相对简单。

  2. You can just draw pixels directly to a Texture2d object (using SetPixel ) in memory by doing the math yourself.您可以通过自己进行数学运算,将像素直接绘制到内存中的Texture2d对象(使用SetPixel )。 Unity has Rect shape and you could create others to suit your needs. Unity 具有Rect形状,您可以创建其他形状以满足您的需求。 Here are some links that should help.这里有一些应该有帮助的链接。

Polygon Contains多边形包含

Procedural Primitives程序原语

Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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