Skip to content

Instantly share code, notes, and snippets.

@KanaHayama
Last active May 20, 2025 07:47
Show Gist options
  • Save KanaHayama/93e356bd92b2fc963426bd2d7bd356f8 to your computer and use it in GitHub Desktop.
Save KanaHayama/93e356bd92b2fc963426bd2d7bd356f8 to your computer and use it in GitHub Desktop.
Enable latest C# language version for Unity

Introduction

Follow the following steps to enable the latest C# syntax for your Unity project.

Only tested with Scripting Backend set to Mono and Api Compatibility Level set to .NET Standard 2.1.

Step 1: For Editor Player

In Edit > Project Settings... > Player > Other Settings > Additional Compiler Arguments, add /langversion:latest, then Apply.

Step 2: For Generated VS Projects

Put LangVersionPostprocessor.cs under the <project_root>/Assets/Standard Assets/Editor folder.

You may change its filename and classname.

using System.Text.RegularExpressions;
using UnityEditor;
/// <remarks>
/// This class need to be placed in Assets/Standard Assets/Editor folder to be compiled before other scripts.
/// See <see href="https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html"/>.
/// </remarks>
internal sealed class LangVersionPostprocessor : AssetPostprocessor {
private static string OnGeneratedCSProject(string path, string content) {
var pattern = @"<LangVersion>(.*?)<\/LangVersion>";
var replacement = "<LangVersion>latest</LangVersion>";
content = Regex.Replace(content, pattern, replacement);
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment