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

Mono 5.10.0+ Unity3D integration (for C# 7 support)?

Discussion in 'Experimental Scripting Previews' started by Dejavu2017, Mar 12, 2018.

  1. Dejavu2017

    Dejavu2017

    Joined:
    Sep 29, 2017
    Posts:
    14
    From this page: http://www.mono-project.com/docs/about-mono/releases/5.10.0/, it seems that Mono has gained better C# 7 support as of 2018, and I'm assuming that the upcoming release of Mono 5.12.0 would be even better. What's the plan for Unity3D to integrate the latest greatest Mono releases so C# 7 can finally be "officially" possible in Unity3D?
     
    JesOb likes this.
  2. z00n

    z00n

    Joined:
    Nov 24, 2009
    Posts:
    44
    It's already done. 2018.1.0b10 has `Mono C# compiler version 5.11.0.0`.
     
    Dejavu2017 likes this.
  3. Dejavu2017

    Dejavu2017

    Joined:
    Sep 29, 2017
    Posts:
    14
    I just installed b10, and this is confirmed. On a related note: is it easy to add an option in the "Player Settings" under "Configuration", where "Scripting Runtime Version" locates, another option is added: "C# Language Version", which contains all the currently *valid* Mono supported C# language versions: ISO-1, ISO-2, 3, 4, 5, 6, Default, or Experimental (extracted from mcs --help), so if I chose "Experimental", the generated .csproj file(s) will have the version specified under the "<LangVersion>" section, while currently it's hard coded to "6" for "Stable (.NET 4.x Equivalent)". And there has been issues to manually change "LangVersion" manually - I even tried, as a hack, the mcs.rsp to specify "-langversion: Experimental", and it didn't seem to work for me.
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,935
    In Unity 2018.1, we're supporting C#6. We're looking at C#7 for 2018 or 2018.3, although we've not done enough verification to know when we can upgrade yet. I would expect it to happen soon though.
     
    lloydv, Realtime-art-dev and joncham like this.
  5. z00n

    z00n

    Joined:
    Nov 24, 2009
    Posts:
    44
    I use some #C 7.X features (ref struct, ref return, value tuples) since 2018.1.0b6 or 7. I also use netstandard 2.0 and some cool pre-release libs from Nuget: System.Memory, System.Runtime.CompilerServices.Unsafe etc. They all work, but with every new beta be ready to spend some time fixing some errors and minor issues.

    In 2018.1.0b10 you cannot get all C# 7.X features, only those listed there: https://github.com/mono/mono/issues/6854

    To enable C# 7.2:

    1. Put `-langversion:7.2` in mcs.rsp.

    2. Put `<PropertyGroup><LangVersion>7.2</LangVersion> </PropertyGroup>` in csproj files.

    Of course, manually editing csproj is a bad idea. I use Rider EAP and the patched plugin from https://github.com/JetBrains/resharper-unity/
     
    Qbit86 likes this.
  6. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    In Addition to Put `-langversion:7.2` in mcs.rsp

    I have wrote EditorExtension than do the job for VS

    Put it to separate asmdef and enable for editor only

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.Text;
    4. using SyntaxTree.VisualStudio.Unity.Bridge;
    5. using UnityEditor;
    6. using UnityEditor.Compilation;
    7. using UnityEditorInternal;
    8. using UnityEngine;
    9.  
    10. [InitializeOnLoad]
    11. public class CsProjFixer
    12. {
    13.     static CsProjFixer()
    14.     {
    15.         ProjectFilesGenerator.ProjectFileGeneration += FixProj;
    16.         //CompilationPipeline.assemblyCompilationStarted += a => Debug.Log( "[CsProjFixer] - FixProj: " + a );
    17.     }
    18.     private static String FixProj( String name, String content )
    19.     {
    20.         var path = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(name.Substring(0, name.Length-7));
    21.        
    22.         try
    23.         {
    24.             StringBuilder sb = new StringBuilder();
    25.             string line = null;
    26.  
    27.             Boolean isEditor = name.Contains(".Editor");
    28.            
    29.             using (var sr = new StringReader(content))
    30.             {
    31.                 while ((line = sr.ReadLine()) != null)
    32.                 {
    33.                     if (line == "    <LangVersion>4</LangVersion>")
    34.                     {
    35.                         sb.AppendLine("    <LangVersion>6</LangVersion>");
    36.                     }
    37.                     else if (line == "    <LangVersion>6</LangVersion>")
    38.                     {
    39.                         sb.AppendLine("    <LangVersion>7.2</LangVersion>");
    40.                     }
    41.                     else if (line == "    <Reference Include=\"Boo.Lang\" />")
    42.                     {
    43.                     }
    44.                     else if (line == "    <Reference Include=\"UnityScript.Lang\" />")
    45.                     {
    46.                     }
    47.                     else if ( !String.IsNullOrEmpty(path) && line.StartsWith("    <None Include" ) )
    48.                     {
    49.  
    50.                     }
    51.                     else if (!isEditor)
    52.                     {
    53.                         if (line == "    <Reference Include=\"System.XML\" />")
    54.                         {
    55.                         }
    56.                         else if (line == "    <Reference Include=\"System.Xml.Linq\" />")
    57.                         {
    58.                         }
    59.                         else
    60.                         {
    61.                             sb.AppendLine(line);
    62.                         }
    63.                     }
    64.                     else
    65.                     {
    66.                         sb.AppendLine(line);
    67.                     }
    68.                 }
    69.             }
    70.  
    71.             return sb.ToString();
    72.         }
    73.         catch (Exception ex)
    74.         {
    75.             Debug.LogError        ( "[csProjFixer] - FixProj: Fix Fail for file: " + name );
    76.             Debug.LogException    ( ex );
    77.         }
    78.  
    79.         return content;
    80.     }
    81. }
     
    ZiadJ and z00n like this.
  7. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Why not; it's well documented input format for MSBuild. Not worse than editing make-files.
     
  8. lloydv

    lloydv

    Joined:
    Sep 15, 2015
    Posts:
    55
    Looking forward to c# 7 support so I can use binary literals. Keep up the good work!
     
    Qbit86 likes this.
  9. DavidSWu

    DavidSWu

    Joined:
    Jun 20, 2016
    Posts:
    183