Last active
July 13, 2020 11:14
-
-
Save korchoon/71fdbaca4869e0412c8afb92e6cfb5dc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#if UNITY_EDITOR | |
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
using System.Text; | |
public class LayerGenerator : EditorWindow { | |
[MenuItem("Edit/Rebuild LAYERS.cs %&L")] | |
static void RebuildTagsAndLayersEnums() { | |
var enumsPath = "Assets/Scripts/"; | |
RebuildLayersFile($"{enumsPath}LAYERS.cs"); | |
AssetDatabase.ImportAsset($"{enumsPath}LAYERS.cs", ImportAssetOptions.ForceUpdate); | |
} | |
static void rebuildTagsFile(string filePath) { | |
var sb = new StringBuilder(); | |
sb.Append("//This class is auto-generated, do not modify (TagsLayersEnumBuilder.cs)\n"); | |
sb.Append("public static class Tags {\n"); | |
var srcArr = UnityEditorInternal.InternalEditorUtility.tags; | |
var tags = new String[srcArr.Length]; | |
Array.Copy(srcArr, tags, tags.Length); | |
Array.Sort(tags, StringComparer.InvariantCultureIgnoreCase); | |
for (int i = 0, n = tags.Length; i < n; ++i) { | |
var tagName = tags[i]; | |
sb.Append($"\tpublic const string {Process($"{tagName} = \"{tagName}\";\n")}"); | |
} | |
sb.Append("}\n"); | |
File.WriteAllText(filePath, sb.ToString()); | |
} | |
static void RebuildLayersFile(string filePath) { | |
var sb = new StringBuilder(); | |
sb.Append("//This class is auto-generated, do not modify (use Tools/TagsLayersEnumBuilder)\n"); | |
sb.Append("public static class LAYERS {\n"); | |
var layers = UnityEditorInternal.InternalEditorUtility.layers; | |
for (int i = 0, n = layers.Length; i < n; ++i) { | |
var layerName = layers[i]; | |
sb.Append($"\tpublic const string {GetVariableName(layerName)} = \"{layerName}\";\n"); | |
} | |
sb.Append("\n"); | |
for (int i = 0, n = layers.Length; i < n; ++i) { | |
var layerName = layers[i]; | |
var layerNumber = LayerMask.NameToLayer(layerName); | |
var layerMask = layerNumber == 0 ? "1" : ("1 << " + layerNumber); | |
sb.Append($"\tpublic const int {GetVariableName(layerName)}_Mask = {layerMask};\n"); | |
} | |
sb.Append("\n"); | |
for (int i = 0, n = layers.Length; i < n; ++i) { | |
var layerName = layers[i]; | |
var layerNumber = LayerMask.NameToLayer(layerName); | |
sb.Append($"\tpublic const int {GetVariableName(layerName)}_Number = {layerNumber};\n"); | |
} | |
sb.Append("}\n"); | |
File.WriteAllText(filePath, sb.ToString()); | |
} | |
static string GetVariableName(string str) { | |
return Process(str); | |
} | |
static string Process(string res) { | |
res = res.Replace(" ", ""); | |
if (res[0] >= '0' && res[0] <= '9') | |
res = $"_{res}"; | |
return res; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment