private static IEnumerable<string> GetAllLayers() { return InternalEditorUtility.layers; }
private static CodeMemberField GenerateConstant(string name) { name = name.Replace(" ", ""); var @const = new CodeMemberField( typeof(string), name); @const.Attributes &= ~MemberAttributes.AccessMask; @const.Attributes &= ~MemberAttributes.ScopeMask; @const.Attributes |= MemberAttributes.Public; @const.Attributes |= MemberAttributes.Const; @const.InitExpression = new CodePrimitiveExpression(name); return @const; }
private static void ImitateStaticClass(CodeTypeDeclaration type) { @type.TypeAttributes |= TypeAttributes.Sealed; @type.Members.Add(new CodeConstructor { Attributes = MemberAttributes.Private | MemberAttributes.Final }); }
private static CodeCompileUnit GenerateClassWithConstants( string name, IEnumerable<string> constants) { var compileUnit = new CodeCompileUnit(); var @namespace = new CodeNamespace(); var @class = new CodeTypeDeclaration(name); ImitateStaticClass(@class); foreach (var constantName in constants) { var @const = GenerateConstant(constantName); @class.Members.Add(@const); } @namespace.Types.Add(@class); compileUnit.Namespaces.Add(@namespace); return compileUnit; }
private static void WriteIntoFile(string fullPath, CodeCompileUnit code) { Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); using (var stream = new StreamWriter(fullPath, append: false)) { var writer = new IndentedTextWriter(stream); using (var codeProvider = new CSharpCodeProvider()) { codeProvider.GenerateCodeFromCompileUnit(code, writer, new CodeGeneratorOptions()); } } }
[MenuItem("Habr/Generate layers constants")] private static void GenerateAndForceImport() { const string path = @"Auto/Layers.cs"; var fullPath = Path.Combine(Application.dataPath, path); var className = Path.GetFileNameWithoutExtension(fullPath); var code = GenerateClassWithConstants(className, GetAllLayers()); WriteIntoFile(fullPath, code); AssetDatabase.ImportAsset("Assets/" + path, ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(); }
namespace Habr { using Microsoft.CSharp; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; using System.Reflection; using UnityEditor; using UnityEditorInternal; using UnityEngine; internal static class HabrCodeGen { [MenuItem("Habr/Generate layers constants")] private static void GenerateAndForceImport() { const string path = @"Auto/Layers.cs"; var fullPath = Path.Combine(Application.dataPath, path); var className = Path.GetFileNameWithoutExtension(fullPath); var code = GenerateClassWithConstants(className, GetAllLayers()); WriteIntoFile(fullPath, code); AssetDatabase.ImportAsset("Assets/" + path, ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(); } private static CodeCompileUnit GenerateClassWithConstants( string name, IEnumerable<string> constants) { var compileUnit = new CodeCompileUnit(); var @namespace = new CodeNamespace(); var @class = new CodeTypeDeclaration(name); ImitateStaticClass(@class); foreach (var constantName in constants) { var @const = GenerateConstant(constantName); @class.Members.Add(@const); } @namespace.Types.Add(@class); compileUnit.Namespaces.Add(@namespace); return compileUnit; } private static CodeMemberField GenerateConstant(string name) { name = name.Replace(" ", ""); var @const = new CodeMemberField( typeof(string), name); @const.Attributes &= ~MemberAttributes.AccessMask; @const.Attributes &= ~MemberAttributes.ScopeMask; @const.Attributes |= MemberAttributes.Public; @const.Attributes |= MemberAttributes.Const; @const.InitExpression = new CodePrimitiveExpression(name); return @const; } private static IEnumerable<string> GetAllLayers() { return InternalEditorUtility.layers; } private static void ImitateStaticClass(CodeTypeDeclaration type) { @type.TypeAttributes |= TypeAttributes.Sealed; @type.Members.Add(new CodeConstructor { Attributes = MemberAttributes.Private | MemberAttributes.Final }); } private static void WriteIntoFile(string fullPath, CodeCompileUnit code) { Directory.CreateDirectory(Path.GetDirectoryName(fullPath)); using (var stream = new StreamWriter(fullPath, append: false)) { var tw = new IndentedTextWriter(stream); using (var codeProvider = new CSharpCodeProvider()) { codeProvider.GenerateCodeFromCompileUnit(code, tw, new CodeGeneratorOptions()); } } } } }
// ------------------------------------------------------------------------------ // <autogenerated> // This code was generated by a tool. // Mono Runtime Version: 2.0.50727.1433 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </autogenerated> // ------------------------------------------------------------------------------ public sealed class Layers { public const string Default = "Default"; public const string TransparentFX = "TransparentFX"; public const string IgnoreRaycast = "IgnoreRaycast"; public const string Water = "Water"; public const string UI = "UI"; public const string Habr = "Habr"; private Layers() { } }
Source: https://habr.com/ru/post/309128/
All Articles