Skip to content

Instantly share code, notes, and snippets.

Last active May 3, 2021 16:27
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kyubuns/35a01920c8fd03474386f791bc18ee15 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using UnityEditor;
using UnityEditor.Compilation;
using UnityEditor.Experimental;
using UnityEditorInternal;
using UnityEngine;
public class EnableNullable : AssetPostprocessor
private static string OnGeneratedCSProject(string path, string content)
var assemblyName = Path.GetFileNameWithoutExtension(path);
var assemblyDefinitionFilePath = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName(assemblyName);
if (string.IsNullOrWhiteSpace(assemblyDefinitionFilePath)) return content;
var assemblyDefinition = EditorResources.Load<AssemblyDefinitionAsset>(assemblyDefinitionFilePath);
if (!assemblyDefinition.text.Contains("\"noEngineReferences\": true")) return content;
var document = XDocument.Parse(content);
if (document.Root == null) throw new Exception($"document.Root == null");
XNamespace xNamespace = "";
// ReSharper disable once PossibleNullReferenceException
var propertyGroup = document
.Element(xNamespace + "Project")
.Elements(xNamespace + "PropertyGroup")
propertyGroup.Add(new XElement(xNamespace + "Nullable", "enable"));
return document.ToString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment