MonoBehaviourとの兼ね合いを考えるのが面倒になったので、asmdefでNoEngineReferencesにチェックが入ってるやつだけnull許容参照型を有効にするという妥協をしたバージョン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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