- Home /
The Issue with 'Func'
I'm having problems with addons defining their own version of 'Func' so that they can use the syntax from later versions of .NET.
The problem is that they don't define func in their own namespaces and instead define them in the application namespace, which means they inevitably conflict.
The package manager itself does this, so for example I can't use several .NET WebSocket classes with Unity without having to erase the package manager from the package cache every single time I start Unity, which gets very old.
Is it possible for Unity projects to override Func in their own namespace and not do so in the package namespace, which should prevent this problem from occurring? Is there any trivial workaround anyone has discovered to resolve this?
Answer by Bunny83 · Jul 31, 2019 at 11:23 PM
I think I don't quite understand the question. Which type do you actually mean? Defined in which assembly? What do you mean by application namespace? Do you mean the "global::" namespace? You can always use an a fully qualified classname to address a certain type. If you actually mean that you have two or more assemblies in your project which define all the same type within the same namespace, yes, that's a problem. Usually you can solve such issues with Assembly aliases, however there's no real support for them in Unity.
However I don't know any type that comes with Unity that is defined in the global namespace. I think the only class I can find is the deprecated "AssetModificationProcessor" which was originally defined in the global namespace but was moved into the proper UnityEditor namespace long time ago.
I'm also not sure what exactly you mean by "Func". Do you talk about the Func delegate types defined in the System namespace? What "addons" are those? Are they official addons or third-party addons? If they are third-party you should direct your complaint towards the developer of those addons.
Yes, I'm referring to the System.Func delegate.
It didn't exist in .NET 2, but rather than defining their own new delegate function, people appear to be defining their own versions of System.Func for use in their code.
"Addins" was not the correct term.
Defining a 'Func' delegate is done in a number of Asset Store assets (Soft$$anonymous$$ask, for example), as well as in the Unity Package $$anonymous$$anager, and also in a number of 3rd party assemblies such as WebSocket4Net. Each does this for .NET 2 compatibility reasons, but when each library re-introduces their own version of System.Func, it causes conflicts and compiler errors.
While it is true that there are many assemblies that come with .NET 2.0 support, they usually have different projects for different target frameworks. Just don't use the .NET 2.0 version. For example WebSocket4Net comes with 2.0, 3.5, 4.0 and 4.5 support. If you load the source code yourself you should build the right assembly. So while the 2.0 project does include the Action.cs file (there's no Func definition in this project that I've found), From version 3.5 onwards the Action.cs file is no longer included
So you most likely use the wrong version. The Unity Package manager does not define the Sytem.Func delegate types as far as I can tell. $$anonymous$$aybe you load some dubious third-party package. I don't have that Soft$$anonymous$$ask asset from the store, however glossing over the content I can't spot a file which looks like it might define their own System.Func types.
Your answer