namespace Zinnia.Data.Attribute { using UnityEditor; using UnityEngine; using Zinnia.Extension; /// /// Displays an inspector property drawer with restricted styles. /// [CustomPropertyDrawer(typeof(RestrictedAttribute))] public class RestrictedAttributeDrawer : PropertyDrawer { /// /// The original GUI enabled state. /// protected static bool originalGUIEnabledState; /// /// The original GUI color. /// protected static Color originalGuiColor; /// /// The original font style. /// protected static FontStyle originalFontStyle; /// /// The original normal text color. /// protected static Color originalNormalTextColor; /// /// The original focused text color. /// protected static Color originalFocusedTextColor; /// /// The color to use for muted text. /// protected static Color mutedColor = new Color(0.75f, 0.75f, 0.75f); /// /// The font style to use for muted text. /// protected static FontStyle mutedStyle = FontStyle.Italic; /// public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { originalGUIEnabledState = GUI.enabled; originalGuiColor = GUI.color; originalFontStyle = EditorStyles.label.fontStyle; originalNormalTextColor = EditorStyles.label.normal.textColor; originalFocusedTextColor = EditorStyles.label.focused.textColor; EditorGUI.BeginProperty(position, label, property); RestrictedAttribute attrib = (RestrictedAttribute)attribute; Behaviour behaviour = (Behaviour)property.serializedObject.targetObject; bool isPlayingAndActiveAndEnabled = Application.isPlaying && behaviour.CheckIsActiveAndEnabled(); bool isPlayingAndActiveAndDisabled = Application.isPlaying && !behaviour.CheckIsActiveAndEnabled(); bool makeReadOnly = (attrib.restrictions & RestrictedAttribute.Restrictions.ReadOnlyAlways) != 0 || ((attrib.restrictions & RestrictedAttribute.Restrictions.ReadOnlyAtRunTime) != 0 && Application.isPlaying) || ((attrib.restrictions & RestrictedAttribute.Restrictions.ReadOnlyAtRunTimeAndEnabled) != 0 && isPlayingAndActiveAndEnabled) || ((attrib.restrictions & RestrictedAttribute.Restrictions.ReadOnlyAtRunTimeAndDisabled) != 0 && isPlayingAndActiveAndDisabled); bool muteProperty = (attrib.restrictions & RestrictedAttribute.Restrictions.Muted) != 0; originalGUIEnabledState = GUI.enabled; if (makeReadOnly) { GUI.enabled = false; } if (muteProperty) { GUI.color = mutedColor; EditorStyles.label.normal.textColor = mutedColor; EditorStyles.label.focused.textColor = mutedColor; EditorStyles.label.fontStyle = mutedStyle; } EditorGUI.PropertyField(position, property, label, true); GUI.color = originalGuiColor; EditorStyles.label.normal.textColor = originalNormalTextColor; EditorStyles.label.focused.textColor = originalFocusedTextColor; EditorStyles.label.fontStyle = originalFontStyle; GUI.enabled = originalGUIEnabledState; EditorGUI.EndProperty(); } /// public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return EditorGUI.GetPropertyHeight(property); } } }