Advertisement
Guest User

Unity Layer To Enums

a guest
May 6th, 2021
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. // For Unity3D: code-generates an enum file based on current project Layers
  5. // by @kurtdekker
  6. //
  7. // Place in an Editor folder somewhere
  8. //
  9.  
  10. public static class LayersToEnum
  11. {
  12.     // Move this or rename it however you like, as long as it is a C# file.
  13.     const string EnumSourceFile = "Assets/UnityLayer.cs";
  14.  
  15.     // Move this menu item wherever you prefer to have it.
  16.     [MenuItem( "Assets/Regenerate Layer Enums")]
  17.     static void GenerateCode()
  18.     {
  19.         string code = "";
  20.  
  21.         code += "\n";
  22.  
  23.         code += "// CAUTION: auto-generated by LayersToEnum.GenerateCode()\n";
  24.         code += "// by @kurtdekker\n";
  25.  
  26.         code += "\n";
  27.  
  28.         code += "// To use these in your script, simply make a public variable of this type,\n";
  29.         code += "// and then you can meaningfully select the layer as human-readable.\n";
  30.  
  31.         code += "\n";
  32.  
  33.         code += "// If you change layers, be sure to regenerate with this code.\n";
  34.  
  35.         code += "\n";
  36.  
  37.         code += "// NOTE: You SHOULD commit this file to source control.\n";
  38.  
  39.         code += "\n";
  40.  
  41.         code += "public enum UnityLayer\n";
  42.  
  43.         code += "{\n";
  44.  
  45.         code += "// If you get compiler errors, see bottom of generator script\n";
  46.         code += "// for how to get rid of bad characters and layer names.\n";
  47.         code += "\n";
  48.  
  49.         for (int i = 0; i < 32; i++)
  50.         {
  51.             var s = LayerMask.LayerToName(i);
  52.             if (!string.IsNullOrEmpty(s))
  53.             {
  54.                 Debug.Log( System.String.Format( "{0} - '{1}'", i, s));
  55.  
  56.                 var safeName = MakeSafeName(s);
  57.  
  58.                 code += "\t" + safeName + " = " + i.ToString() + ",\n";
  59.             }
  60.         }
  61.  
  62.         code += "}\n";
  63.  
  64.         Debug.Log( code);
  65.  
  66.         System.IO.File.WriteAllText( EnumSourceFile, code);
  67.  
  68.         AssetDatabase.Refresh();
  69.     }
  70.  
  71.     static string MakeSafeName( string s)
  72.     {
  73.         s = s.Replace( ' ', '_');
  74.  
  75.         // put any other silly non-identifier-safe
  76.         // characters you find in your layers here
  77.         // and make them safe for identifiers, and
  78.         // don't do that anymore please. No spaces!
  79.  
  80.         // Also if you name one of your layers as a
  81.         // C# keyword, that too will need to be
  82.         // replaced here or else you'll get errors.
  83.  
  84.         return s;
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement