Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- // For Unity3D: code-generates an enum file based on current project Layers
- // by @kurtdekker
- //
- // Place in an Editor folder somewhere
- //
- public static class LayersToEnum
- {
- // Move this or rename it however you like, as long as it is a C# file.
- const string EnumSourceFile = "Assets/UnityLayer.cs";
- // Move this menu item wherever you prefer to have it.
- [MenuItem( "Assets/Regenerate Layer Enums")]
- static void GenerateCode()
- {
- string code = "";
- code += "\n";
- code += "// CAUTION: auto-generated by LayersToEnum.GenerateCode()\n";
- code += "// by @kurtdekker\n";
- code += "\n";
- code += "// To use these in your script, simply make a public variable of this type,\n";
- code += "// and then you can meaningfully select the layer as human-readable.\n";
- code += "\n";
- code += "// If you change layers, be sure to regenerate with this code.\n";
- code += "\n";
- code += "// NOTE: You SHOULD commit this file to source control.\n";
- code += "\n";
- code += "public enum UnityLayer\n";
- code += "{\n";
- code += "// If you get compiler errors, see bottom of generator script\n";
- code += "// for how to get rid of bad characters and layer names.\n";
- code += "\n";
- for (int i = 0; i < 32; i++)
- {
- var s = LayerMask.LayerToName(i);
- if (!string.IsNullOrEmpty(s))
- {
- Debug.Log( System.String.Format( "{0} - '{1}'", i, s));
- var safeName = MakeSafeName(s);
- code += "\t" + safeName + " = " + i.ToString() + ",\n";
- }
- }
- code += "}\n";
- Debug.Log( code);
- System.IO.File.WriteAllText( EnumSourceFile, code);
- AssetDatabase.Refresh();
- }
- static string MakeSafeName( string s)
- {
- s = s.Replace( ' ', '_');
- // put any other silly non-identifier-safe
- // characters you find in your layers here
- // and make them safe for identifiers, and
- // don't do that anymore please. No spaces!
- // Also if you name one of your layers as a
- // C# keyword, that too will need to be
- // replaced here or else you'll get errors.
- return s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement