Skip to content

Commit

Permalink
Merge pull request #6 from Thundernerd/develop
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
Thundernerd committed Feb 17, 2021
2 parents c8fc448 + c54ea9f commit 3639785
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 327 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# Changelog

## [1.0.2] - 2020-07-14
## [2.0.0] - 2021-02-16
### Changed
- Code generation happens through CodeDom instead of custom generation

### Removed
- Sorting Layer generator

## [1.0.2] - 2020-07-14
### Fixed
- Missing meta file

## [1.0.1] - 2020-07-14

### Fixed
- Dependency

## [1.0.0] - 2020-07-11

### Added
- Layers Generator
- Sorting Layers Generator
Expand Down
File renamed without changes
File renamed without changes
59 changes: 37 additions & 22 deletions Editor/LayerGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using UnityEditor;
using System.CodeDom;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

Expand All @@ -9,39 +12,51 @@ public class LayerGenerator : ICodeGenerator
[MenuItem("TNRD/Code Generation/Layers")]
private static void Execute()
{
var generator = new LayerGenerator();
LayerGenerator generator = new LayerGenerator();
generator.Generate();
}

public void Generate()
{
var layers = InternalEditorUtility.layers;
string[] layers = InternalEditorUtility.layers
.OrderBy(x => x)
.ToArray();

var generator = new Generator();
var @class = new Class("Layers");
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace();
CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("Layers")
{
IsClass = true,
TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed
};

for (int i = 0; i < layers.Length; i++)
{
var layerName = Utilities.GetScreamName(layers[i]);
var maskName = layerName + "_MASK";

@class.AddField(
new Field(layerName, i, typeof(int))
{
IsConst = true,
});

@class.AddField(
new Field(maskName, string.Format("1 << {0}", i), typeof(int))
{
IsConst = true,
});
string layer = layers[i];
string layerName = Utilities.GetScreamName(layer);
string maskName = layerName + "_MASK";
int layerValue = LayerMask.NameToLayer(layer);

CodeMemberField layerField = new CodeMemberField(typeof(int), layerName)
{
Attributes = MemberAttributes.Public | MemberAttributes.Const,
InitExpression = new CodePrimitiveExpression(layerValue)
};

CodeMemberField maskField = new CodeMemberField(typeof(int), maskName)
{
Attributes = MemberAttributes.Public | MemberAttributes.Const,
InitExpression = new CodePrimitiveExpression(1 << layerValue)
};

classDeclaration.Members.Add(layerField);
classDeclaration.Members.Add(maskField);
}

generator.AddClass(@class);
generator.SaveToFile(Application.dataPath + "/Generated/Layers.cs");
codeNamespace.Types.Add(classDeclaration);
codeCompileUnit.Namespaces.Add(codeNamespace);

AssetDatabase.Refresh();
Utilities.GenerateToFile(codeCompileUnit, Application.dataPath + "/Generated", "Layers.cs");
}
}
}
47 changes: 0 additions & 47 deletions Editor/SortingLayerGenerator.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/SortingLayerGenerator.cs.meta

This file was deleted.

38 changes: 0 additions & 38 deletions Editor/TagCodeGenerator.cs

This file was deleted.

51 changes: 51 additions & 0 deletions Editor/TagGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.CodeDom;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace TNRD.CodeGeneration.Tags
{
public class TagGenerator : ICodeGenerator
{
[MenuItem("TNRD/Code Generation/Tags")]
private static void Execute()
{
TagGenerator generator = new TagGenerator();
generator.Generate();
}

public void Generate()
{
string[] tags = InternalEditorUtility.tags
.OrderBy(x => x)
.ToArray();

CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace codeNamespace = new CodeNamespace();
CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("Tags")
{
IsClass = true,
TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed
};

foreach (string tag in tags)
{
CodeMemberField field = new CodeMemberField
{
Attributes = MemberAttributes.Public | MemberAttributes.Const,
Name = Utilities.GetScreamName(tag),
Type = new CodeTypeReference(typeof(string)),
InitExpression = new CodePrimitiveExpression(tag)
};
classDeclaration.Members.Add(field);
}

codeNamespace.Types.Add(classDeclaration);
compileUnit.Namespaces.Add(codeNamespace);

Utilities.GenerateToFile(compileUnit, Application.dataPath + "/Generated", "Tags.cs");
}
}
}
File renamed without changes.
53 changes: 40 additions & 13 deletions Editor/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.CSharp;
using UnityEditor;

namespace TNRD.CodeGeneration
{
public static class Utilities
{
[MenuItem("TNRD/Code Generation/Generate All", false, Int32.MaxValue)]
[MenuItem("TNRD/Code Generation/Generate All", false, int.MaxValue)]
private static void GenerateAll()
{
var typeDefinition = typeof(ICodeGenerator);
var assembly = Assembly.GetExecutingAssembly();
var types = assembly.GetTypes()
Type typeDefinition = typeof(ICodeGenerator);

Assembly assembly = Assembly.GetExecutingAssembly();
List<Type> types = assembly.GetTypes()
.Where(x => x.GetInterfaces().Contains(typeDefinition))
.ToList();

for (int i = 0; i < types.Count; i++)
{
var type = types[i];
Type type = types[i];
if (type.IsAbstract)
continue;

var instance = (ICodeGenerator) Activator.CreateInstance(type);
ICodeGenerator instance = (ICodeGenerator) Activator.CreateInstance(type);
instance.Generate();
}
}

public static string GetScreamName(string name)
{
var formattedName = "";
string formattedName = "";

name = FilterSpaces(name);

Expand All @@ -42,8 +47,8 @@ public static string GetScreamName(string name)
continue;
}

var c = name[i];
var pc = name[i - 1];
char c = name[i];
char pc = name[i - 1];
if (char.IsUpper(c) && char.IsLower(pc))
formattedName += "_";

Expand All @@ -55,7 +60,7 @@ public static string GetScreamName(string name)

private static string FilterSpaces(string name)
{
var index = -1;
int index = -1;

while ((index = name.IndexOf(' ')) != -1)
{
Expand All @@ -65,12 +70,34 @@ private static string FilterSpaces(string name)
return name;
}

var upperChar = char.ToUpper(name[index + 1]).ToString();
string upperChar = char.ToUpper(name[index + 1]).ToString();
name = name.Remove(index, 2);
name = name.Insert(index, upperChar);
}

return name;
}

public static void GenerateToFile(CodeCompileUnit unit, string directory, string filename)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions
{
BracingStyle = "C"
};

StringWriter writer = new StringWriter();
codeProvider.GenerateCodeFromCompileUnit(unit, writer, options);
writer.Flush();
string output = writer.ToString();

string directoryPath = directory;
string filePath = directoryPath + "/" + filename;
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);

File.WriteAllText(filePath, output);
AssetDatabase.Refresh();
}
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ Once you have installed the package into your project you can access the generat
You can generate the Tags, Layers, and Sorting Layers all separately by using their respective menu items, or generate them all by using the Generate All menu item.


![alt](./~Documentation/menu_items.png)
![alt](./Documentation~/menu_items.png)


Once you have generated one or more through the menu, you will see the files appear in your project located int he Generated folder, which resides at top level in your Assets folder.

![alt](./~Documentation/generated_files.png)
![alt](./Documentation~/generated_files.png)

To use the generated files you simply access them through their classes which are: Tags, Layers, and SortingLayers.
Below is an example of a use case for the Tags.
Expand Down
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "net.tnrd.layertagsgenerator",
"displayName": "Layer Tags Generator",
"version": "1.0.2",
"version": "2.0.0",
"unity": "2019.1",
"description": "A simple tool to generate files that contain the project's Layers, Sorting Layers, and Tags.",
"description": "A simple tool to generate files that contain the project's Layers and Tags.",
"keywords": [
"layer",
"layers",
Expand All @@ -12,11 +12,8 @@
"generator"
],
"author": {
"name": "Christiaan Bloemendaal",
"name": "TNRD",
"email": "unity3d@tnrd.net",
"url": "https://www.tnrd.net"
},
"dependencies": {
"net.tnrd.codegenerator": "1.0.0"
}
}
}

0 comments on commit 3639785

Please sign in to comment.