- Home /
System.Drawing or equivalent.,Getting System.Drawing to work in unity - or alternative library.
I need to draw a curve passing through a set a points. System.Drawing has the perfect function for it, Graphics.DrawCurve(), however, I cannot get it to work.
System.Drawing.Common.dll compiles with .Net standard 2.0, but does not seem to be supported. Error: PlatformNotSupportedException: System.Drawing is not supported on this platform.
With other version of the .dll/ .NET the compiler cannot find System.Drawing.Graphics.
Anyone has any idea if it is possible to get this to work (and explanation as to why not, if that is the case would also be greatly appreciated)? Or if there is any other library with a similar function? I found several options for drawing Bezier curves, but I cannot find anything that will fit a curve to a list of points the way DrawCurve does.
Answer by Bunny83 · Nov 20, 2019 at 03:44 PM
Well first of all it should be noted that the Graphics class in the System.Drawing namespace just capsulates a GDI+ rendering surface which is a Windows / Microsoft proprietary interface. Since Unity uses Mono or the IL2CPP compiler it's quite difficult to actually get it working. The classical System.Drawing.dll requires the actual .NET framework and a windows system. Mono does have it's own System.Drawing implementation however it still just capsulates the GDI+ native objects of the windows API.
Using System.Drawing, even when building for windows PCs only, would be a complete overkill. This assembly (and all dependencies) are huge. Also you would actually create a native windows object (like a bitmap or DIP image) where you would need to extract the pixel information, convert it into something Unity can use and create an actual Texture2D in Unity. Of course it's probably possible with some native code plugin magic to somehow transfer the data on a lower level. Though this is way too complicated to draw a curve.
As you can read in the documenation of DrawCurve it just draws a cardinal CatmullRom spline. The default tension is set to 0.5 unless you use an overload and specify a different one. If you don't want to work through the interpolation of such a spline yourself, the Centripetal Catmull-Rom spline wikipedia article has a rudimentary implementation. This implementation is not really well made since it should just show an example. I haven't really used the following things nor checked their quality. However I think they should provide you some code examples and snippets to get started:
https://www.habrador.com/tutorials/interpolation/1-catmull-rom-splines/
http://wiki.unity3d.com/index.php/Hermite_Spline_Controller
https://en.wikibooks.org/wiki/Cg_Programming/Unity/Hermite_Curves
https://assetstore.unity.com/detail/tools/modeling/splinemesh-104989(related forum post)
Some paid assets my google search found:
https://assetstore.unity.com/detail/tools/animation/aurora-spline-9953
https://assetstore.unity.com/detail/tools/utilities/dreamteck-splines-61926
ps: you haven't mentioned what platform(s) you actually want to target.
Thank you so much! That is exactly what I wanted to know. With the Wikipedia article I should be able to get my own implementation. (platform was just windows, but agreed, Graphics looks like an overkill.)
I dislike this answer. It offers alternatives due to "bad design" over the op's query regarding getting system.drawing working. Regardless if he got it working fine, ill continue trawling as this solution wont work for me.