private void GetReference(object _object) { SerializedProperty _property = (SerializedProperty)_object; System.Type _type = EditorGUIUtilityEnhanced.FindSerializedObjectField(_property.serializedObject, _property.name).FieldType; _property.objectReferenceValue = ((Component)_property.serializedObject.targetObject).GetComponent(_type); _property.serializedObject.ApplyModifiedProperties(); }
/******************************** ******* PROGRESS BAR ******* *******************************/ /********** UTILITY **********/ /// <summary> /// Try to get a progress bar max value or display error box. /// </summary> /// <param name="_position">Rect to draw error box if needed.</param> /// <param name="_property">Object to get variable value from.</param> /// <param name="_maxValueVariableName">Name of the variable acting as maximum value.</param> /// <param name="_maxValue">Max value.</param> /// <returns>Returns false if variable type is incompatible, true otherwise.</returns> private static bool TryGetProgressBarMaxValue(Rect _position, SerializedProperty _property, string _maxValueVariableName, out float _maxValue) { // Try to get progress bar max value if (EditorGUIUtilityEnhanced.GetSerializedObjectFieldOrPropertyValueAsSingle(_property.serializedObject, _maxValueVariableName, out _maxValue)) { return(true); } // If variable type is not comptatible, show error box EditorGUI.HelpBox(_position, $"Error : {_property.name} field | \"{_maxValueVariableName}\" value cannot be converted to single !", MessageType.Error); return(false); }
/// <summary> /// Try to get a progress bar value or display error box. /// </summary> /// <param name="_position">Rect to draw error box if needed.</param> /// <param name="_property">Property to get value from.</param> /// <param name="_value">Value of the property.</param> /// <returns>Returns false if property type is incompatible, true otherwise.</returns> private static bool TryGetProgressBarValue(Rect _position, SerializedProperty _property, out float _value) { // Try to get progress bar value if (EditorGUIUtilityEnhanced.GetPropertyValueAsSingle(_property, out _value)) { return(true); } // If property type is not comptatible, show error box EditorGUI.HelpBox(_position, $"Error : {_property.name} field | Progress bar cannot be used with \"{_property.propertyType}\" variable type !", MessageType.Error); return(false); }
/// <summary> /// Draw a serialized property field associated with another property ; /// when setting field value, associated property will be set automatically. /// Usefull for clamping value or calling event on inspector edit. /// </summary> /// <param name="_position">Rect to draw field in.</param> /// <param name="_property">Serialized property to edit.</param> /// <param name="_label">Displayed label.</param> /// <param name="_propertyName">Name of the associated property (must be in the same script as the property field).</param> public static void PropertyField(Rect _position, SerializedProperty _property, GUIContent _label, string _propertyName) { // Draw property field and new value by reflection EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(_position, _property, _label); if (EditorGUI.EndChangeCheck()) { _property.serializedObject.ApplyModifiedProperties(); EditorGUIUtilityEnhanced.SetPropertyValue(_property, _propertyName); } }
/// <summary> /// Draw an property field with an asset preview below it. /// </summary> /// <param name="_position">Rect to draw field in.</param> /// <param name="_property">Property to display and draw asset preview.</param> /// <param name="_label">Displayed label.</param> /// <param name="_foldout">The shown asset preview foldout state.</param> /// <returns>Returns new asset preview foldout state.</returns> public static bool AssetPreviewField(Rect _position, SerializedProperty _property, GUIContent _label, bool _foldout) { Texture2D _texture = EditorGUIUtilityEnhanced.GetAssetPreview(_property); // Display property field Rect _rect = new Rect() { x = _position.x, y = _position.y, width = _position.width - (_texture ? 25 : 0), height = EditorGUIUtility.singleLineHeight }; EditorGUI.PropertyField(_rect, _property, _label); // If no texture, draw nothing if (!_texture) { // If property is not valid, display informative box if (_property.propertyType != SerializedPropertyType.ObjectReference) { _position.y += _rect.height + EditorGUIUtility.standardVerticalSpacing; _position.height = EditorGUIUtilityEnhanced.DefaultHelpBoxHeight; EditorGUI.HelpBox(_position, "Asset Preview attribute can only be used with object reference type fields !", MessageType.Error); } return(false); } // Display foldout button next to property field _rect.x += _position.width - 5; _rect.width = _position.width - _rect.width; _foldout = EditorGUI.Foldout(_rect, _foldout, GUIContent.none); // If visible & assigned, display asset preview if (_foldout) { float _space = _rect.height + EditorGUIUtility.standardVerticalSpacing; float _aspect = _position.height - _space; _position.x += _position.width - 25 - _aspect; _position.y += _space; _position.width = _aspect; _position.height = _aspect; EditorGUI.DrawPreviewTexture(_position, _texture); } return(_foldout); }
/// <summary> /// Draw a required property field. /// </summary> /// <param name="_position">Rect to draw field in.</param> /// <param name="_property">Property to draw and check validity.</param> /// <param name="_label">Label to display before property.</param> public static void RequiredProperty(Rect _position, SerializedProperty _property, GUIContent _label) { Rect _rect = new Rect(_position.x, _position.y, _position.width, EditorGUIUtility.singleLineHeight); EditorGUI.PropertyField(_rect, _property, _label); if (!EditorGUIUtilityEnhanced.IsPropertyRequired(_property)) { return; } _rect.y += _rect.height + EditorGUIUtility.standardVerticalSpacing; _rect.height = EditorGUIUtilityEnhanced.DefaultHelpBoxHeight; EditorGUI.HelpBox(_rect, "Keep in mind to set a reference to this field !", MessageType.Error); }
/***************************** ********** MAX ********** ****************************/ /// <summary> /// Restrain a property value to a maximum one. /// </summary> /// <param name="_position">Rect to draw field.</param> /// <param name="_property">Editing property.</param> /// <param name="_maxValue">Property max value.</param> public static void MaxField(Rect _position, SerializedProperty _property, float _maxValue) => MaxField(_position, _property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _maxValue);
/***************************** ******** MIN MAX ******** ****************************/ /// <summary> /// Draw a min max slider field for a vector2. /// </summary> /// <param name="_position">Rect to draw field.</param> /// <param name="_property">Editing property.</param> /// <param name="_minLimit">Min allowed limit value.</param> /// <param name="_maxLimit">Max allowed limit value.</param> public static void MinMaxField(Rect _position, SerializedProperty _property, float _minLimit, float _maxLimit) => MinMaxField(_position, _property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _minLimit, _maxLimit);
/********************************* ******* ASSET PREVIEW ******* ********************************/ /// <summary> /// Draw an property field with an asset preview below it. /// </summary> /// <param name="_position">Rect to draw field in.</param> /// <param name="_property">Property to display and draw asset preview.</param> /// <param name="_foldout">The shown asset preview foldout state.</param> /// <returns>Returns new asset preview foldout state.</returns> public static bool AssetPreviewField(Rect _position, SerializedProperty _property, bool _foldout) => AssetPreviewField(_position, _property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _foldout);
/// <summary> /// Draw a progress bar. /// </summary> /// <param name="_position">Rect to draw progress bar.</param> /// <param name="_property">Property used as value in bar progression.</param> /// <param name="_maxValue">Bar maximum value.</param> /// <param name="_color">Bar color.</param> public static void ProgressBar(Rect _position, SerializedProperty _property, float _maxValue, Color _color) => ProgressBar(_position, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _property, _maxValue, _color);
/********** EDITABLE **********/ /// <summary> /// Draw an editable progress bar. /// </summary> /// <param name="_position">Rect to draw progress bar.</param> /// <param name="_property">Property used as value in bar progression.</param> /// <param name="_maxValueVariableName">Name of the variable used as maximum value (must be in the same script as the property).</param> /// <param name="_color">Bar color.</param> public static void EditableProgressBar(Rect _position, SerializedProperty _property, string _maxValueVariableName, Color _color) => EditableProgressBar(_position, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _property, _maxValueVariableName, _color);
/**************************** ******* REQUIRED ******* ***************************/ /// <summary> /// Draw a required property field. /// </summary> /// <param name="_property">Property to draw and check validity.</param> public static void RequiredProperty(SerializedProperty _property) => RequiredProperty(_property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property));
/*************************** ******* METHODS ******* **************************/ // Specify how tall the GUI for this decorator is in pixels public override float GetPropertyHeight(SerializedProperty _property, GUIContent _label) { AssetPreviewAttribute _attribute = (AssetPreviewAttribute)attribute; return(EditorGUIUtilityEnhanced.GetAssetPreviewPropertyHeight(_property, _attribute.Fodlout, _attribute.Aspect)); }
/********************************** ******* PROPERTY FIELD ******* *********************************/ /// <summary> /// Draw a serialized property field associated with another property ; /// when setting field value, associated property will be set automatically. /// Usefull for clamping value or calling event on inspector edit. /// </summary> /// <param name="_position">Rect to draw field in.</param> /// <param name="_property">Serialized property to edit.</param> /// <param name="_propertyName">Name of the associated property (must be in the same script as the property field).</param> public static void PropertyField(Rect _position, SerializedProperty _property, string _propertyName) => PropertyField(_position, _property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _propertyName);
/*************************** ******* METHODS ******* **************************/ // Specify how tall the GUI for this decorator is in pixels public override float GetPropertyHeight(SerializedProperty _property, GUIContent _label) { return(EditorGUIUtilityEnhanced.GetRequiredPropertyHeight(_property)); }
/// <summary> /// Draw an property field with an asset preview below it. /// </summary> /// <param name="_property">Property to display and draw asset preview.</param> /// <param name="_label">Displayed label.</param> /// <param name="_foldout">The shown asset preview foldout state.</param> /// <param name="_previewAspect">Aspect of the displayed asset preview.</param> /// <returns>Returns new asset preview foldout state.</returns> public static bool AssetPreviewField(SerializedProperty _property, GUIContent _label, bool _foldout, float _previewAspect) { Rect _rect = EditorGUILayout.GetControlRect(true, EditorGUIUtilityEnhanced.GetAssetPreviewPropertyHeight(_property, _foldout, _previewAspect)); return(EditorGUIEnhanced.AssetPreviewField(_rect, _property, _label, _foldout)); }
/// <summary> /// Draw a required property field. /// </summary> /// <param name="_property">Property to draw and check validity.</param> /// <param name="_label">Label to display before property.</param> public static void RequiredProperty(SerializedProperty _property, GUIContent _label) { Rect _rect = EditorGUILayout.GetControlRect(true, EditorGUIUtilityEnhanced.GetRequiredPropertyHeight(_property)); EditorGUIEnhanced.RequiredProperty(_rect, _property, _label); }
/**************************** ******* READONLY ******* ***************************/ /// <summary> /// Draw a readonly property field. /// </summary> /// <param name="_position">Rect to draw field in</param> /// <param name="_property">Property to display</param> public static void ReadOnlyProperty(Rect _position, SerializedProperty _property) => ReadOnlyProperty(_position, _property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), false);
/********************************* ******* ASSET PREVIEW ******* ********************************/ /// <summary> /// Draw an property field with an asset preview below it. /// </summary> /// <param name="_property">Property to display and draw asset preview.</param> /// <param name="_foldout">The shown asset preview foldout state.</param> /// <param name="_previewAspect">Aspect of the displayed asset preview.</param> /// <returns>Returns new asset preview foldout state.</returns> public static bool AssetPreviewField(SerializedProperty _property, bool _foldout, float _previewAspect) => AssetPreviewField(_property, EditorGUIUtilityEnhanced.GetPropertyLabel(_property), _foldout, _previewAspect);