Flynny85

Unity Constants generator

Aug 5th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.60 KB | None | 0 0
  1. using System.Text.RegularExpressions;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.Animations;
  5. using System.IO;
  6. using System;
  7. using System.Collections.Generic;
  8.  
  9. namespace UnityToolbag
  10. {
  11.     public static class UnityConstantsGenerator
  12.     {
  13.         [MenuItem("Edit/Generate UnityConstants.cs")]
  14.         public static void Generate()
  15.         {
  16.             // Try to find an existing file in the project called "UnityConstants.cs"
  17.             string filePath = string.Empty;
  18.             foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories)) {
  19.                 if (Path.GetFileNameWithoutExtension(file) == "UnityConstants") {
  20.                     filePath = file;
  21.                     break;
  22.                 }
  23.             }
  24.  
  25.             // If no such file exists already, use the save panel to get a folder in which the file will be placed.
  26.             if (string.IsNullOrEmpty(filePath)) {
  27.                 string directory = EditorUtility.OpenFolderPanel("Choose location for UnityConstants.cs", Application.dataPath, "");
  28.  
  29.                 // Canceled choose? Do nothing.
  30.                 if (string.IsNullOrEmpty(directory)) {
  31.                     return;
  32.                 }
  33.  
  34.                 filePath = Path.Combine(directory, "UnityConstants.cs");
  35.             }
  36.  
  37.             // Write out our file
  38.             using (var writer = new StreamWriter(filePath)) {
  39.                 writer.WriteLine("// This file is auto-generated. Modifications are not saved.");
  40.                 writer.WriteLine();
  41.                 writer.WriteLine("namespace UnityConstants");
  42.                 writer.WriteLine("{");
  43.  
  44.                 // Write out the tags
  45.                 writer.WriteLine("    public static class Tags");
  46.                 writer.WriteLine("    {");
  47.                 foreach (var tag in UnityEditorInternal.InternalEditorUtility.tags) {
  48.                     writer.WriteLine("        /// <summary>");
  49.                     writer.WriteLine("        /// Name of tag '{0}'.", tag);
  50.                     writer.WriteLine("        /// </summary>");
  51.                     writer.WriteLine("        public const string {0} = \"{1}\";", MakeSafeForCode(tag), tag);
  52.                 }
  53.                 writer.WriteLine("    }");
  54.                 writer.WriteLine();
  55.  
  56.                 // Write out sorting layers
  57.                 writer.WriteLine("    public static class SortingLayers");
  58.                 writer.WriteLine("    {");
  59.                 foreach (var layer in SortingLayer.layers) {
  60.                     writer.WriteLine("        /// <summary>");
  61.                     writer.WriteLine("        /// ID of sorting layer '{0}'.", layer.name);
  62.                     writer.WriteLine("        /// </summary>");
  63.                     writer.WriteLine("        public const int {0} = {1};", MakeSafeForCode(layer.name), layer.id);
  64.                 }
  65.                 writer.WriteLine("    }");
  66.                 writer.WriteLine();
  67.  
  68.                 // Write out layers
  69.                 writer.WriteLine("    public static class Layers");
  70.                 writer.WriteLine("    {");
  71.                 for (int i = 0; i < 32; i++) {
  72.                     string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i);
  73.                     if (!string.IsNullOrEmpty(layer)) {
  74.                         writer.WriteLine("        /// <summary>");
  75.                         writer.WriteLine("        /// Index of layer '{0}'.", layer);
  76.                         writer.WriteLine("        /// </summary>");
  77.                         writer.WriteLine("        public const int {0} = {1};", MakeSafeForCode(layer), i);
  78.                     }
  79.                 }
  80.                 writer.WriteLine();
  81.                 for (int i = 0; i < 32; i++) {
  82.                     string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i);
  83.                     if (!string.IsNullOrEmpty(layer)) {
  84.                         writer.WriteLine("        /// <summary>");
  85.                         writer.WriteLine("        /// Bitmask of layer '{0}'.", layer);
  86.                         writer.WriteLine("        /// </summary>");
  87.                         writer.WriteLine("        public const int {0}Mask = 1 << {1};", MakeSafeForCode(layer), i);
  88.                     }
  89.                 }
  90.                 writer.WriteLine("    }");
  91.                 writer.WriteLine();
  92.  
  93.                 // Write out scenes
  94.                 writer.WriteLine("    public static class Scenes");
  95.                 writer.WriteLine("    {");
  96.                 int sceneIndex = 0;
  97.                 foreach (var scene in EditorBuildSettings.scenes) {
  98.                     if (!scene.enabled) {
  99.                         continue;
  100.                     }
  101.  
  102.                     var sceneName = Path.GetFileNameWithoutExtension(scene.path);
  103.  
  104.                     writer.WriteLine("        /// <summary>");
  105.                     writer.WriteLine("        /// ID of scene '{0}'.", sceneName);
  106.                     writer.WriteLine("        /// </summary>");
  107.                     writer.WriteLine("        public const int {0} = {1};", MakeSafeForCode(sceneName), sceneIndex);
  108.  
  109.                     sceneIndex++;
  110.                 }
  111.                 writer.WriteLine("    }");
  112.                 writer.WriteLine();
  113.  
  114.                 // Write out Input axes
  115.                 writer.WriteLine("    public static class Axes");
  116.                 writer.WriteLine("    {");
  117.                 var axes = new HashSet<string>();
  118.                 var inputManagerProp = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
  119.                 foreach (SerializedProperty axe in inputManagerProp.FindProperty("m_Axes")) {
  120.                     var name = axe.FindPropertyRelative("m_Name").stringValue;
  121.                     var variableName = MakeSafeForCode(name);
  122.                     if (!axes.Contains(variableName)) {
  123.                         writer.WriteLine("        /// <summary>");
  124.                         writer.WriteLine("        /// Input axis '{0}'.", name);
  125.                         writer.WriteLine("        /// </summary>");
  126.                         writer.WriteLine("        public const string {0} = \"{1}\";", variableName, name);
  127.                         axes.Add(variableName);
  128.                     }
  129.                 }
  130.  
  131.                 // End of namespace UnityConstants
  132.                 writer.WriteLine("    }");
  133.                 writer.WriteLine();
  134.  
  135.  
  136.                 // Write out Animator property Hashes
  137.                 writer.WriteLine("    public static class AnimHash");
  138.                 writer.WriteLine("    {");
  139.                 var _allControllersGUID = AssetDatabase.FindAssets ("t:AnimatorController");
  140.                 Dictionary<string , int> anim_Properties = new Dictionary<string, int>();
  141.                 AnimatorController _controller_Anim;
  142.  
  143.                 foreach (string controller in _allControllersGUID) {
  144.                     _controller_Anim = ((AnimatorController)AssetDatabase.LoadAssetAtPath(  AssetDatabase.GUIDToAssetPath(controller) , typeof(AnimatorController)));
  145.                     if (_controller_Anim != null) {
  146.                         foreach (var property in _controller_Anim.parameters) {
  147.                             if ( anim_Properties.ContainsKey (property.name.ToString() ) == false ) {
  148.                                 anim_Properties.Add ( property.name.ToString (), property.name.GetHashCode ());
  149.                             }
  150.                         }
  151.                     }
  152.                 }
  153.  
  154.                 foreach (string property in anim_Properties.Keys) {
  155.                     writer.WriteLine("        /// <summary>");
  156.                     writer.WriteLine("        /// Hash of property '{0}'.", property);
  157.                     writer.WriteLine("        /// </summary>");
  158.                     writer.WriteLine("        public const int {0} = {1};", MakeSafeForCode(property.ToString() ), anim_Properties[ property ] );
  159.                 }
  160.  
  161.                 writer.WriteLine("    }");
  162.  
  163.                 // End of namespace UnityConstants
  164.                 writer.WriteLine("}");
  165.                 writer.WriteLine();
  166.             }
  167.  
  168.             // Refresh
  169.             AssetDatabase.Refresh();
  170.         }
  171.  
  172.         private static string MakeSafeForCode(string str)
  173.         {
  174.             str = Regex.Replace(str, "[^a-zA-Z0-9_]", "_", RegexOptions.Compiled);
  175.             if (char.IsDigit(str[0])) {
  176.                 str = "_" + str;
  177.             }
  178.             return str;
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment