Skip to content

Instantly share code, notes, and snippets.

@kyubuns
Last active May 3, 2021 16:27
Embed
What would you like to do?
MonoBehaviourとの兼ね合いを考えるのが面倒になったので、asmdefでNoEngineReferencesにチェックが入ってるやつだけnull許容参照型を有効にするという妥協をしたバージョン
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 = "http://schemas.microsoft.com/developer/msbuild/2003";
// ReSharper disable once PossibleNullReferenceException
var propertyGroup = document
.Element(xNamespace + "Project")
.Elements(xNamespace + "PropertyGroup")
.First();
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