├── Editor ├── TagGenerator.cs.meta ├── Utilities.cs.meta ├── ICodeGenerator.cs.meta ├── LayerGenerator.cs.meta ├── ICodeGenerator.cs ├── TNRD.LayerTagsGenerator.Editor.asmdef.meta ├── TNRD.LayerTagsGenerator.Editor.asmdef ├── TagGenerator.cs ├── LayerGenerator.cs └── Utilities.cs ├── Documentation~ ├── menu_items.png └── generated_files.png ├── CHANGELOG.md.meta ├── LICENSE.md.meta ├── README.md.meta ├── package.json.meta ├── Editor.meta ├── CHANGELOG.md ├── package.json ├── .github └── FUNDING.yml ├── LICENSE.md └── README.md /Editor/TagGenerator.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c31099fa62b24989953dd316008a18de 3 | timeCreated: 1594471242 -------------------------------------------------------------------------------- /Editor/Utilities.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f67ca0b8ac2647e389e0f2c55121ec58 3 | timeCreated: 1594471251 -------------------------------------------------------------------------------- /Editor/ICodeGenerator.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f865e04d31f94d31860e50bb03046a79 3 | timeCreated: 1594471516 -------------------------------------------------------------------------------- /Editor/LayerGenerator.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 318ca1054c6842b7bc30ba49355153e2 3 | timeCreated: 1594471238 -------------------------------------------------------------------------------- /Documentation~/menu_items.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thundernerd/Unity3D-LayersTagsGenerator/HEAD/Documentation~/menu_items.png -------------------------------------------------------------------------------- /Documentation~/generated_files.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thundernerd/Unity3D-LayersTagsGenerator/HEAD/Documentation~/generated_files.png -------------------------------------------------------------------------------- /Editor/ICodeGenerator.cs: -------------------------------------------------------------------------------- 1 | namespace TNRD.CodeGeneration 2 | { 3 | public interface ICodeGenerator 4 | { 5 | void Generate(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /CHANGELOG.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e52485ef875aee4a9aff3f6e99f68bf 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /LICENSE.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0f2e526334ef59d4ea2343b28694ef42 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /README.md.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4b9f9a38f0f2d164893b437b22669b20 3 | TextScriptImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /package.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7791e0110fd0f2049a93f36de8ca3eb6 3 | PackageManifestImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6d5e5b1775c566948ba40e1b5518f114 3 | folderAsset: yes 4 | DefaultImporter: 5 | externalObjects: {} 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Editor/TNRD.LayerTagsGenerator.Editor.asmdef.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 586fb755b756bee40a4741b3342d2a5b 3 | AssemblyDefinitionImporter: 4 | externalObjects: {} 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [2.0.0] - 2021-02-16 4 | ### Changed 5 | - Code generation happens through CodeDom instead of custom generation 6 | 7 | ### Removed 8 | - Sorting Layer generator 9 | 10 | ## [1.0.2] - 2020-07-14 11 | ### Fixed 12 | - Missing meta file 13 | 14 | ## [1.0.1] - 2020-07-14 15 | ### Fixed 16 | - Dependency 17 | 18 | ## [1.0.0] - 2020-07-11 19 | ### Added 20 | - Layers Generator 21 | - Sorting Layers Generator 22 | - Tags Generator 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "net.tnrd.layertagsgenerator", 3 | "displayName": "Layer Tags Generator", 4 | "version": "2.0.0", 5 | "unity": "2019.1", 6 | "description": "A simple tool to generate files that contain the project's Layers and Tags.", 7 | "keywords": [ 8 | "layer", 9 | "layers", 10 | "tag", 11 | "tags", 12 | "generator" 13 | ], 14 | "author": { 15 | "name": "TNRD", 16 | "email": "unity3d@tnrd.net", 17 | "url": "https://www.tnrd.net" 18 | } 19 | } -------------------------------------------------------------------------------- /Editor/TNRD.LayerTagsGenerator.Editor.asmdef: -------------------------------------------------------------------------------- 1 | { 2 | "name": "TNRD.LayerTagsGenerator.Editor", 3 | "references": [ 4 | "TNRD.CodeGenerator.Editor" 5 | ], 6 | "includePlatforms": [ 7 | "Editor" 8 | ], 9 | "excludePlatforms": [], 10 | "allowUnsafeCode": false, 11 | "overrideReferences": false, 12 | "precompiledReferences": [], 13 | "autoReferenced": true, 14 | "defineConstraints": [], 15 | "versionDefines": [], 16 | "noEngineReferences": false 17 | } -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: thundernerd 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Christiaan Bloemendaal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Editor/TagGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.CodeDom; 2 | using System.Linq; 3 | using System.Reflection; 4 | using UnityEditor; 5 | using UnityEditorInternal; 6 | using UnityEngine; 7 | 8 | namespace TNRD.CodeGeneration.Tags 9 | { 10 | public class TagGenerator : ICodeGenerator 11 | { 12 | [MenuItem("TNRD/Code Generation/Tags")] 13 | private static void Execute() 14 | { 15 | TagGenerator generator = new TagGenerator(); 16 | generator.Generate(); 17 | } 18 | 19 | public void Generate() 20 | { 21 | string[] tags = InternalEditorUtility.tags 22 | .OrderBy(x => x) 23 | .ToArray(); 24 | 25 | CodeCompileUnit compileUnit = new CodeCompileUnit(); 26 | CodeNamespace codeNamespace = new CodeNamespace(); 27 | CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("Tags") 28 | { 29 | IsClass = true, 30 | TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed 31 | }; 32 | 33 | foreach (string tag in tags) 34 | { 35 | CodeMemberField field = new CodeMemberField 36 | { 37 | Attributes = MemberAttributes.Public | MemberAttributes.Const, 38 | Name = Utilities.GetScreamName(tag), 39 | Type = new CodeTypeReference(typeof(string)), 40 | InitExpression = new CodePrimitiveExpression(tag) 41 | }; 42 | classDeclaration.Members.Add(field); 43 | } 44 | 45 | codeNamespace.Types.Add(classDeclaration); 46 | compileUnit.Namespaces.Add(codeNamespace); 47 | 48 | Utilities.GenerateToFile(compileUnit, Application.dataPath + "/Generated", "Tags.cs"); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Editor/LayerGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.CodeDom; 2 | using System.Linq; 3 | using System.Reflection; 4 | using UnityEditor; 5 | using UnityEditorInternal; 6 | using UnityEngine; 7 | 8 | namespace TNRD.CodeGeneration.Layers 9 | { 10 | public class LayerGenerator : ICodeGenerator 11 | { 12 | [MenuItem("TNRD/Code Generation/Layers")] 13 | private static void Execute() 14 | { 15 | LayerGenerator generator = new LayerGenerator(); 16 | generator.Generate(); 17 | } 18 | 19 | public void Generate() 20 | { 21 | string[] layers = InternalEditorUtility.layers 22 | .OrderBy(x => x) 23 | .ToArray(); 24 | 25 | CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); 26 | CodeNamespace codeNamespace = new CodeNamespace(); 27 | CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("Layers") 28 | { 29 | IsClass = true, 30 | TypeAttributes = TypeAttributes.Public | TypeAttributes.Sealed 31 | }; 32 | 33 | for (int i = 0; i < layers.Length; i++) 34 | { 35 | string layer = layers[i]; 36 | string layerName = Utilities.GetScreamName(layer); 37 | string maskName = layerName + "_MASK"; 38 | int layerValue = LayerMask.NameToLayer(layer); 39 | 40 | CodeMemberField layerField = new CodeMemberField(typeof(int), layerName) 41 | { 42 | Attributes = MemberAttributes.Public | MemberAttributes.Const, 43 | InitExpression = new CodePrimitiveExpression(layerValue) 44 | }; 45 | 46 | CodeMemberField maskField = new CodeMemberField(typeof(int), maskName) 47 | { 48 | Attributes = MemberAttributes.Public | MemberAttributes.Const, 49 | InitExpression = new CodePrimitiveExpression(1 << layerValue) 50 | }; 51 | 52 | classDeclaration.Members.Add(layerField); 53 | classDeclaration.Members.Add(maskField); 54 | } 55 | 56 | codeNamespace.Types.Add(classDeclaration); 57 | codeCompileUnit.Namespaces.Add(codeNamespace); 58 | 59 | Utilities.GenerateToFile(codeCompileUnit, Application.dataPath + "/Generated", "Layers.cs"); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Layers & Tags Generator 2 | 3 |

4 | GitHub package.json version 5 | 6 | GitHub issues 7 | 8 | 9 | GitHub pull requests 10 | 11 | 12 | GitHub license 13 | 14 | GitHub last commit 15 |

16 | 17 | A package that contains generators that will generate classes containing the project's Layers and Tags for easy access. 18 | 19 | ## Installation 20 | 1. The package is available on the [openupm registry](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli). 21 | ``` 22 | openupm add net.tnrd.layertagsgenerator 23 | ``` 24 | 2. Installing through a [Unity Package](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.layertagsgenerator?registry=https://package.openupm.com) created by the [Package Installer Creator](https://package-installer.glitch.me) from [Needle](https://needle.tools) 25 | 26 | [](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.layertagsgenerator?registry=https://package.openupm.com) 27 | 28 | ## Usage 29 | 30 | Once you have installed the package into your project you can access the generators through the menu as shown below. 31 | 32 | 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. 33 | 34 | 35 | ![alt](./Documentation~/menu_items.png) 36 | 37 | 38 | 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. 39 | 40 | ![alt](./Documentation~/generated_files.png) 41 | 42 | To use the generated files you simply access them through their classes which are: Tags, Layers, and SortingLayers. 43 | Below is an example of a use case for the Tags. 44 | 45 | ```c# 46 | public class Foo : MonoBehaviour 47 | { 48 | private void OnCollisionEnter(Collision other) 49 | { 50 | if (other.transform.CompareTag(Tags.PLAYER)) 51 | { 52 | ... 53 | } 54 | } 55 | } 56 | ``` 57 | 58 | ## Support 59 | Layers & Tags Generator is an open-source project that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating. 60 | 61 | [![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY) 62 | 63 | ## Contributions 64 | Pull requests are welcomed. Please feel free to fix any issues you find, or add new features. 65 | 66 | -------------------------------------------------------------------------------- /Editor/Utilities.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.CodeDom; 3 | using System.CodeDom.Compiler; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Reflection; 8 | using Microsoft.CSharp; 9 | using UnityEditor; 10 | 11 | namespace TNRD.CodeGeneration 12 | { 13 | public static class Utilities 14 | { 15 | [MenuItem("TNRD/Code Generation/Generate All", false, int.MaxValue)] 16 | private static void GenerateAll() 17 | { 18 | Type typeDefinition = typeof(ICodeGenerator); 19 | 20 | Assembly assembly = Assembly.GetExecutingAssembly(); 21 | List types = assembly.GetTypes() 22 | .Where(x => x.GetInterfaces().Contains(typeDefinition)) 23 | .ToList(); 24 | 25 | for (int i = 0; i < types.Count; i++) 26 | { 27 | Type type = types[i]; 28 | if (type.IsAbstract) 29 | continue; 30 | 31 | ICodeGenerator instance = (ICodeGenerator) Activator.CreateInstance(type); 32 | instance.Generate(); 33 | } 34 | } 35 | 36 | public static string GetScreamName(string name) 37 | { 38 | string formattedName = ""; 39 | 40 | name = FilterSpaces(name); 41 | 42 | for (int i = 0; i < name.Length; i++) 43 | { 44 | if (i == 0) 45 | { 46 | formattedName += name[i].ToString(); 47 | continue; 48 | } 49 | 50 | char c = name[i]; 51 | char pc = name[i - 1]; 52 | if (char.IsUpper(c) && char.IsLower(pc)) 53 | formattedName += "_"; 54 | 55 | formattedName += c.ToString(); 56 | } 57 | 58 | return formattedName.ToUpper(); 59 | } 60 | 61 | private static string FilterSpaces(string name) 62 | { 63 | int index = -1; 64 | 65 | while ((index = name.IndexOf(' ')) != -1) 66 | { 67 | if (index == name.Length - 1) 68 | { 69 | name = name.Remove(index, 1); 70 | return name; 71 | } 72 | 73 | string upperChar = char.ToUpper(name[index + 1]).ToString(); 74 | name = name.Remove(index, 2); 75 | name = name.Insert(index, upperChar); 76 | } 77 | 78 | return name; 79 | } 80 | 81 | public static void GenerateToFile(CodeCompileUnit unit, string directory, string filename) 82 | { 83 | CSharpCodeProvider codeProvider = new CSharpCodeProvider(); 84 | CodeGeneratorOptions options = new CodeGeneratorOptions 85 | { 86 | BracingStyle = "C" 87 | }; 88 | 89 | StringWriter writer = new StringWriter(); 90 | codeProvider.GenerateCodeFromCompileUnit(unit, writer, options); 91 | writer.Flush(); 92 | string output = writer.ToString(); 93 | 94 | string directoryPath = directory; 95 | string filePath = directoryPath + "/" + filename; 96 | if (!Directory.Exists(directoryPath)) 97 | Directory.CreateDirectory(directoryPath); 98 | 99 | File.WriteAllText(filePath, output); 100 | AssetDatabase.Refresh(); 101 | } 102 | } 103 | } 104 | --------------------------------------------------------------------------------