簡體   English   中英

OS X 上的 Unity 和 System.Drawing

[英]Unity and System.Drawing on OS X

我正在嘗試在 OS X Mountain Lion 上使用 Unity (4.3.2) 中 Mono 的 System.Drawing。 我有以下簡單的 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"/>

到 /Applications/Unity/Unity.app/Contents/Frameworks/Mono/etc/mono/config 嘗試強制 Unity 找出要加載的 DLL。 我還將項目的播放器設置設置為使用“.NET 2.0”Api 兼容級別(而不是子集)。 如果我構建項目並運行獨立的可執行文件,問題仍然存在。

如果我嘗試使用 Mono.Cairo(自然而然地使用不同的 dll),也會發生同樣的問題。 所以我的問題是,如何在 Mac 上的 Unity 中使用 System.Drawing 或任何未默認啟用的程序集? 我還想最終將應用程序部署到最終用戶,因此如果獨立構建需要不同的 hack,他們將非常感激。

在 macOS 上安裝 GDI+ 的 Homebrew 包:

brew install mono-libgdiplus

更新:我搜索了同樣的問題,並找到了另一個答案,我試過了,這對我有用:

您可以使用以下行更新配置文件 /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 版本)

根據您的評論,我可以推薦兩件事:

  1. 看看這里 通過這種方式,您可以在 3d 中創建所有形狀,這也可以讓您在場景中可視化它們。 如果我沒記錯的話, Vectrosity (一個很棒的 Unity 插件)就是這樣做的。 在內存中制作位圖並將坐標從 3d 空間傳輸到 2d 應該相對簡單。

  2. 您可以通過自己進行數學運算,將像素直接繪制到內存中的Texture2d對象(使用SetPixel )。 Unity 具有Rect形狀,您可以創建其他形狀以滿足您的需求。 這里有一些應該有幫助的鏈接。

多邊形包含

程序原語

希望這會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM