Can't use C# .NET 6 PriorityQueue in Unity
Asked Answered
I

1

8

I'm trying to use PriorityQueue in Unity with C#. The documentation says that it's supported in .NET 6 in namespace System.Collections.Generic.

I've tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Test : Monobehaviour
{
    void Start()
    {
        var queue = new PriorityQueue<int, int>();
    }
}

But an error is thrown back:

The type or namespace name 'PriorityQueue<,>' could not be found (are
you missing a using directive or an assembly reference?)
[Assembly-CSharp]

I've checked the .NET version within VS Code: enter image description here

Why doesn't it work in Unity?

Institutional answered 3/1, 2022 at 15:44 Comment(7)
What is the target framework version in the project file? Just running dotnet from a prompt doesn't prove much.Hyposensitize
@JeroenMostert I found this in the .csproj file <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>. This is the problem?Institutional
Well yes, it's obviously targeting .NET 4.7.1, and given the name of that element this is an old-style .csproj, not the new-style (that uses TargetFramework and TargetFrameworks, and is a lot shorter besides). I'm not sure Unity even has support for .NET Core (yet), which 6 is, but that's another question.Hyposensitize
@JeroenMostert I'm currently using Unity 2020.3, a bit behind now, not sure if that's the problem.Institutional
@JeroenMostert just search around in Unity doc 2020.3 a bit, said that .NET standard 2.0 and .NET 4.x are supported. Yeah... Seems like even Unity 2022.1 doc said the same thing, so...Institutional
Fortunately priority queues are not rocket science (if that's the only thing you're going for in terms of .NET 6); compatible implementations should be easy to find. Sufficiently unimportant queues that aren't crucially dependent on performance can even be faked with other data structures (like a SortedList or SortedDictionary using a tuple that includes the priority and an ID for uniqueness, with only the priority used for sorting through a custom comparer).Hyposensitize
I have the same issue. I checked visual studio installer and know .NET 6 is installed. Project target framework is .NET Framework 4.8. I'm also just using this in a console application in visual studio, nothing special like unity involved.Palpate
V
26

The version reported by dotnet is completely unrelated to Unity's C#/.NET version (at least until Unity finally migrates from Mono). While the language supported is almost equal to C# 9.0, the actual library that is used is a loose superset of what Mono provides, and actually is roughly equal to .NET Framework 4.8/.NET Standard 2.0 ATM; since neither of those has PriorityQueue, it's not available. You can either:

Veron answered 20/8, 2022 at 20:57 Comment(1)
Yeah, I have imported C5 and used their IntervalHeap since then, pretty fine!Institutional

© 2022 - 2024 — McMap. All rights reserved.