Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Trying to use a plot library in to a unity game in Linux, but get error

Discussion in 'Scripting' started by cfunk82, Sep 8, 2023.

  1. cfunk82

    cfunk82

    Joined:
    Dec 12, 2021
    Posts:
    3
    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 :

    Code (CSharp):
    1. PlatformNotSupportedException: System.Drawing is not supported on this platform.
    2. System.Drawing.FontFamily.get_Families () (at <e29a573067fd4716bad485c84d1b5a87>:0)
    3. ScottPlot.Drawing.InstalledFont..cctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
    4. Rethrow as TypeInitializationException: The type initializer for 'ScottPlot.Drawing.InstalledFont' threw an exception.
    5. ScottPlot.Renderable.Message..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
    6. ScottPlot.Renderable.BenchmarkMessage..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
    7. ScottPlot.Settings..ctor () (at <e5a796cb36b74a19a23ccf923391ac39>:0)
    8. ScottPlot.Plot..ctor (System.Int32 width, System.Int32 height) (at <e5a796cb36b74a19a23ccf923391ac39>:0)
    9. ScottPlotExample.Start () (at Assets/NewBehaviourScript.cs:12)
    How can I make this work ? Thanks
     
  2. Deleted User

    Deleted User

    Guest

  3. cfunk82

    cfunk82

    Joined:
    Dec 12, 2021
    Posts:
    3
    Oh yes, this is the scirpt. It's quite simple, just an ScottPlot example :

    Code (CSharp):
    1. using UnityEngine;
    2. using ScottPlot;
    3. using System.Drawing;
    4. using System.Drawing.Imaging;
    5.  
    6.  
    7. public class ScottPlotExample : MonoBehaviour
    8. {
    9.     void Start()
    10.     {
    11.  
    12.  
    13.         // Create a new ScottPlot figure
    14.         var plt = new Plot(400, 300);
    15.  
    16.         // Generate some sample data
    17.         double[] x = { 1, 2, 3, 4, 5 };
    18.         double[] y = { 10, 5, 20, 15, 30 };
    19.  
    20.         // Add a scatter plot
    21.         plt.PlotScatter(x, y, markerSize: 10, label: "Sample Data");
    22.  
    23.         // Customize the plot (optional)
    24.         plt.Title("ScottPlot Example");
    25.         plt.XLabel("X-Axis");
    26.         plt.YLabel("Y-Axis");
    27.         plt.Grid(true);
    28.  
    29.         // Render the plot
    30.         plt.Render();
    31.  
    32.         // Convert ScottPlot's System.Drawing.Bitmap to UnityEngine.Texture2D
    33.         var texture2D = ConvertBitmapToTexture2D(plt.GetBitmap());
    34.  
    35.         // Get the SpriteRenderer component attached to the GameObject
    36.         var spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    37.  
    38.         // Create a sprite using the Texture2D and set it on the GameObject
    39.         spriteRenderer.sprite = Sprite.Create(
    40.             texture2D,
    41.             new Rect(0, 0, plt.Width, plt.Height),
    42.             new Vector2(0.5f, 0.5f)
    43.         );
    44.  
    45.         Debug.Log("Hello, Unity!");
    46.     }
    47.  
    48.     private Texture2D ConvertBitmapToTexture2D(Bitmap bitmap)
    49.     {
    50.         // Convert System.Drawing.Bitmap to System.Drawing.Image
    51.         Image image = (Image)bitmap;
    52.  
    53.         // Create a MemoryStream to hold the image bytes
    54.         MemoryStream memoryStream = new MemoryStream();
    55.  
    56.         // Save the image to the MemoryStream in PNG format
    57.         image.Save(memoryStream, ImageFormat.Png);
    58.  
    59.         // Create a new Texture2D and load the image bytes from the MemoryStream
    60.         Texture2D texture2D = new Texture2D(1, 1);
    61.         texture2D.LoadImage(memoryStream.ToArray());
    62.  
    63.         return texture2D;
    64.     }
    65. }
    66.  
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,761
  5. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    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.
     
  6. cfunk82

    cfunk82

    Joined:
    Dec 12, 2021
    Posts:
    3
    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 : https://scottplot.net/faq/dependencies/
    Note that libgdiplus is already installed.

    Code (CSharp):
    1. Linux & MacOS
    2. Non-Windows users must take the following steps to enable System.Drawing.Common rendering support:
    3.  
    4. Step 1: Install libgdiplus
    5.  
    6. Linux: apt-get install -y libgdiplus
    7.  
    8. MacOS: brew install mono-libgdiplus
    9.  
    10. Step 2: Manually add the System.Drawing.Common package to your project:
    11.  
    12. dotnet add package System.Drawing.Common
    13. Step 3: Follow Microsoft’s recommended action and edit runtimeconfig.json to EnableUnixSupport.
    14.  
    15. 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
    16.  
    17.