#if UNITY_EDITOR using System; using UnityEditor; using UnityEngine; namespace GameKit.Dependencies.Utilities { public enum EditorLayoutEnableType { Enabled = 0, Disabled = 1, DisabledWhilePlaying = 2 } public static class EditorGuiLayoutTools { /// /// Adds a helpbox field. /// public static void AddHelpBox(string text, MessageType messageType = MessageType.Info) { EditorGUILayout.HelpBox(text, messageType); } /// /// Adds a property field. /// public static void AddPropertyField(SerializedProperty sp, string fieldName, string tooltip = "") { if (tooltip == "") tooltip = sp.tooltip; EditorGUILayout.PropertyField(sp, new GUIContent(fieldName, tooltip)); } /// /// Adds a property field. /// public static void AddPropertyField(SerializedProperty sp, GUIContent guiContent) { EditorGUILayout.PropertyField(sp, guiContent); } /// /// Adds a property field. /// public static void AddPropertyField(SerializedProperty sp, GUIContent guiContent = null, EditorLayoutEnableType enableType = EditorLayoutEnableType.Enabled, params GUILayoutOption[] options) { bool disable = IsDisableLayoutType(enableType); if (disable) GUI.enabled = false; EditorGUILayout.PropertyField(sp, guiContent, options); if (disable) GUI.enabled = true; } /// /// Adds a property field. /// /// True to have property enabled. [Obsolete("Use AddPropertyField(SerializedProperty, GUIContent, EditorLayoutEnableType, GUILayoutOption.")] public static void AddPropertyField(SerializedProperty sp, GUIContent guiContent = null, bool enabled = true, params GUILayoutOption[] options) { EditorLayoutEnableType enableType = (enabled) ? EditorLayoutEnableType.Enabled : EditorLayoutEnableType.Disabled; bool disable = IsDisableLayoutType(enableType); if (disable) GUI.enabled = false; EditorGUILayout.PropertyField(sp, guiContent, options); if (disable) GUI.enabled = true; } /// /// Adds an object field. /// public static void AddObjectField(string label, MonoScript ms, System.Type type, bool allowSceneObjects, EditorLayoutEnableType enableType = EditorLayoutEnableType.Enabled, params GUILayoutOption[] options) { bool disable = IsDisableLayoutType(enableType); if (disable) GUI.enabled = false; EditorGUILayout.ObjectField("Script:", ms, type, allowSceneObjects, options); if (disable) GUI.enabled = true; } /// /// Disables GUI if playing. /// public static void DisableGUIIfPlaying() { if (Application.isPlaying) GUI.enabled = false; } /// /// Enables GUI if playing. /// public static void EnableGUIIfPlaying() { if (Application.isPlaying) GUI.enabled = true; } /// /// Returns if a layout field should be disabled. /// /// /// private static bool IsDisableLayoutType(EditorLayoutEnableType enableType) { return (enableType == EditorLayoutEnableType.Disabled || (enableType == EditorLayoutEnableType.DisabledWhilePlaying && Application.isPlaying)); } } public static class PropertyDrawerToolExtensions { /// /// Returns GetPropertyHeight value based on drawerTool properties. /// public static float GetPropertyHeight(this PropertyDrawerTool drawerTool) { if (drawerTool == null) return EditorGUIUtility.singleLineHeight; return (EditorGUIUtility.singleLineHeight * drawerTool.LineSpacingMultiplier * drawerTool.PropertiesDrawn); } } /// /// Various utility classes relating to floats. /// public class PropertyDrawerTool { public PropertyDrawerTool() { Debug.LogError($"This initializer is not supported. Use the initializer with arguments."); } public PropertyDrawerTool(Rect position, float lineSpacingMultiplier = 1f) { Position = position; LineSpacingMultiplier = lineSpacingMultiplier; Position = position; _startingIndent = EditorGUI.indentLevel; } /// /// Starting position as indicated by the OnGUI method. /// /// This value may be modified by user code. public Rect Position = default; /// /// Preferred spacing between each draw. /// public float LineSpacingMultiplier; /// /// Number of entries drawn by this object. /// public int PropertiesDrawn = 0; /// /// Additional position Y of next draw. /// private float _additionalPositionY = 0; /// /// Indent level during initialization. /// private readonly int _startingIndent; /// /// Sets EditorGUI.Indent to the level it were when initializing this class. /// public void SetIndentToStarting() => EditorGUI.indentLevel = _startingIndent; /// /// Draws a label. /// public void DrawLabel(GUIContent lLabel) => DrawLabel(lLabel, EditorStyles.label.fontStyle, indent: 0); /// /// Draws a label. /// public void DrawLabel(GUIContent lLabel, FontStyle styleOverride) => DrawLabel(lLabel, styleOverride, indent: 0); /// /// Draws a label. /// public void DrawLabel(GUIContent lLabel, FontStyle styleOverride, int indent) { PropertiesDrawn++; if (indent != 0) EditorGUI.indentLevel += indent; //Set style. FontStyle startingStyle = EditorStyles.label.fontStyle; EditorStyles.label.fontStyle = styleOverride; EditorGUI.PrefixLabel(GetRect(), GUIUtility.GetControlID(FocusType.Passive), lLabel); EditorStyles.label.fontStyle = startingStyle; if (indent != 0) EditorGUI.indentLevel -= indent; } /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop) => DrawProperty(prop, lLabel: "", indent: 0); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, string label) => DrawProperty(prop, new GUIContent(label), EditorStyles.label.fontStyle, indent: 0); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, GUIContent content) => DrawProperty(prop, content, EditorStyles.label.fontStyle, indent: 0); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, int indent) => DrawProperty(prop, lLabel: "", indent); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, string lLabel, int indent) => DrawProperty(prop, lLabel, EditorStyles.label.fontStyle, indent); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, GUIContent content, int indent) => DrawProperty(prop, content, EditorStyles.label.fontStyle, indent); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, GUIContent content, FontStyle labelStyle) => DrawProperty(prop, content, labelStyle, indent: 0); /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, string lLabel, FontStyle labelStyle, int indent) { GUIContent content = (lLabel == "") ? default : new GUIContent(lLabel); DrawProperty(prop, content, labelStyle, indent); } /// /// Draws a property. /// public void DrawProperty(SerializedProperty prop, GUIContent content, FontStyle labelStyle, int indent) { PropertiesDrawn++; EditorGUI.indentLevel += indent; FontStyle startingStyle = EditorStyles.label.fontStyle; EditorStyles.label.fontStyle = labelStyle; EditorGUI.PropertyField(GetRect(), prop, content); EditorStyles.label.fontStyle = startingStyle; EditorGUI.indentLevel -= indent; } /// /// Gets the next Rect to draw at. /// /// public Rect GetRect(float? lineSpacingMultiplierOverride = null) { float multiplier = lineSpacingMultiplierOverride ?? LineSpacingMultiplier; Rect result = new(Position.x, Position.y + _additionalPositionY, Position.width, EditorGUIUtility.singleLineHeight * multiplier); _additionalPositionY += EditorGUIUtility.singleLineHeight * multiplier; return result; } } } #endif