Using drawing package like System.Drawing

I need to dynamically(in script) draw on texture.

I know I can use Texture2D.SetPixel, but I need shapes(which I can implement by my own) and text(which is much more complex).

I know I can do something like a camera drawing to texture and use GUI to draw on it, but GUI is rather lacking most of the drawing feature in package like System.Drawing.

What I thought of doing is writing to System.Drawing.Bitmap using System.Drawing.Graphics, save the Bitmap to memory stream and then using Texture2D.LoadImage to load the image. But then I noticed System.Drawing is disabled in Unity.

I found a similar problem in forums regarding this, but the answer was that its not compatible with OpenGL/DirectX.

What I need to do is not drawing to screen, I need to draw to memory stream which is fully compatible with both OpenGL and DirectX (I was using this in an DirectX project I’m trying to convert to Unity now).

What can I do?

Hi there,

not sure if still someone wants to know how to use System.Drawing even if it’s not officially supported by Unity.
Ok here is what I’ve found out for Unity 3.5.1f2 so far:

  1. If you wanna use it and don’t want to get Errors in Unity Editor (not MonoDevelop) you must drag the file System.Drawing.dll into your Project tree. On default Windows Installation the file can be found in the following directory: %Unity Folder%\Editor\Data\Mono\lib\mono\2.0 on OSX it should be in the Unity.app in somewhere. You may also be able to use the current System.Drawing.dll of Mono 2.10.8 but I didn’t test this yet.
  2. The Api Compatibility Level must be set to .NET 2.0!! The option “.NET 2.0 Subset” doesn’t work.
  3. For a Windows only version you would be done now and it should work. I didn’t test the whole functionality of System.Drawing!! I just tested some of the Bitmap functionality.
  4. Additional step for MacOSX:
    After you’ve build the Application you should open the .app/Contents directory. Create a Plugins directory there and the copy the file libgdiplus.dylib from the lib directory of Mono 2.10.8 for OSX (On windows you can use 7-zip and some other tools to get the libgdiplus.dylib from the OSX download image)! After another test the version of Unity at least the one shipped with Windows Unity doesn’t work. Now rename the file libgdiplus.dylib to gdiplus.dll.bundle. After that it should also work under OSX.

As I’m new to Unity I’m not sure if it’s possible to do the last step over the editor too so it’ll do that step automatically.

Under OSX I used XCodes Instruments to find out where Unity application tries to load the gdiplus.dll from and has choosen a path that is inside the applications directory (Plugins). Don’t ask me why it tries just to load a file or normally a bundle with the name gdiplus.dll.bundle instead of libgdiplus.dylib like it does in other directories.

Ok that’s it so far hope this will help someone somehow. As I said I didn’t use the full functionality of System.Drawing.dll nor did I test if really every class / function works.

WARNING: COMMERCIAL SOLUTION

You can use AltSketch Graphics Library that has System.Drawing like program interface and fully C# managed code (Mono, Mobile platforms supported too).

AltSketch project forum thread here:
http://forum.unity3d.com/threads/on-sale-50-off-altsdk-altassets-altgui-altos-altsketch-altstudio-frameworks-editors.335966/#post-2182031

AltSketch Description:

AltSketch is a pure C# CLS compliant 100% managed, without unsafe blocks Vector Graphics Library with Software and Hardware Render Backends for Games, Scientific Applications & other uses of Computer Graphics. All core functionality collected in one lightweight library AltSketch.dll for Microsoft .NET Framework or Mono (including Silverlight / Moonlight). It depends only on System assembly (.NET Framework 2.0).

AltSketch has Software and Hardware Render Backends (solid and texure AA visual data triangulation), contains a subsystem AltNETType - port of wonderful font rendering library Freetype and subsystem AltNETImage for Image manipulations (loading, saving, transformations).

As AltSketch has several integrations with most popular GUI systems and Mobile platforms, you can develop and test a drawing part of your 3D application/game as independent program, and then easy integrate it into your main solution. Now you can draw Graphics in 3D with Brushes and Pens like you do it with any drawing library. It is flexible and useful development method in case of main project bulking and test complexity, or useful for team development process. Now you can develop graphics part of your Mobile applications without using an emulator as stand-alone application, only running on Mobile devices (or Emulator) for final tests.

AltSketch has Virtual File System support with Zip containers for resources loading. All AltSketch functionality & extensions works via VirtualFS and all resources can be loaded as from real file system, so from Zip containers (from files or memory streams).

AltSketch SDK includes wide variety of open source extensions: ported to AltSketch popular libraries (GUI, Plot, GIS, Computer Science, Physics, Graphics etc.): AForge, NPlot, OxyPlot, PieChart, ZedGraph, MigraDoc, PDFsharp, GMap, HTML Renderer, Box2D, Farseer Physics, etc.

AltSketch SDK includes many Integration Demos with a wide variety GUI-s & frameworks (all Integrations & HW Render Backends are open sourced, so you can easily adapt it to your applications or improve it if you need)

alt text

This is a super old answer, but I am looking at getting SkiaSharp to work with Unity. SlkiaSharp is a powerful, free, open-source 2D graphics engine that supports all .NET platforms.

It already works with Unity, but I am having a few issues in some areas on some platforms: Linux build is putting libNative.so in wrong place - Unity Answers

Information:

A good portion of the AForge.NET graphics framework (AFAIK, all of AForge.Imaging) is Mono-compatible (according to this forum post).

I haven’t yet taken the steps to build a DLL and try it in Unity— would love to hear if anyone else takes a stab at it.  And I’ll update this answer if I get it working.

for mac
download: Download DLL file system.drawing.dll
place into assets folder.
seemed to work for me.