From 30a021152a83b1ac37aabc4c00f9985d76b56eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20V=C3=A1zquez?= Date: Thu, 4 Feb 2021 11:31:00 +0100 Subject: [PATCH] Fixed the display name of a Volume Parameter when is defined the attribute InspectorName --- com.unity.render-pipelines.core/CHANGELOG.md | 1 + .../Editor/Volume/VolumeComponentEditor.cs | 54 ++++++++++++------- .../Editor/Volumes/VolumeComponentTests.cs | 50 +++++++++++++++++ 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/com.unity.render-pipelines.core/CHANGELOG.md b/com.unity.render-pipelines.core/CHANGELOG.md index 54ed2042c17..c04a4b1d68d 100644 --- a/com.unity.render-pipelines.core/CHANGELOG.md +++ b/com.unity.render-pipelines.core/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed - Fixed missing warning UI about Projector component being unsupported (case 1300327). +- Fixed the display name of a Volume Parameter when is defined the attribute InspectorName ### Added - Support for the PlayStation 5 platform has been added. diff --git a/com.unity.render-pipelines.core/Editor/Volume/VolumeComponentEditor.cs b/com.unity.render-pipelines.core/Editor/Volume/VolumeComponentEditor.cs index 8dd38fcc68c..bcf0dcf2b02 100644 --- a/com.unity.render-pipelines.core/Editor/Volume/VolumeComponentEditor.cs +++ b/com.unity.render-pipelines.core/Editor/Volume/VolumeComponentEditor.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using UnityEditor.AnimatedValues; using UnityEngine; using UnityEngine.Assertions; using UnityEngine.Rendering; -using UnityEditor.AnimatedValues; namespace UnityEditor.Rendering { @@ -350,7 +350,7 @@ public virtual void OnInspectorGUI() // Display every field as-is foreach (var parameter in m_Parameters) { - if (parameter.displayName.text != "") + if (!string.IsNullOrEmpty(parameter.displayName.text)) PropertyField(parameter.param, parameter.displayName); else PropertyField(parameter.param); @@ -457,36 +457,50 @@ protected void PropertyField(SerializedDataParameter property) } /// - /// Draws a given in the editor using a custom label - /// and tooltip. + /// Handles unity built-in decorators (Space, Header, Tooltips, ...) from attributes /// - /// The property to draw in the editor. - /// A custom label and/or tooltip. - protected void PropertyField(SerializedDataParameter property, GUIContent title) + /// The property to obtain the attributes and handle the decorators + /// A custom label and/or tooltip that might be updated by and/or by + void HandleDecorators(SerializedDataParameter property, GUIContent title) { - // Handle unity built-in decorators (Space, Header, Tooltip etc) foreach (var attr in property.attributes) { - if (attr is PropertyAttribute) + if (!(attr is PropertyAttribute)) + continue; + + switch (attr) { - if (attr is SpaceAttribute) - { - EditorGUILayout.GetControlRect(false, (attr as SpaceAttribute).height); - } - else if (attr is HeaderAttribute) + case SpaceAttribute spaceAttribute: + EditorGUILayout.GetControlRect(false, spaceAttribute.height); + break; + case HeaderAttribute headerAttribute: { - var rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight); - rect.y += 0f; - rect = EditorGUI.IndentedRect(rect); - EditorGUI.LabelField(rect, (attr as HeaderAttribute).header, EditorStyles.miniLabel); + var rect = EditorGUI.IndentedRect(EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight)); + EditorGUI.LabelField(rect, headerAttribute.header, EditorStyles.miniLabel); + break; } - else if (attr is TooltipAttribute) + case TooltipAttribute tooltipAttribute: { if (string.IsNullOrEmpty(title.tooltip)) - title.tooltip = (attr as TooltipAttribute).tooltip; + title.tooltip = tooltipAttribute.tooltip; + break; } + case InspectorNameAttribute inspectorNameAttribute: + title.text = inspectorNameAttribute.displayName; + break; } } + } + + /// + /// Draws a given in the editor using a custom label + /// and tooltip. + /// + /// The property to draw in the editor. + /// A custom label and/or tooltip. + protected void PropertyField(SerializedDataParameter property, GUIContent title) + { + HandleDecorators(property, title); // Custom parameter drawer VolumeParameterDrawer drawer; diff --git a/com.unity.render-pipelines.core/Tests/Editor/Volumes/VolumeComponentTests.cs b/com.unity.render-pipelines.core/Tests/Editor/Volumes/VolumeComponentTests.cs index e15eb2311c4..83406ff7b31 100644 --- a/com.unity.render-pipelines.core/Tests/Editor/Volumes/VolumeComponentTests.cs +++ b/com.unity.render-pipelines.core/Tests/Editor/Volumes/VolumeComponentTests.cs @@ -105,5 +105,55 @@ public string[] AdditionalProperties(Type volumeComponentType) return fields; } + + #region Decorators Handling Test + + class VolumeComponentDecorators : VolumeComponent + { + [Tooltip("Increase to make the noise texture appear bigger and less")] + public FloatParameter _NoiseTileSize = new FloatParameter(25.0f); + + [InspectorName("Color")] + public ColorParameter _FogColor = new ColorParameter(Color.grey); + + [InspectorName("Size and occurrence"), Tooltip("Increase to make patches SMALLER, and frequent")] + public ClampedFloatParameter _HighNoiseSpaceFreq = new ClampedFloatParameter(0.1f, 0.1f, 1f); + } + + readonly (string displayName, string tooltip)[] k_ExpectedResults = + { + (string.Empty, "Increase to make the noise texture appear bigger and less"), + ("Color", string.Empty), + ("Size and occurrence", "Increase to make patches SMALLER, and frequent") + }; + + [Test] + public void TestHandleParameterDecorators() + { + var component = ScriptableObject.CreateInstance(); + var editor = (VolumeComponentEditor)Activator.CreateInstance(typeof(VolumeComponentEditor)); + editor.Invoke("Init", component, null); + + var parameters = + editor.GetField("m_Parameters") as List<(GUIContent displayName, int displayOrder, + SerializedDataParameter param)>; + + Assert.True(parameters != null && parameters.Count() == k_ExpectedResults.Count()); + + for (int i = 0; i < k_ExpectedResults.Count(); ++i) + { + var property = parameters[i].param; + var title = new GUIContent(parameters[i].displayName); + + editor.Invoke("HandleDecorators", property, title); + + Assert.True(k_ExpectedResults[i].displayName == title.text); + Assert.True(k_ExpectedResults[i].tooltip == title.tooltip); + } + + ScriptableObject.DestroyImmediate(component); + } + + #endregion } }