Skip to content

Instantly share code, notes, and snippets.

@iletai
Created March 28, 2021 07:23
Show Gist options
  • Save iletai/d1828fcfe6023b21a4162e0ff08ff546 to your computer and use it in GitHub Desktop.
Save iletai/d1828fcfe6023b21a4162e0ff08ff546 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
using System;
public class WarningDisable : AssetPostprocessor
{
/// add more warning type to here. Some common warning
/// 0169 : var nerver use
/// 0649 : var nerver assign (will annoying with [SerializableField] vars)
/// Eg: warnings = "0169, 0649"
static readonly string warnings = "0649";
/// Secret method called by unity after it generates the solution
private static void OnGeneratedCSProjectFiles()
{
string currentDir = Directory.GetCurrentDirectory();
string[] csprojFiles = Directory.GetFiles(currentDir, "*.csproj");
for (int i = 0; i < csprojFiles.Length; i ++)
{
FixProject(csprojFiles[i]);
}
}
static bool FixProject(string filePath)
{
string content = File.ReadAllText(filePath);
// default .csproj is already disable 0169 warning
string searchString = "<NoWarn>0169</NoWarn>";
string replaceString = string.Format("<NoWarn>{0}</NoWarn>", warnings);
if(content.IndexOf(searchString) != -1)
{
content = Regex.Replace(content,searchString,replaceString);
File.WriteAllText(filePath,content);
return true;
}
else
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment