0
\$\begingroup\$

I'm using a dll in my project which is built against a higher target framework (4.6.1 actually)

I can't build or attach to Unity because of this. Visual Studio shows this error:

The primary reference "MQTTnet" could not be resolved because it was built against the ".NETFramework,Version=v4.6.1" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.6".

The only option in player settings in Unity for API compatibility level is .NET 4.6

how can I change that to 4.6.1 in order to be able to build or attach to Unity?

\$\endgroup\$
1
  • \$\begingroup\$ For 2020, on Mac, this really is just an insane Unity bug. Here is one fix forum.unity.com/threads/… \$\endgroup\$
    – Fattie
    Aug 25, 2020 at 14:52

3 Answers 3

2
\$\begingroup\$

Add the following script anywhere in your project to automatically change the target framework of the c# project files.

using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

public class VisualStudioProjectGenerationPostProcess : AssetPostprocessor
{
    private static void OnGeneratedCSProjectFiles()
        {
            Debug.Log("OnGeneratedCSProjectFiles");
            var dir = Directory.GetCurrentDirectory();
            var files = Directory.GetFiles(dir, "*.csproj");
            foreach (var file in files)
                ChangeTargetFrameworkInfProjectFiles(file);
        }

    static void ChangeTargetFrameworkInfProjectFiles(string file)
    {
        var text = File.ReadAllText(file);
        var find = "TargetFrameworkVersion>v4.6</TargetFrameworkVersion";
        var replace = "TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion";

        if (text.IndexOf(find) != -1)
        {
            text = Regex.Replace(text, find, replace);
            File.WriteAllText(file, text);
        }
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ an impressive tip \$\endgroup\$
    – Fattie
    Aug 25, 2020 at 14:08
2
\$\begingroup\$

Since the documented OnGeneratedCSProjectFiles callback has been removed, the VisualStudioProjectGenerationPostProcess is no longer working.

Workaround

Move Library/PackageCache/[email protected] to Packages/[email protected]

Edit Editor/ProjectGeneration/ProjectGeneration.cs, search for k_TargetFrameworkVersion, and change to your desired version.

Remember to regularly check if com.unity.ide.vscode should be updated. You can watch the changes on https://github.com/needle-mirror/com.unity.ide.vscode

\$\endgroup\$
1
\$\begingroup\$

Found a workaround for this.

look for TargetFrameworkVersion in every .csproj file and change v4.6 to v4.6.1

I used a sed script to automate this, and run I it every time Unity generates a project:

sed -i 's/TargetFrameworkVersion>v4.6<\/TargetFrameworkVersion/TargetFrameworkVersion>v4.6.1<\/TargetFrameworkVersion/g' next-step/Source/*.csproj
\$\endgroup\$
4
  • \$\begingroup\$ Just make sure this still works when actually compiling the game. I have a suspicion that it won't at least for some target platforms. \$\endgroup\$
    – Ed Marty
    Dec 23, 2018 at 0:16
  • \$\begingroup\$ My target platforms are Android and iOS and there hasn't been any problem so far. Also this will change .csproj files. Does Unity use Visual Studio .csproj files for anything when building? I guess it shouldn't and this can't cause any problem. \$\endgroup\$ Dec 23, 2018 at 13:37
  • \$\begingroup\$ It does not. What I'm getting at its that you are apparently using a third party library that requires .NET 4.6.1 but Unity uses 4.6. \$\endgroup\$
    – Ed Marty
    Dec 24, 2018 at 7:23
  • \$\begingroup\$ My guess is Unity is not actually using 4.6. 4.6 is just the general version number used in player settings. But it puts 4.6 as target framework in csproj files. We are extensively using that library in our project, and never had an issue with this. Unity must actually be using a higher and different version of .Net in builds. \$\endgroup\$ Dec 24, 2018 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .