Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Any way to force .net 4.6.1? 4.7.1 unavailable for OS

Discussion in 'Experimental Scripting Previews' started by hunterofakara, Oct 18, 2018.

  1. hunterofakara

    hunterofakara

    Joined:
    Oct 4, 2018
    Posts:
    21
    Since scripting runtime .net 3.5 is being deprecated, I've tried switching to 4.x in the beta, but it seems to jump to using 4.7.1 without any middleground.

    When visual studio is launched, it asks to convert to 4.6.1 each time since the 4.7.1 targeting pack isn't installed.
    I cannot currently install any higher because my OS version doesn't support it.

    I can still use VS to edit scripts, but tracking down errors is tricky without direct integration. (Unity is complaining it can't find Microsoft.Win32.Registry after switching to 4.x, but VS intellisense shows it just fine for example)

    Is there any way to get unity to use 4.6.1 specifically instead of the generic 4.x profile?

    I understand upgrading my OS is one solution, but it'd be a shame if this was the first thing that flat out didn't work on my current one due to such a minor difference.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    We don't have a way to change the target framework version in the .csproj file that Unity generates now, but we are working on a solution for this issue. Which OS are you using that you cannot install .NET 4.7.1?
     
  3. hunterofakara

    hunterofakara

    Joined:
    Oct 4, 2018
    Posts:
    21
    A pre-anniversary update windows 10. Particularly annoying that .net 4.7.1 works for Win7 SP1 from 2011 but not a windows 10 install from 2015.
    Mono 5.10 that supports .net 4.7.1 installs just fine by comparison.

    Okay, thanks - I'll go back to 3.5 for now and hope something gets worked out before it gets fully removed.

    I know I'm quite silly/stubborn about not updating in so long, but at this point it'll be a knock-on effect that'll cause a whole bunch more conflicts than it'll solve heh.
     
  4. van800

    van800

    JetBrains Employee

    Joined:
    May 19, 2016
    Posts:
    72
    When you use JetBrains Rider, its EditorPlugin has an option to manually override TargetFrameworkVersion.
     
  5. sailro

    sailro

    Microsoft

    Joined:
    Jul 30, 2014
    Posts:
    167
    Qbit86 likes this.
  6. hunterofakara

    hunterofakara

    Joined:
    Oct 4, 2018
    Posts:
    21
    Thanks! With that as a base, I found/modified something similar and got it working.
    Here's the result for if anyone in the future has similar problems:

    Code (CSharp):
    1. using System.IO;
    2. using System.Linq;
    3. using System.Text;
    4. using System.Xml.Linq;
    5.  
    6. using UnityEditor;
    7.  
    8. //#if ENABLE_VSTU //not defined in VS for me for some reason
    9. using SyntaxTree.VisualStudio.Unity.Bridge;
    10.  
    11. [InitializeOnLoad]
    12. public class ProjectFileHook
    13. {
    14.     private const string SCHEMA = @"http://schemas.microsoft.com/developer/msbuild/2003";
    15.     private const string ASSEMBLY_NAME = "AssemblyName";//XML category for assembly
    16.     private const string ASSEMBLY_CSHARP_RUNTIME = "Assembly-CSharp";//value for assembly name
    17.     private const string TARGET_FRAMEWORK_VERSION = "TargetFrameworkVersion";//XML category for .net framework
    18.     private const string FRAMEWORK_EXPECTED = "v4.7.1";//old value for framework
    19.     private const string FRAMEWORK_DESIRED = "v4.6.1";//new value for framework
    20.    
    21.     class Utf8StringWriter : StringWriter
    22.     {//to allow UTF8 saving
    23.         public override Encoding Encoding
    24.         {
    25.             get { return Encoding.UTF8; }
    26.         }
    27.     }
    28.  
    29.     static ProjectFileHook()
    30.     {
    31.         ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
    32.         {
    33.             var document = XDocument.Parse(content);
    34.             var assemblyName = document.Descendants(XName.Get(ASSEMBLY_NAME, SCHEMA)).FirstOrDefault();
    35.             if( null != assemblyName && assemblyName.Value.Contains(ASSEMBLY_CSHARP_RUNTIME) )
    36.             {
    37.                 var target = document.Descendants(XName.Get(TARGET_FRAMEWORK_VERSION, SCHEMA)).FirstOrDefault();
    38.                 if( null != target && target.Value.Contains(FRAMEWORK_EXPECTED) )
    39.                 {
    40.                     target.SetValue(FRAMEWORK_DESIRED);
    41.                 }
    42.             }
    43.             var str = new Utf8StringWriter();
    44.             document.Save(str);
    45.  
    46.             return str.ToString();
    47.         };
    48.     }
    49. }
    50. //#endif

    With that working, I managed to use a nuget asset to get microsoft.win32.registry.dll included (though had to fiddle a lot with the dll import settings) and it all compiles!
    Hopefully there'll be support for not overwriting nuget stuff added by VS in the future, since some of the more specialized references aren't there directly anymore when using 2.0 standard if I understand it right.
    Would be a lot less hastle letting VS manage it all and tell unity about each DLL automatically than having to use a clone of the same functionality within unity.