Deozaan

TagsLayersBuilder.cs - Unity3D Editor Extension

Mar 20th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. // Tags And Layers Builder - Auto Generate Tags and Layers classes containing consts for all variables for code completion - 2012-08-03
  2. // released under MIT License
  3. // http://www.opensource.org/licenses/mit-license.php
  4. //
  5. //@author       Devin Reimer - AlmostLogical Software
  6. //@website      http://blog.almostlogical.com
  7. /*
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11. */
  12.  
  13. using UnityEngine;
  14. using UnityEditor;
  15. using System.Collections;
  16. using System.IO;
  17.  
  18. //Note: This class uses UnityEditorInternal which is an undocumented internal feature
  19. public class TagsLayersBuilder : EditorWindow {
  20.     private const string FOLDER_LOCATION = "Scripts/AutoGenerated/";
  21.     private const string TAGS_FILE_NAME = "Tags";
  22.     private const string LAYERS_FILE_NAME = "Layers";
  23.     private const string SCRIPT_EXTENSION = ".cs";
  24.  
  25.     [MenuItem("Edit/Rebuild Tags And Layers Classes")]
  26.     static void RebuildTagsAndLayersClasses() {
  27.         string folderPath = Application.dataPath + "/" + FOLDER_LOCATION;
  28.         if (!Directory.Exists(folderPath)) {
  29.             Directory.CreateDirectory(folderPath);
  30.         }
  31.         File.WriteAllText(folderPath + TAGS_FILE_NAME + SCRIPT_EXTENSION, GetClassContent(TAGS_FILE_NAME, UnityEditorInternal.InternalEditorUtility.tags));
  32.         File.WriteAllText(folderPath + LAYERS_FILE_NAME + SCRIPT_EXTENSION, GetClassContent(LAYERS_FILE_NAME, UnityEditorInternal.InternalEditorUtility.layers));
  33.         AssetDatabase.ImportAsset("Assets/" + FOLDER_LOCATION + TAGS_FILE_NAME + SCRIPT_EXTENSION, ImportAssetOptions.ForceUpdate);
  34.         AssetDatabase.ImportAsset("Assets/" + FOLDER_LOCATION + LAYERS_FILE_NAME + SCRIPT_EXTENSION, ImportAssetOptions.ForceUpdate);
  35.     }
  36.  
  37.     private static string GetClassContent(string className, string[] labelsArray) {
  38.         string output = "";
  39.         output += "//This class is auto-generated do not modify (TagsLayersBuilder.cs) - blog.almostlogical.com\n";
  40.         output += "public class " + className + "\n";
  41.         output += "{\n";
  42.         foreach (string label in labelsArray) {
  43.             output += "\t" + BuildConstVariable(label) + "\n";
  44.         }
  45.         output += "}";
  46.         return output;
  47.     }
  48.  
  49.     private static string BuildConstVariable(string varName) {
  50.         return "public const string " + ToUpperCaseWithUnderscores(varName) + " = " + '"' + varName + '"' + ";";
  51.     }
  52.  
  53.     private static string ToUpperCaseWithUnderscores(string input) {
  54.         string output = "" + input[0];
  55.  
  56.         for (int n = 1; n < input.Length; n++) {
  57.             if ((char.IsUpper(input[n]) || input[n] == ' ') && !char.IsUpper(input[n - 1]) && input[n - 1] != '_' && input[n - 1] != ' ') {
  58.                 output += "_";
  59.             }
  60.  
  61.             if (input[n] != ' ' && input[n] != '_') {
  62.                 output += input[n];
  63.             }
  64.         }
  65.  
  66.         output = output.ToUpper();
  67.         return output;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment