2

I'm trying to use Span in Unity.

I understand that System.Memory is not supported yet, but I'd like to implement it anyway.

I directly downloaded the library from NuGet. I got the System.Memory.dll from netstandard2.0 folder in the nuget package and dragged it in the project Assets/Scripts folder.

But I get this error, and I cannot use it in my IDE (Rider).

Assembly 'Assets/System.Memory.dll' will not be loaded due to errors: Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?

My question is: how can I use Span in Unity?


Unity Version: Unity 2019.1.7f.
OS: Windows

Jack Mariani
  • 2,270
  • 1
  • 15
  • 30
  • 4
    As the error says, you're missing `System.Runtime.CompilerServices.Unsafe`. Look at the "Dependencies" tab [on NuGet](https://www.nuget.org/packages/system.memory) to see what dependencies it needs. You'll need to check all of your dependencies' dependencies too. Probably easier to follow https://stackoverflow.com/a/53458099/1086121 – canton7 Jul 04 '19 at 09:55
  • Why -1? After some hours of search I made my question here, then after other hours I found my answer (and I noted this answer was asked in multiple places on Unity Forum so it was not just my issue). An answer here might have saved me the hours of the second part of the search. Is this somehow wrong? – Jack Mariani Jul 08 '19 at 07:55

2 Answers2

4

Nuget may generate issues in Unity.

The solution is to directly download the dll.

  1. Get the library from the direct download => Download Package

  2. Import the System.Memory.dll from lib/netstandartd2.0 folder into the Project Window

  3. Select System.Memory.dll in the Project Window then Disable "Validate Reference"

enter image description here

I took the info from unity forum and applied to my situation. (In the forum they are using Unity Packages, while I'm just setting the library on the Assets)

Jack Mariani
  • 2,270
  • 1
  • 15
  • 30
  • 1
    Thank you for this. Have you run into any issues using Spans since then? – Jon Koelzer Oct 16 '19 at 01:32
  • I used it for unity 2019.1.something on a windows server and it worked properly for a few months. Never tried on mobile or other applications. Sometime unity crashed for unknown reasons, but I related it to Enet C# Wrapper library (can't be fully sure). – Jack Mariani Oct 16 '19 at 06:15
1

I had the same issue.

It was solved by downloading the missing package

and copying lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll to my game assets folder.

vise
  • 12,713
  • 11
  • 52
  • 64