Beispiel #1
0
    static void Build()
    {
        var layerNames = new List<string>();

        var objs = Resources.FindObjectsOfTypeAll<Object>();
        var sortingLayers = new List<string>();
        var inputNames = new List<string>();
        var navMeshLayers = new List<string>();

        foreach (var obj in objs)
        {
            switch (obj.name)
            {
                case "InputManager":
                    {
                        var axesProperty = new SerializedObject(obj).FindProperty("m_Axes");

                        for (var j = 0; j < axesProperty.arraySize; j++)
                        {
                            inputNames.Add(axesProperty.GetArrayElementAtIndex(j).FindPropertyRelative("m_Name").stringValue);
                        }
                    }
                    break;
                case "TagManager":
                    {
                        var sortinglayersProperty = new SerializedObject(obj).FindProperty("m_SortingLayers");

                        for (var j = 0; j < sortinglayersProperty.arraySize; j++)
                        {
                            sortingLayers.Add(sortinglayersProperty.GetArrayElementAtIndex(j).FindPropertyRelative("name").stringValue);
                        }
                    }
                    break;

                case "NavMeshLayers":
                    {

                        var navMeshlayersObject = new SerializedObject(obj);

                        for (var j = 0; j < 3; j++)
                        {
                            navMeshLayers.Add(navMeshlayersObject.FindProperty("Built-in Layer " + j).FindPropertyRelative("name").stringValue);
                        }

                        for (var j = 0; j < 28; j++)
                        {
                            navMeshLayers.Add(navMeshlayersObject.FindProperty("User Layer " + j).FindPropertyRelative("name").stringValue);
                        }
                    }
                    break;
            }
        }

        for (var i = 0; i < 32; i++)
        {
            layerNames.Add(LayerMask.LayerToName(i));
        }

        AssetDatabase.StartAssetEditing();
        {
            Build("Tag", InternalEditorUtility.tags);
            Build("Layer", layerNames.ToArray());
            Build("SortingLayer", sortingLayers.ToArray());
            Build("NavMeshLayer", navMeshLayers.ToArray());
            Build("Input", inputNames.ToArray());
            Build("Scene", EditorBuildSettings.scenes.Where(scene => scene.enabled).Select<EditorBuildSettingsScene, string>(scene => Path.GetFileNameWithoutExtension(scene.path)).ToArray());
        }
        AssetDatabase.StopAssetEditing();
        EditorUtility.UnloadUnusedAssetsImmediate();
        AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
    }
        static string GenerateFields(string indent, Options options)
        {
            var lines = new List<string> (128);
            var uniquesList = new HashSet<string> ();

            // layers, layer masks
            if ((int) (options & Options.Layers) != 0) {
                foreach (var layerName in InternalEditorUtility.layers) {
                    lines.Add (string.Format (LayerName, indent, CleanupName (layerName), CleanupValue (layerName)));
                    lines.Add (string.Format (LayerMask, indent, CleanupName (layerName)));
                }
            }

            // tags
            if ((int) (options & Options.Tags) != 0) {
                foreach (var tagName in InternalEditorUtility.tags) {
                    lines.Add (string.Format (TagName, indent, CleanupName (tagName), CleanupValue (tagName)));
                }
            }

            // scenes
            if ((int) (options & Options.Scenes) != 0) {
                foreach (var scene in EditorBuildSettings.scenes) {
                    var sceneName = Path.GetFileNameWithoutExtension (scene.path);
                    lines.Add (string.Format (SceneName, indent, CleanupName (sceneName), CleanupValue (sceneName)));
                }
            }

            // animators
            if ((int) (options & Options.Animators) != 0) {
                foreach (var guid in AssetDatabase.FindAssets ("t:animatorcontroller")) {
                    var assetPath = AssetDatabase.GUIDToAssetPath (guid);
                    var ac = AssetDatabase.LoadAssetAtPath<UnityEditor.Animations.AnimatorController> (assetPath);
                    for (int i = 0, iMax = ac.parameters.Length; i < iMax; i++) {
                        var name = ac.parameters[i].name;
                        if (!uniquesList.Contains (name)) {
                            lines.Add (string.Format (AnimatorName, indent, CleanupName (name), CleanupValue (name)));
                            uniquesList.Add (name);
                        }
                    }
                }
                uniquesList.Clear ();
            }

            // axes
            if ((int) (options & Options.Axes) != 0) {
                var inputManager = AssetDatabase.LoadAllAssetsAtPath ("ProjectSettings/InputManager.asset")[0];
                var axes = new SerializedObject (inputManager).FindProperty ("m_Axes");
                for (int i = 0, iMax = axes.arraySize; i < iMax; i++) {
                    var axis = axes.GetArrayElementAtIndex (i).FindPropertyRelative ("m_Name").stringValue;
                    if (!uniquesList.Contains (axis)) {
                        lines.Add (string.Format (AxisName, indent, CleanupName (axis), CleanupValue (axis)));
                        uniquesList.Add (axis);
                    }
                }
                uniquesList.Clear ();
            }

            // shaders
            if ((int) (options & Options.Shaders) != 0) {
                foreach (var guid in AssetDatabase.FindAssets ("t:shader")) {
                    var assetPath = AssetDatabase.GUIDToAssetPath (guid);
                    var shader = AssetDatabase.LoadAssetAtPath<Shader> (assetPath);
                    if (shader.name.IndexOf ("Hidden") != 0) {
                        for (int i = 0, iMax = ShaderUtil.GetPropertyCount (shader); i < iMax; i++) {
                            if (!ShaderUtil.IsShaderPropertyHidden (shader, i)) {
                                var name = ShaderUtil.GetPropertyName (shader, i);
                                if (!uniquesList.Contains (name)) {
                                    lines.Add (string.Format (ShaderName, indent, CleanupName (name), CleanupValue (name)));
                                    uniquesList.Add (name);
                                }
                            }
                        }
                    }
                }
                uniquesList.Clear ();
            }

            lines.Sort ();
            return string.Join ("\n\n", lines.ToArray ());
        }