namespace Zinnia.Utility { using System; using System.Collections.Generic; using System.Reflection; using UnityEditor; using UnityEngine; /// /// The EditorHelper provides a collection of common static methods for use within Editor scripts. /// public static class EditorHelper { /// /// The GetTooltipAttribute method returns the [Tooltip(")] attribute for the given FieldInfo. /// /// The FieldInfo to get the tooltip attribute from. /// A TooltipAttribute linked to the given FieldInfo. public static TooltipAttribute GetTooltipAttribute(FieldInfo fieldInfo) { return (TooltipAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(TooltipAttribute)); } /// /// The BuildGUIContent method returns a GUIContent formatted element. /// /// The type of the FieldInfo for the given field name. /// The name of the field to build the GUIContent for. /// An optional string to override the display text in the GUIContent. /// A GUIContent formatted with the appropriate field information and tooltip attribute. public static GUIContent BuildGUIContent(string fieldName, string displayOverride = null) { string displayName = (displayOverride != null ? displayOverride : ObjectNames.NicifyVariableName(fieldName)); FieldInfo fieldInfo = typeof(T).GetField(fieldName); TooltipAttribute tooltipAttribute = GetTooltipAttribute(fieldInfo); return (tooltipAttribute == null ? new GUIContent(displayName) : new GUIContent(displayName, tooltipAttribute.tooltip)); } /// /// The AddHeader method attempts to add a header label. /// /// The type of the FieldInfo for the given field name. /// The name of the field to look for the header attribute from. /// An optional string to use for the display text in the header. public static void AddHeader(string fieldName, string displayOverride = null) { string displayName = (displayOverride != null ? displayOverride : ObjectNames.NicifyVariableName(fieldName)); FieldInfo fieldInfo = typeof(T).GetField(fieldName); HeaderAttribute headerAttribute = (HeaderAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(HeaderAttribute)); AddHeader(headerAttribute == null ? displayName : headerAttribute.header); } /// /// The AddHeader method attempts to add a header label. /// /// The string for the header label. /// Determines whether a line space should be added before the header. public static void AddHeader(string header, bool spaceBeforeHeader = true) { if (spaceBeforeHeader) { EditorGUILayout.Space(); } EditorGUILayout.LabelField(header, EditorStyles.boldLabel); } /// /// The DrawUsingDestructiveStyle method draws an action with a destructive style. /// /// The GUIStyle to copy the style from. /// The Action to draw the style with. public static void DrawUsingDestructiveStyle(GUIStyle styleToCopy, Action drawAction) { Color previousBackgroundColor = GUI.backgroundColor; GUIStyle destructiveButtonStyle = new GUIStyle(styleToCopy) { normal = { textColor = Color.white }, active = { textColor = Color.white } }; GUI.backgroundColor = Color.red; drawAction(destructiveButtonStyle); GUI.backgroundColor = previousBackgroundColor; } /// /// The DrawScrollableSelectableLabel method attempts to draw a selectable label that is also scrollable. /// /// The current scroll position of the label element. /// The width of the label element. /// The text to display within the label element. /// The GUIStyle to draw the label with. public static void DrawScrollableSelectableLabel(ref Vector2 scrollPosition, ref float width, string text, GUIStyle style) { using (EditorGUILayout.ScrollViewScope scrollViewScope = new EditorGUILayout.ScrollViewScope(scrollPosition)) { scrollPosition = scrollViewScope.scrollPosition; float textHeight = style.CalcHeight(new GUIContent(text), width); EditorGUILayout.SelectableLabel(text, style, GUILayout.MinHeight(textHeight)); if (Event.current.type == EventType.Repaint) { width = GUILayoutUtility.GetLastRect().width; } } } /// /// Allows accessing the 's tags list through a . /// /// The 's tags list. public static SerializedProperty GetTagManagerTagsProperty() { SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadMainAssetAtPath("ProjectSettings/TagManager.asset")); return tagManager.FindProperty("tags"); } /// /// Adds tags to the . /// /// The tags to add. /// The tags that were added. A provided tag is only included if it was not already in the tags list of the . public static List AddTags(params string[] tags) { List results = new List(tags.Length); SerializedProperty tagsProperty = GetTagManagerTagsProperty(); foreach (string tag in tags) { bool found = false; for (int index = 0; index < tagsProperty.arraySize; index++) { if (tagsProperty.GetArrayElementAtIndex(index).stringValue != tag) { continue; } found = true; break; } if (found) { continue; } int addIndex = tagsProperty.arraySize; tagsProperty.InsertArrayElementAtIndex(addIndex); tagsProperty.GetArrayElementAtIndex(addIndex).stringValue = tag; results.Add(tag); } tagsProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo(); return results; } /// /// Removes tags from the . /// /// The tags to remove. /// The tags that were removed. A provided tag is only included if it was in the tags list of the . public static List RemoveTags(params string[] tags) { List results = new List(tags.Length); SerializedProperty tagsProperty = GetTagManagerTagsProperty(); foreach (string tag in tags) { for (int index = 0; index < tagsProperty.arraySize; index++) { if (tagsProperty.GetArrayElementAtIndex(index).stringValue != tag) { continue; } tagsProperty.DeleteArrayElementAtIndex(index); results.Add(tag); } } tagsProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo(); return results; } /// /// Draws a horizontal line on the GUI. /// /// The color for the line. /// The height of the line. /// The margin space above and below the line. public static void DrawHorizontalLine(Color color, float height, Vector2 margin) { GUILayout.Space(margin.x); EditorGUI.DrawRect(EditorGUILayout.GetControlRect(false, height), color); GUILayout.Space(margin.y); } /// /// Draws a horizontal line on the GUI. /// public static void DrawHorizontalLine() { DrawHorizontalLine(new Color(0f, 0f, 0f, 0.3f), 1f, Vector2.one * 5f); } } }