public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var sortingLayerNames = SortingLayerHelper.sortingLayerNames; if (property.propertyType != SerializedPropertyType.Integer) { EditorGUI.HelpBox(position, string.Format("{0} is not an integer but has [SortingLayer].", property.name), MessageType.Error); } else if (sortingLayerNames != null) { EditorGUI.BeginProperty(position, label, property); // Look up the layer name using the current layer ID string oldName = SortingLayerHelper.GetSortingLayerNameFromID(property.intValue); // Use the name to look up our array index into the names list int oldLayerIndex = Array.IndexOf(sortingLayerNames, oldName); // Show the popup for the names int newLayerIndex = EditorGUI.Popup(position, label.text, oldLayerIndex, sortingLayerNames); // If the index changes, look up the ID for the new index to store as the new ID if (newLayerIndex != oldLayerIndex) { property.intValue = SortingLayerHelper.GetSortingLayerIDForIndex(newLayerIndex); } EditorGUI.EndProperty(); } else { EditorGUI.BeginProperty(position, label, property); int newValue = EditorGUI.IntField(position, label.text, property.intValue); if (newValue != property.intValue) { property.intValue = newValue; } EditorGUI.EndProperty(); } }
public static void Generate() { // Try to find an existing file in the project called "UnityConstants.cs" string filePath = string.Empty; foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories)) { if (Path.GetFileNameWithoutExtension(file) == "UnityConstants") { filePath = file; break; } } // If no such file exists already, use the save panel to get a folder in which the file will be placed. if (string.IsNullOrEmpty(filePath)) { string directory = EditorUtility.OpenFolderPanel("Choose location for UnityConstants.cs", Application.dataPath, ""); // Canceled choose? Do nothing. if (string.IsNullOrEmpty(directory)) { return; } filePath = Path.Combine(directory, "UnityConstants.cs"); } // Write out our file using (var writer = new StreamWriter(filePath)) { writer.WriteLine("// This file is auto-generated. Modifications are not saved."); writer.WriteLine(); writer.WriteLine("namespace UnityConstants"); writer.WriteLine("{"); // Write out the tags writer.WriteLine(" public static class Tags"); writer.WriteLine(" {"); foreach (var tag in UnityEditorInternal.InternalEditorUtility.tags) { writer.WriteLine(" /// <summary>"); writer.WriteLine(" /// Name of tag '{0}'.", tag); writer.WriteLine(" /// </summary>"); writer.WriteLine(" public const string {0} = \"{1}\";", MakeSafeForCode(tag), tag); } writer.WriteLine(" }"); writer.WriteLine(); // Write out sorting layers var sortingLayerNames = SortingLayerHelper.sortingLayerNames; if (sortingLayerNames != null) { writer.WriteLine(" public static class SortingLayers"); writer.WriteLine(" {"); for (int i = 0; i < sortingLayerNames.Length; i++) { var name = sortingLayerNames[i]; int id = SortingLayerHelper.GetSortingLayerIDForName(name); writer.WriteLine(" /// <summary>"); writer.WriteLine(" /// ID of sorting layer '{0}'.", name); writer.WriteLine(" /// </summary>"); writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(name), id); } writer.WriteLine(" }"); writer.WriteLine(); } // Write out layers writer.WriteLine(" public static class Layers"); writer.WriteLine(" {"); for (int i = 0; i < 32; i++) { string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i); if (!string.IsNullOrEmpty(layer)) { writer.WriteLine(" /// <summary>"); writer.WriteLine(" /// Index of layer '{0}'.", layer); writer.WriteLine(" /// </summary>"); writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(layer), i); } } writer.WriteLine(); for (int i = 0; i < 32; i++) { string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i); if (!string.IsNullOrEmpty(layer)) { writer.WriteLine(" /// <summary>"); writer.WriteLine(" /// Bitmask of layer '{0}'.", layer); writer.WriteLine(" /// </summary>"); writer.WriteLine(" public const int {0}Mask = 1 << {1};", MakeSafeForCode(layer), i); } } writer.WriteLine(" }"); writer.WriteLine(); // Write out scenes writer.WriteLine(" public static class Scenes"); writer.WriteLine(" {"); for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) { string scene = Path.GetFileNameWithoutExtension(EditorBuildSettings.scenes[i].path); writer.WriteLine(" /// <summary>"); writer.WriteLine(" /// Name of '{0}'.", scene); writer.WriteLine(" /// </summary>"); writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(scene), i); } writer.WriteLine(" }"); writer.WriteLine("}"); writer.WriteLine(); } // Refresh AssetDatabase.Refresh(); }
public static void DrawSortingLayerGUI(UnityEngine.Object target, UnityEngine.Object[] targets) { // Get the renderer from the target object var targetRenderer = (target as MonoBehaviour).gameObject.GetComponent <Renderer> (); if (targetRenderer != null) { string sortingLayerName = targetRenderer.sortingLayerName; int sortingLayerID = targetRenderer.sortingLayerID; int sortingOrder = targetRenderer.sortingOrder; var sortingLayerNames = SortingLayerHelper.sortingLayerNames; // If we have the sorting layers array, we can make a nice dropdown. For stability's sake, if the array is null // we just use our old logic. This makes sure the script works in some fashion even if Unity changes the name of // that internal field we reflected. if (sortingLayerNames != null) { // Look up the layer name using the current layer ID string oldName = SortingLayerHelper.GetSortingLayerNameFromID(sortingLayerID); // Use the name to look up our array index into the names list int oldLayerIndex = Array.IndexOf(sortingLayerNames, oldName); // Show the popup for the names int newLayerIndex = EditorGUILayout.Popup("Sorting Layer", oldLayerIndex, sortingLayerNames); // If the index changes, look up the ID for the new index to store as the new ID if (newLayerIndex != oldLayerIndex) { foreach (UnityEngine.Object enumTarget in targets) { var renderer = (enumTarget as MonoBehaviour).gameObject.GetComponent <Renderer> (); Undo.RecordObject(renderer, "Edit Sorting Layer"); renderer.sortingLayerID = SortingLayerHelper.GetSortingLayerIDForIndex(newLayerIndex); EditorUtility.SetDirty(renderer); } } } else { // Expose the sorting layer name string newSortingLayerName = EditorGUILayout.TextField("Sorting Layer Name", sortingLayerName); if (newSortingLayerName != sortingLayerName) { foreach (UnityEngine.Object enumTarget in targets) { var renderer = (enumTarget as MonoBehaviour).gameObject.GetComponent <Renderer> (); Undo.RecordObject(renderer, "Edit Sorting Layer Name"); renderer.sortingLayerName = newSortingLayerName; EditorUtility.SetDirty(renderer); } } // Expose the sorting layer ID int newSortingLayerId = EditorGUILayout.IntField("Sorting Layer ID", sortingLayerID); if (newSortingLayerId != sortingLayerID) { foreach (UnityEngine.Object enumTarget in targets) { var renderer = (enumTarget as MonoBehaviour).gameObject.GetComponent <Renderer> (); Undo.RecordObject(renderer, "Edit Sorting Layer ID"); renderer.sortingLayerID = newSortingLayerId; EditorUtility.SetDirty(renderer); } } } // Expose the manual sorting order int newSortingLayerOrder = EditorGUILayout.IntField("Order in Layer", sortingOrder); if (newSortingLayerOrder != sortingOrder) { foreach (UnityEngine.Object enumTarget in targets) { var renderer = (enumTarget as MonoBehaviour).gameObject.GetComponent <Renderer> (); Undo.RecordObject(renderer, "Edit Sorting Order"); renderer.sortingOrder = newSortingLayerOrder; EditorUtility.SetDirty(renderer); } } } else { EditorGUILayout.LabelField("There is no Renderer component on selected object"); } }
public override void OnInspectorGUI() { // Get the renderer from the target object var renderer = (target as SortingLayerExposed).gameObject.renderer; // If there is no renderer, we can't do anything if (!renderer) { EditorGUILayout.HelpBox("SortingLayerExposed must be added to a game object that has a renderer.", MessageType.Error); return; } var sortingLayerNames = SortingLayerHelper.sortingLayerNames; // If we have the sorting layers array, we can make a nice dropdown. For stability's sake, if the array is null // we just use our old logic. This makes sure the script works in some fashion even if Unity changes the name of // that internal field we reflected. if (sortingLayerNames != null) { // Look up the layer name using the current layer ID string oldName = SortingLayerHelper.GetSortingLayerNameFromID(renderer.sortingLayerID); // Use the name to look up our array index into the names list int oldLayerIndex = Array.IndexOf(sortingLayerNames, oldName); // Show the popup for the names int newLayerIndex = EditorGUILayout.Popup("Sorting Layer", oldLayerIndex, sortingLayerNames); // If the index changes, look up the ID for the new index to store as the new ID if (newLayerIndex != oldLayerIndex) { Undo.RecordObject(renderer, "Edit Sorting Layer"); renderer.sortingLayerID = SortingLayerHelper.GetSortingLayerIDForIndex(newLayerIndex); EditorUtility.SetDirty(renderer); } } else { // Expose the sorting layer name string newSortingLayerName = EditorGUILayout.TextField("Sorting Layer Name", renderer.sortingLayerName); if (newSortingLayerName != renderer.sortingLayerName) { Undo.RecordObject(renderer, "Edit Sorting Layer Name"); renderer.sortingLayerName = newSortingLayerName; EditorUtility.SetDirty(renderer); } // Expose the sorting layer ID int newSortingLayerId = EditorGUILayout.IntField("Sorting Layer ID", renderer.sortingLayerID); if (newSortingLayerId != renderer.sortingLayerID) { Undo.RecordObject(renderer, "Edit Sorting Layer ID"); renderer.sortingLayerID = newSortingLayerId; EditorUtility.SetDirty(renderer); } } // Expose the manual sorting order int newSortingLayerOrder = EditorGUILayout.IntField("Sorting Layer Order", renderer.sortingOrder); if (newSortingLayerOrder != renderer.sortingOrder) { Undo.RecordObject(renderer, "Edit Sorting Order"); renderer.sortingOrder = newSortingLayerOrder; EditorUtility.SetDirty(renderer); } }
public static void Generate() { // Try to find an existing file in the project called "UnityConstants.cs" string filePath = string.Empty; foreach (var file in Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories)) { if (Path.GetFileNameWithoutExtension(file) == "UnityConstants") { filePath = file; break; } } // If no such file exists already, use the save panel to get a folder in which the file will be placed. if (string.IsNullOrEmpty(filePath)) { string directory = EditorUtility.OpenFolderPanel("Choose location for UnityConstants.cs", Application.dataPath, ""); // Canceled choose? Do nothing. if (string.IsNullOrEmpty(directory)) { return; } filePath = Path.Combine(directory, "UnityConstants.cs"); } // Write out our file using (var writer = new StreamWriter(filePath)) { writer.WriteLine("// This file is auto-generated. Modifications are not saved."); writer.WriteLine(); writer.WriteLine("namespace UnityConstants"); writer.WriteLine("{"); // Write out the tags writer.WriteLine(" public static class Tags"); writer.WriteLine(" {"); foreach (var tag in UnityEditorInternal.InternalEditorUtility.tags) { writer.WriteLine(" public const string {0} = \"{1}\";", MakeSafeForCode(tag), tag); } writer.WriteLine(" }"); writer.WriteLine(); // Write out sorting layers var sortingLayerNames = SortingLayerHelper.sortingLayerNames; if (sortingLayerNames != null) { writer.WriteLine(" public static class SortingLayers"); writer.WriteLine(" {"); for (int i = 0; i < sortingLayerNames.Length; i++) { var name = sortingLayerNames[i]; int id = SortingLayerHelper.GetSortingLayerIDForName(name); writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(name), id); } writer.WriteLine(" }"); writer.WriteLine(); } // Write out layers writer.WriteLine(" public static class Layers"); writer.WriteLine(" {"); for (int i = 0; i < 32; i++) { string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i); if (string.IsNullOrEmpty(layer)) { continue; } writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(layer), i); } writer.WriteLine(); for (int i = 0; i < 32; i++) { string layer = UnityEditorInternal.InternalEditorUtility.GetLayerName(i); if (string.IsNullOrEmpty(layer)) { continue; } writer.WriteLine(" public const int {0}Mask = 1 << {1};", MakeSafeForCode(layer), i); } writer.WriteLine(" }"); writer.WriteLine(); // Write out scenes writer.WriteLine(" public static class Scenes"); writer.WriteLine(" {"); for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) { string scene = Path.GetFileNameWithoutExtension(EditorBuildSettings.scenes[i].path); writer.WriteLine(" public const int {0} = {1};", MakeSafeForCode(scene), i); } writer.WriteLine(" }"); writer.WriteLine(); // Write out Input axes writer.WriteLine(" public static class Axes"); writer.WriteLine(" {"); var axes = new HashSet <string>(); var inputManagerProp = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]); foreach (SerializedProperty axe in inputManagerProp.FindProperty("m_Axes")) { var name = axe.FindPropertyRelative("m_Name").stringValue; var variableName = MakeSafeForCode(name); if (axes.Contains(variableName)) { continue; } writer.WriteLine(" public const string {0} = \"{1}\";", variableName, name); axes.Add(variableName); } writer.WriteLine(" }"); // End of namespace UnityConstants writer.WriteLine("}"); writer.WriteLine(); } // Refresh AssetDatabase.Refresh(); }