I am trying to use Scottplot library in to a Unity project in Linux. I am using all the defaults and the last version of unity and Nuget. The installation of Scottplot is fine, but when I run the C# script I get this :
PlatformNotSupportedException: System.Drawing is not supported on this platform.
System.Drawing.FontFamily.get_Families () (at <e29a573067fd4716bad485c84d1b5a87>:0)
ScottPlot.Drawing.InstalledFont..cctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
Rethrow as TypeInitializationException: The type initializer for 'ScottPlot.Drawing.InstalledFont' threw an exception.
ScottPlot.Renderable.Message..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
ScottPlot.Renderable.BenchmarkMessage..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
ScottPlot.Settings..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
ScottPlot.Plot..ctor (System.Int32 width, System.Int32 height) (at <e5a796cb36b74a19a23ccf923391ac39>:0)
ScottPlotExample.Start () (at Assets/NewBehaviourScript.cs:12)
Oh yes, this is the scirpt. It’s quite simple, just an ScottPlot example :
using UnityEngine;
using ScottPlot;
using System.Drawing;
using System.Drawing.Imaging;
public class ScottPlotExample : MonoBehaviour
{
void Start()
{
// Create a new ScottPlot figure
var plt = new Plot(400, 300);
// Generate some sample data
double[] x = { 1, 2, 3, 4, 5 };
double[] y = { 10, 5, 20, 15, 30 };
// Add a scatter plot
plt.PlotScatter(x, y, markerSize: 10, label: "Sample Data");
// Customize the plot (optional)
plt.Title("ScottPlot Example");
plt.XLabel("X-Axis");
plt.YLabel("Y-Axis");
plt.Grid(true);
// Render the plot
plt.Render();
// Convert ScottPlot's System.Drawing.Bitmap to UnityEngine.Texture2D
var texture2D = ConvertBitmapToTexture2D(plt.GetBitmap());
// Get the SpriteRenderer component attached to the GameObject
var spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
// Create a sprite using the Texture2D and set it on the GameObject
spriteRenderer.sprite = Sprite.Create(
texture2D,
new Rect(0, 0, plt.Width, plt.Height),
new Vector2(0.5f, 0.5f)
);
Debug.Log("Hello, Unity!");
}
private Texture2D ConvertBitmapToTexture2D(Bitmap bitmap)
{
// Convert System.Drawing.Bitmap to System.Drawing.Image
Image image = (Image)bitmap;
// Create a MemoryStream to hold the image bytes
MemoryStream memoryStream = new MemoryStream();
// Save the image to the MemoryStream in PNG format
image.Save(memoryStream, ImageFormat.Png);
// Create a new Texture2D and load the image bytes from the MemoryStream
Texture2D texture2D = new Texture2D(1, 1);
texture2D.LoadImage(memoryStream.ToArray());
return texture2D;
}
}
I very much doubt that would work because System.Drawing encapsulates the Windows GDI+ if I remember correctly so it’s a Windows only API. AFAIK, Mono supports it but again, only on the Windows platform.
It’s as it says in the tin: the library is relying on System.Drawing, which only works on Windows. You’ll have to look for a different library that works on Linux as well.
I just noticed that in the Scottplot library install guide it says that it can be used in Linux, but recommend editing runtimeconfig.json to enable Unix support. I’m new to Unity and C#, so I’m struggling now to understand how to achieve this : ScottPlot dependencies - ScottPlot FAQ
Note that libgdiplus is already installed.
Linux & MacOS
Non-Windows users must take the following steps to enable System.Drawing.Common rendering support:
Step 1: Install libgdiplus
Linux: apt-get install -y libgdiplus
MacOS: brew install mono-libgdiplus
Step 2: Manually add the System.Drawing.Common package to your project:
dotnet add package System.Drawing.Common
Step 3: Follow Microsoft’s recommended action and edit runtimeconfig.json to EnableUnixSupport.
Step 4: Ensure your project does not include a version of System.Drawing.Common newer than 5.x because newer versions of System.Drawing.Common are not supported on non-Windows systems