Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing .csproj in Unity enable nullable reference types

Tags:

c#

unity3d

Unity3D's 2020.2 release is now supporting C# 8 and nullable reference types. The default way to opt in to this language feature is to put <Nullable>enable</Nullable> in your .csproj file, but Unity regenerates .csproj and removes this line every time it compiles.

Is there a way to insert custom content into my .csproj, or do I need to fall back to the tedious process of starting every file with #nullable enable?

like image 421
Derek Thurn Avatar asked Oct 06 '20 15:10

Derek Thurn


1 Answers

I did the configuration that @Fadeway suggested and it works for building within Unity itself (2020.3.15f2). But it does not work when using outside editors (VSCode in my case).

However, by adding a new file named Directory.Build.props along with you .sln project file, with the following contents:

<Project>
  <PropertyGroup>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

It works as expected in the editor and does not get overwritten when regenerating the .csproj files from within Unity

like image 194
Corintho Avatar answered Sep 25 '22 06:09

Corintho